/// <summary> /// Plays an AudioClip at a given position in world space. (Copy AudioSource.PlayClipAtPoint but using a Pool) /// </summary> /// <param name="clip">Audio data to play.</param> /// <param name="position">Position in world space from which sound originates.</param> /// <param name="volume">Playback volume.</param> /// <param pitch="volume">Playback pitch.</param> public static AudioSource PlayClipAtPoint(AudioClip clip, Vector3 position, float volume = 1f, float pitch = 1f) { AudioSource audioSource; if (!s_oneShotPool) { s_prefab = new GameObject("One shot audio"); s_prefab.transform.position = position; audioSource = (AudioSource)s_prefab.AddComponent(typeof(AudioSource)); s_oneShotPool = PoolManager.CreatePool(s_prefab); s_prefab.hideFlags = HideFlags.HideAndDontSave; } GameObject gameObject = s_oneShotPool.Instantiate(position); audioSource = gameObject.GetComponent <AudioSource>(); audioSource.clip = clip; audioSource.spatialBlend = 1f; audioSource.pitch = pitch; audioSource.volume = volume; audioSource.outputAudioMixerGroup = null; audioSource.Play(); float timeToLive = clip.length * Mathf.Max(Time.timeScale, 0.01f) / Mathf.Max(Mathf.Abs(pitch), 0.1f); s_oneShotPool.Destroy(gameObject, timeToLive); return(audioSource); }
/// <summary> /// Initializes a new instance of the ObjectPooler class. /// 初始化ObjectPooler类的新实例 /// </summary> public ObjectPool(GameObject objectPrefab, int initialSize, Transform parent = null, PoolBehaviour poolBehaviour = PoolBehaviour.GameObject) { this.objectPrefab = objectPrefab; this.parent = parent; this.poolBehaviour = poolBehaviour; for (int i = 0; i < initialSize; i++) { CreateObject(); } }
public bool Despawn(GameObject target) { if (spawn.Contains(target)) { PoolBehaviour pb = target.GetComponent <PoolBehaviour>(); if (pb && pb.visible) { --visibleCount; } target.SetActive(false); pool.Add(target); BroadcastDespawn(target); return(true); } return(false); }
public GameObject GetPool(GameObject prefab, Vector3 pos, Quaternion rotation) { GameObject obj; if (pool.Count < 1) { if (spawn.Count > template.count && template.dontCreate) { return(null); } obj = GameObject.Instantiate(prefab, pos, rotation); if (parent) { obj.transform.SetParent(parent); } obj.AddComponent <PoolBehaviour>(); spawn.Add(obj); pool.Add(obj); } obj = pool[0]; obj.transform.position = pos; obj.transform.rotation = rotation; obj.SetActive(true); BroadcastSpawn(obj); pool.RemoveAt(0); PoolBehaviour pb = obj.GetComponent <PoolBehaviour>(); pb.visible = visibleCount < template.count; if (pb.visible) { ++visibleCount; } return(obj); }
public SubPool(string key, GameObject reference, int count, Transform container, PoolBehaviour poolBehaviour) { if (string.IsNullOrEmpty(key) || reference == null || container == null) { return; } Key = key; m_Reference = reference; m_Container = container; m_PoolBehaviour = poolBehaviour; if (count < 1) { count = defaultCount; } IsInited = true; for (int i = 0; i < count; ++i) { CreateObject(); } }
public bool CreatePool(string key, GameObject obj, int count, PoolBehaviour poolBehaviour) { if (string.IsNullOrEmpty(key) || obj == null || ContainsKey(key)) { return(false); } Transform container = (new GameObject(string.Format("[container] {0}", key))).transform; container.parent = transform; GameObject reference = Instantiate(obj); reference.name = obj.name; reference.transform.parent = container; if (reference.activeInHierarchy) { reference.SetActive(false); } m_SubPoolList.Add(new SubPool(key, reference, count, container, poolBehaviour)); return(true); }
private void ReturnObject(PoolBehaviour poolObject) { poolObject.Reset(); poolObject.NextInactive = _firstInactive; _firstInactive = (T)poolObject; }
public GameObject GetPooledObject(string tag, bool active = true, bool canBeActive = false) { ClearNullElements(); PoolBehaviour obj = null; for (int i = 0; i < pooledObjects.Count; i++) { if (pooledObjects[i] == null) { continue; } if ((!pooledObjects[i].gameObject.activeSelf || canBeActive) && pooledObjects[i].name == tag) { Item item = pooledObjects[i].GetComponent <Item>(); if (item && item.canBePooled) { obj = pooledObjects[i]; } else if (!item) { obj = pooledObjects[i]; } if (obj) { break; } } } if (itemsToPool == null) { LoadFromScriptable(); } if (!obj) { foreach (var item in itemsToPool) { if (item != null && item.objectToPool == null) { continue; } if (item.objectToPool.name == tag) { if (item.shouldExpand) { obj = CreatePooledObject(item); break; } } } } if (LevelManager.THIS.DebugSettings.FallingLog) { DebugLogKeeper.Log(obj + " unpooled", DebugLogKeeper.LogType.Falling); } if (obj != null) { obj.gameObject.SetActive(active); return(obj.gameObject); } return(null); }