/// <summary> /// 添加对象池的方法 /// </summary> /// <param name="name">Name.</param> void Register(string name) { GameObject obj = Resources.Load(name) as GameObject; Subpool subpool = new Subpool(obj); poolDic.Add(name, subpool); }
/// <summary> /// 添加对象池的方法 /// </summary> /// <param name="name">Name.</param> void Register(GameObject _obj) { GameObject obj = _obj; Subpool subpool = new Subpool(obj); poolDic.Add(obj.name, subpool); }
/// <summary> /// 获取对象池中游戏对象 /// </summary> /// <param name="name">Name.</param> public GameObject Spawn(string name) { if (!poolDic.ContainsKey(name)) { Register(name); } Subpool subpool = poolDic [name]; return(subpool.SubPoolSpawn()); }
/// <summary> /// 获取对象池中游戏对象 /// </summary> /// <param name="name">Name.</param> public GameObject Spawn(GameObject obj, Transform parent, Vector3 position, Quaternion quaternion) { if (!poolDic.ContainsKey(obj.name)) { Register(obj); } Subpool subpool = poolDic[obj.name]; return(subpool.SubPoolSpawn(parent, position, quaternion)); }
protected Subpool CreatePool(string type) { // find the requested resource GameObject template; try { template = Instantiate(Resources.Load("Prefabs/" + type)) as GameObject; } catch (Exception exception) { // on error, log and return null Debug.LogError(string.Format("Could not instantiate resource of type: {0}. Exception: {1}", type, exception)); return(null); } // create a new subpool object and parent it to this gameObject GameObject subpoolObject = new GameObject(); subpoolObject.transform.SetParent(this.transform); // rename it based on type subpoolObject.name = string.Format("{0}Pool", type); // add a Subpool component to it Subpool subpool = subpoolObject.AddComponent <Subpool>(); // add the Subpool to the subpool dictionary this._subpools.Add(type, subpool); // disable the template template.SetActive(false); // parent it to the subpool template.transform.SetParent(subpoolObject.transform); // name it based on the type template.name = type; // set the subpool's template and return subpool.template = template; return(subpool); }