public Transform Spawn(Transform prefab, Vector3 pos, Quaternion rot) { Transform transform; foreach (PrefabPool current in this._prefabPools) { if (current.prefabGO == prefab.gameObject) { transform = current.SpawnInstance(pos, rot); Transform result; if (transform == null) { result = null; return(result); } if (!this.dontReparent && transform.parent != this.group) { transform.parent = this.group; } this._spawned.Add(transform); result = transform; return(result); } } PrefabPool prefabPool = new PrefabPool(prefab); this.CreatePrefabPool(prefabPool); transform = prefabPool.SpawnInstance(pos, rot); transform.parent = this.group; this._spawned.Add(transform); return(transform); }
/// <summary> /// 获取一个资源实例. /// </summary> public Transform Spawn(Transform prefab, Vector3 pos, Quaternion rot, Transform parent) { Transform inst; bool worldPositionStays; for (int i = 0; i < this._prefabPools.Count; i++) { if (this._prefabPools[i].prefabGO == prefab.gameObject) { inst = this._prefabPools[i].SpawnInstance(pos, rot); if (inst == null) { return(null); } worldPositionStays = !(inst is RectTransform); if (parent != null) { inst.SetParent(parent, worldPositionStays); } else if (!this.dontReparent && inst.parent != this.group) { inst.SetParent(this.group, worldPositionStays); } this._spawned.Add(inst); inst.gameObject.BroadcastMessage("OnSpawned", this, SendMessageOptions.DontRequireReceiver); return(inst); } } PrefabPool newPrefabPool = new PrefabPool(prefab); this.CreatePrefabPool(newPrefabPool); inst = newPrefabPool.SpawnInstance(pos, rot); worldPositionStays = !(inst is RectTransform); if (parent != null) { inst.SetParent(parent, worldPositionStays); } else if (!this.dontReparent && inst.parent != this.group) { inst.SetParent(this.group, worldPositionStays); } this._spawned.Add(inst); inst.gameObject.BroadcastMessage("OnSpawned", this, SendMessageOptions.DontRequireReceiver); return(inst); }
public Transform Spawn(Transform prefab, Vector3 pos, Quaternion rot, Transform parent) { int i = 0; Transform transform; while (i < this._prefabPools.Count) { if (this._prefabPools[i].prefabGO == prefab.gameObject) { transform = this._prefabPools[i].SpawnInstance(pos, rot); if (transform == null) { return(null); } if (parent != null) { transform.parent = parent; } else if (!this.dontReparent && transform.parent != this.group) { transform.parent = this.group; } this._spawned.Add(transform); transform.gameObject.BroadcastMessage("OnSpawned", this, SendMessageOptions.DontRequireReceiver); return(transform); } else { i++; } } PrefabPool prefabPool = new PrefabPool(prefab); this.CreatePrefabPool(prefabPool); transform = prefabPool.SpawnInstance(pos, rot); if (parent != null) { transform.parent = parent; } else { transform.parent = this.group; } this._spawned.Add(transform); transform.gameObject.BroadcastMessage("OnSpawned", this, SendMessageOptions.DontRequireReceiver); return(transform); }
/// <description> /// Spawns an instance or creates a new instance if none are available. /// Either way, an instance will be set to the passed position and /// rotation. /// /// Detailed Information: /// Checks the Data structure for an instance that was already created /// using the prefab. If the prefab has been used before, such as by /// setting it in the Unity Editor to preload instances, or just used /// before via this function, one of its instances will be used if one /// is available, or a new one will be created. /// /// If the prefab has never been used a new PrefabPool will be started /// with default options. /// /// To alter the options on a prefab pool, use the Unity Editor or see /// the documentation for the PrefabPool class and /// SpawnPool.SpawnPrefabPool() /// /// Broadcasts "OnSpawned" to the instance. Use this to manage states. /// /// An overload of this function has the same initial signature as Unity's /// Instantiate() that takes position and rotation. The return Type is different /// though. Unity uses and returns a GameObject reference. PoolManager /// uses and returns a Transform reference (or other supported type, such /// as AudioSource and ParticleSystem) /// </description> /// <param name="prefab"> /// The prefab used to spawn an instance. Only used for reference if an /// instance is already in the pool and available for respawn. /// NOTE: Type = Transform /// </param> /// <param name="pos">The position to set the instance to</param> /// <param name="rot">The rotation to set the instance to</param> /// <param name="parent">An optional parent for the instance</param> /// <returns> /// The instance's Transform. /// /// If the Limit option was used for the PrefabPool associated with the /// passed prefab, then this method will return null if the limit is /// reached. You DO NOT need to test for null return values unless you /// used the limit option. /// </returns> public Transform Spawn(Transform prefab, Vector3 pos, Quaternion rot, Transform parent) { Transform inst; #region Use from Pool for (int i = 0; i < this._prefabPools.Count; i++) { // Determine if the prefab was ever used as explained in the docs // I believe a comparison of two references is processor-cheap. if (this._prefabPools[i].prefabGO == prefab.gameObject) { // Now we know the prefabPool for this prefab exists. // Ask the prefab pool to setup and activate an instance. // If there is no instance to spawn, a new one is instanced inst = this._prefabPools[i].SpawnInstance(pos, rot); // This only happens if the limit option was used for this // Prefab Pool. if (inst == null) { return(null); } if (parent != null) // User explicitly provided a parent { inst.SetParent(parent); } else if (!this.dontReparent && inst.parent != this.group) // Auto organize? { // If a new instance was created, it won't be grouped inst.SetParent(this.group); } // Add to internal list - holds only active instances in the pool // This isn't needed for Pool functionality. It is just done // as a user-friendly feature which has been needed before. this._spawned.Add(inst); // Notify instance it was spawned so it can manage it's state inst.gameObject.BroadcastMessage( "OnSpawned", this, SendMessageOptions.DontRequireReceiver ); // Done! return(inst); } } #endregion Use from Pool #region New PrefabPool // The prefab wasn't found in any PrefabPools above. Make a new one PrefabPool newPrefabPool = new PrefabPool(prefab); this.CreatePrefabPool(newPrefabPool); // Spawn the new instance (Note: prefab already set in PrefabPool) inst = newPrefabPool.SpawnInstance(pos, rot); if (parent != null) // User explicitly provided a parent { inst.SetParent(parent); } else // Auto organize { inst.SetParent(this.group); } // New instances are active and must be added to the internal list this._spawned.Add(inst); #endregion New PrefabPool // Notify instance it was spawned so it can manage it's state inst.gameObject.BroadcastMessage( "OnSpawned", this, SendMessageOptions.DontRequireReceiver ); // Done! return(inst); }