本章项目成果展示
我们打开上一篇38管理器基类的项目,

本章要做的事情是利用对象池制作第三个职业------魔法师
在GameRoot对象下创建空物体

重命名为PoolRoot
将GameRoot拖拽至预制体

创建脚本:

编写脚本:PoolManager.cs

cs
using UnityEngine;
public class PoolManager : ManagerBase<PoolManager>{
//根节点
[SerializeField]
GameObject poolRootObj;
public override void Init(){
base.Init();
Debug.Log("PoolManager 初始化成功");
}
}
将脚本挂载至GameRoot对象身上
并将空物体绑定在父物体框选下

创建脚本:GameObjectPoolData.cs


cs
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameObjectPoolData{
//对象池中 父节点
public GameObject fatherObj;
//对象容器
public Queue<GameObject> poolQueue;
public GameObjectPoolData(GameObject obj, GameObject poolRootObj){
//创建父节点 并设置到对象池根节点下方
fatherObj = new GameObject(obj.name);
fatherObj.transform.SetParent(poolRootObj.transform);
poolQueue = new Queue<GameObject>();
//把首次创建时候 需要放入的对象 放进容器
PushObj(obj);
}
/// <summary>
/// 放进
/// </summary>
public void PushObj(GameObject obj) {
//对象进容器
poolQueue.Enqueue(obj);
//设置父物体
obj.transform.SetParent(fatherObj.transform);
//设置隐藏
obj.SetActive(false);
obj.transform.position = GameObject.FindWithTag("Player").transform.position;
}
/// <summary>
/// 拿出
/// </summary>
public GameObject GetObj() {
GameObject obj = poolQueue.Dequeue();
//显示对象
obj.SetActive(true);
//父物体置空
obj.transform.parent = null;
//回归默认场景
SceneManager.MoveGameObjectToScene(obj, SceneManager.GetActiveScene());
return obj;
}
}
创建脚本:BulletShooting.cs

cs
using UnityEngine;
public class BulletShooting : MonoBehaviour{
public GameObject bulletObj;
void Update(){
if (Input.GetKeyDown(KeyCode.A)) {
PoolManager.Instance.GetGameObject<UsePool>(bulletObj);
}
}
}
将射击脚本挂载至GameRoot对象身上(UsePool.cs脚本在下面)

在资源商店下载免费子弹bullet预制体


解压缩预制体后重命名为Bullet

新建脚本:UsePool.cs

cs
using UnityEngine;
public class UsePool : MonoBehaviour{
void OnEnable(){
Invoke("Dead",3);
}
void Dead() {
PoolManager.Instance.PushGameObject(gameObject);
}
}
新建脚本:BulletMove.cs

cs
using UnityEngine;
public class BulletMove : MonoBehaviour{
public Vector3 nowPosition;
float speed = 13f;
void Awake(){
nowPosition = transform.position;
}
void Start(){
transform.position = new Vector3(transform.position.x + 3,
transform.position.y + 2f, transform.position.z);
}
void Update(){
transform.position += transform.forward * speed * Time.deltaTime;
}
}
挂载脚本:

将Bullet对象拖拽至射击脚本

运行项目 - 连续按A键

对象池 已完成
调整人物动画控制器制作第三个职业
在资源商店下载免费资源人物动画 及 武器模型



同以往文章切换职业一样增加第三个角色
运行项目 - 按E键切换至魔法师职业 连续按A键

本章利用GameObject对象池做了第三职业 ------ 魔法师
下篇文章的内容:
1.播放动画时禁止点击移动
2.窗口可拖拽脚本
3.点击名称寻找地点功能
4.隐藏怪物的生成
5.怪物I攻击范围内的主动攻击
6.掉落坐骑蛋的获取
7.异步传送转换场景
以及开放回合制、坐骑系统、宠物系统、背包系统、神炼系统、商城系统、Boss的目标跟随任务导航系统以及UI播放3D动画效果等等。
具体项目运行效果请关注water1024的b站视频项目演示《破碎纪元》
