//对象池-放回游戏物体 public void OnUnSpawn(GameObject go) { if (go == null) { return; } ReusableObject reusableCom = go.GetComponent <ReusableObject>(); if (reusableCom != null) { reusableCom.IsIdle = true; //将物体置于空闲状态 } }
//对象池-取游戏物体 public GameObject OnSpawn() { GameObject go = null; go = GetIdleReuasbleGo(); if (go == null) //如果对象池中没有空闲的物体,则需要创建新物体对象 { go = GameObject.Instantiate(m_prefab); ReusableObject reusableCom = go.GetComponent <ReusableObject>(); if (reusableCom != null) { m_goList.Add(reusableCom); reusableCom.IsIdle = false; //将物体置于正在被使用的状态 } else { GameObject.Destroy(go); //如果物体不具有ReusableObject则销毁创建出来的物体对象 go = null; } } return(go); }