コード例 #1
0
        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);
            }
        }
コード例 #2
0
ファイル: Util.cs プロジェクト: vcmiller/SlightlyBetterRats
        /// <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);
        }
コード例 #3
0
 private void OnParticleSystemStopped()
 {
     if (_despawnOnDone && _spawnable.IsSpawned)
     {
         Spawnable.Despawn(_spawnable);
     }
 }
コード例 #4
0
            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);
            }
コード例 #5
0
 public static void Despawn(Spawnable instance, float inSeconds = 0.0f)
 {
     if (inSeconds <= 0.0f)
     {
         PoolManager.Instance.DespawnInstance(instance);
     }
     else
     {
         instance.Invoke(nameof(DespawnSelf), inSeconds);
     }
 }
コード例 #6
0
        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));
        }
コード例 #7
0
        private void Update()
        {
            timeToLive -= Time.deltaTime;

            if (timeToLive <= 0)
            {
                if (spawnOnDeath)
                {
                    Spawnable.Spawn(spawnOnDeath, transform.position, transform.rotation);
                }
                Spawnable.Despawn(gameObject);
            }
        }
コード例 #8
0
            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);
            }
コード例 #9
0
        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);
            }
        }
コード例 #10
0
            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);
            }
コード例 #11
0
        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);
            }
        }
コード例 #12
0
        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;
        }
コード例 #13
0
 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));
 }
コード例 #14
0
 private void Awake()
 {
     _spawnable = GetComponentInParent <Spawnable>();
     _particles = GetComponent <ParticleSystem>();
 }
コード例 #15
0
 public SpawnPool(Spawnable prefab, int poolID, Transform transform)
 {
     Prefab    = prefab;
     PoolID    = poolID;
     Transform = transform;
 }
コード例 #16
0
 public void DespawnInstance(Spawnable instance)
 {
     Debug.Assert(instance.IsSpawned);
     Debug.Assert(instance.PoolID >= 0 && instance.PoolID < _pools.Count);
     _pools[instance.PoolID].Despawn(instance);
 }