private void Awake() { if (inst == this) { return; } if (inst != null) { bool keepOther = conflictResolution == ConflictResolution.DestroyNewer || (id == inst.id && conflictResolution == ConflictResolution.DestroyNewerIfSameId); if (keepOther) { gameObject.SetActive(false); return; } else { inst.Stop(); Spawnable.Despawn(inst.gameObject, inst.GetComponent <AudioFade>().fadeDuration); } } audioSources = GetComponentsInChildren <AudioSource>(); currentIndex = shuffle ? UnityEngine.Random.Range(0, audioSources.Length) : 0; inst = this; if (persistThroughLoad) { DontDestroyOnLoad(gameObject); } }
/// <summary> /// A more versatile version of AudioSource.PlayClipAtPoint. /// </summary> /// <param name="clip">The clip to play.</param> /// <param name="point">The point at which to play.</param> /// <param name="volume">The volume to play at.</param> /// <param name="spatial">Spatial blend (0 = 2D, 1 = 3D).</param> /// <param name="pitch">The pitch to play at.</param> /// <param name="loop">Whether to loop the sound.</param> /// <param name="output">Output mixer group.</param> /// <param name="attach">Transform to attach the spawned AudioSource to.</param> /// <returns>The spawned AudioSource.</returns> public static AudioSource PlayClipAtPoint(AudioClip clip, Vector3 point, float volume = 1, float spatial = 1, float pitch = 1, bool loop = false, AudioMixerGroup output = null, Transform attach = null) { if (clip == null) { return(null); } GameObject obj = new GameObject(); obj.name = "One shot audio (SBR)"; obj.transform.parent = attach; obj.transform.position = point; var src = obj.AddComponent <AudioSource>(); src.clip = clip; src.loop = loop; src.spatialBlend = spatial; src.volume = volume; src.pitch = pitch; src.outputAudioMixerGroup = output; src.Play(); if (!loop) { Spawnable.Despawn(obj, clip.length); } return(src); }
private void OnParticleSystemStopped() { if (_despawnOnDone && _spawnable.IsSpawned) { Spawnable.Despawn(_spawnable); } }
public void Despawn(Spawnable instance) { Debug.Assert(instance.IsSpawned, $"Trying to despawn not-spawned instance {instance}"); instance.transform.SetParent(Transform, false); instance.WasDespawned(); instance.gameObject.SetActive(false); _freeList.Enqueue(instance.InstanceID); }
public static void Despawn(Spawnable instance, float inSeconds = 0.0f) { if (inSeconds <= 0.0f) { PoolManager.Instance.DespawnInstance(instance); } else { instance.Invoke(nameof(DespawnSelf), inSeconds); } }
public Spawnable SpawnInstance(Spawnable prefab, Vector3?position, Quaternion?rotation, Transform parent, bool inWorldSpace) { Debug.Assert(!prefab.IsSpawned); if (!_poolsByPrefab.TryGetValue(prefab, out SpawnPool pool)) { pool = new SpawnPool(prefab, _pools.Count, transform); _pools.Add(pool); _poolsByPrefab.Add(prefab, pool); } return(pool.Spawn(position, rotation, parent, inWorldSpace)); }
private void Update() { timeToLive -= Time.deltaTime; if (timeToLive <= 0) { if (spawnOnDeath) { Spawnable.Spawn(spawnOnDeath, transform.position, transform.rotation); } Spawnable.Despawn(gameObject); } }
private int Instantiate() { Spawnable inst = Object.Instantiate(Prefab, Transform, false); inst.gameObject.SetActive(false); inst.PoolID = PoolID; int instanceID = _instances.Count; inst.InstanceID = instanceID; _instances.Add(inst); _freeList.Enqueue(instanceID); return(instanceID); }
public override void Fire(Vector3 direction, bool align = true) { base.Fire(direction, align); RaycastHit hit; if (Physics.Raycast(transform.position, direction, out hit, range, hitMask, triggerInteraction)) { OnHitCollider(hit.collider, hit.point); } else { Spawnable.Despawn(gameObject, linger); } }
public Spawnable Spawn(Vector3?position, Quaternion?rotation, Transform parent, bool inWorldSpace) { if (_freeList.Count == 0) { Instantiate(); } int instanceID = _freeList.Dequeue(); Spawnable instance = _instances[instanceID]; Debug.Assert(!instance.IsSpawned, $"Trying to spawn already-spawned instance {instance}"); instance.transform.Initialize(position, rotation, null, parent, inWorldSpace); instance.gameObject.SetActive(true); instance.WasSpawned(); return(instance); }
protected virtual void OnHitObject(Transform col, Vector3 position) { Vector3 impact = velocity * impactForce; col.Damage(new PointDamage(damage * damageMultiplier, position, velocity.normalized, velocity.magnitude * impactForce)); if (impactForce > 0) { var rb = col.GetComponentInParent <Rigidbody>(); if (rb) { rb.AddForce(impact, ForceMode.Impulse); } var rb2d = col.GetComponentInParent <Rigidbody2D>(); if (rb2d) { rb2d.AddForce(impact, ForceMode2D.Impulse); } } HitObject?.Invoke(col.gameObject, position); if (stopOnHit) { velocity = Vector3.zero; } if (destroyOnHit) { Spawnable.Despawn(gameObject, linger); fired = false; } if (impactSound) { impactSound.PlayAtPoint(transform.position); } if (impactPrefab) { Spawnable.Spawn(impactPrefab, position, transform.rotation, parentImpactObject ? col : null, true); } }
public override void Fire(Vector3 direction, bool align = true) { base.Fire(direction, align); RaycastHit2D hit; bool trig = Physics2D.queriesHitTriggers; Physics2D.queriesHitTriggers = hitsTriggers; if (hit = Physics2D.Raycast(transform.position, direction, range, hitMask)) { OnHitCollider2D(hit.collider, hit.point); } else { Spawnable.Despawn(gameObject, linger); } Physics2D.queriesHitTriggers = trig; }
public static Spawnable Spawn(Spawnable prefab, Vector3?position = null, Quaternion?rotation = null, Transform parent = null, bool inWorldSpace = false) { return(PoolManager.Instance.SpawnInstance(prefab, position, rotation, parent, inWorldSpace)); }
private void Awake() { _spawnable = GetComponentInParent <Spawnable>(); _particles = GetComponent <ParticleSystem>(); }
public SpawnPool(Spawnable prefab, int poolID, Transform transform) { Prefab = prefab; PoolID = poolID; Transform = transform; }
public void DespawnInstance(Spawnable instance) { Debug.Assert(instance.IsSpawned); Debug.Assert(instance.PoolID >= 0 && instance.PoolID < _pools.Count); _pools[instance.PoolID].Despawn(instance); }