Exemplo n.º 1
0
        void Start()
        {
            if (SoundMaterial == null || SoundMaterial.AudioSets.Count == 0)
            {
                return;
            }

            if (AutoCreateSources)
            {
                if (!ImpactAudio.isActiveAndEnabled)
                {
                    ImpactAudio = getAudioCopy(ImpactAudio, gameObject);
                }

                baseImpactVol   = ImpactAudio.volume;
                baseImpactPitch = ImpactAudio.pitch;

                audioContainersDic = new Dictionary <int, PhysSoundAudioContainer>();
                AudioContainers    = new List <PhysSoundAudioContainer>();

                foreach (PhysSoundAudioSet audSet in SoundMaterial.AudioSets)
                {
                    PhysSoundAudioContainer audCont = new PhysSoundAudioContainer(audSet.Key);
                    audCont.SlideAudio = getAudioCopy(ImpactAudio, this.gameObject);

                    audCont.Initialize(SoundMaterial.GetAudioSet(audCont.KeyIndex).Slide);
                    audioContainersDic.Add(audCont.KeyIndex, audCont);
                    AudioContainers.Add(audCont);
                }

                ImpactAudio.loop = false;
            }
            else
            {
                if (ImpactAudio)
                {
                    ImpactAudio.loop = false;
                    baseImpactVol    = ImpactAudio.volume;
                    baseImpactPitch  = ImpactAudio.pitch;
                }

                if (AudioContainers.Count > 0)
                {
                    audioContainersDic = new Dictionary <int, PhysSoundAudioContainer>();

                    foreach (PhysSoundAudioContainer audCont in AudioContainers)
                    {
                        if (!SoundMaterial.HasAudioSet(audCont.KeyIndex))
                        {
                            Debug.LogError("PhysSound Object " + gameObject.name + " has an audio container for an invalid Material Type! Select this object in the hierarchy to update its audio container list.");
                            continue;
                        }

                        audCont.Initialize(SoundMaterial.GetAudioSet(audCont.KeyIndex).Slide);
                        audioContainersDic.Add(audCont.KeyIndex, audCont);
                    }
                }
            }
        }
        /// <summary>
        /// Initializes the PhysSoundObject. Use this if you adding a PhysSoundObject component to an object at runtime.
        /// </summary>
        public void Initialize()
        {
            _r   = GetComponent <Rigidbody>();
            _r2D = GetComponent <Rigidbody2D>();

            if (AutoCreateSources)
            {
                baseImpactVol   = ImpactAudio.volume;
                baseImpactPitch = ImpactAudio.pitch;

                _audioContainersDic = new Dictionary <int, PhysSoundAudioContainer>();
                AudioContainers     = new List <PhysSoundAudioContainer>();

                foreach (PhysSoundAudioSet audSet in SoundMaterial.AudioSets)
                {
                    if (audSet.Slide == null)
                    {
                        continue;
                    }

                    PhysSoundAudioContainer audCont = new PhysSoundAudioContainer(audSet.Key);
                    audCont.SlideAudio = PhysSoundTempAudioPool.GetAudioSourceCopy(ImpactAudio, this.gameObject);

                    audCont.Initialize(SoundMaterial);
                    _audioContainersDic.Add(audCont.KeyIndex, audCont);
                    AudioContainers.Add(audCont);
                }

                ImpactAudio.loop = false;
            }
            else
            {
                if (ImpactAudio)
                {
                    ImpactAudio.loop = false;
                    baseImpactVol    = ImpactAudio.volume;
                    baseImpactPitch  = ImpactAudio.pitch;
                }

                if (AudioContainers.Count > 0)
                {
                    _audioContainersDic = new Dictionary <int, PhysSoundAudioContainer>();

                    foreach (PhysSoundAudioContainer audCont in AudioContainers)
                    {
                        if (!SoundMaterial.HasAudioSet(audCont.KeyIndex))
                        {
                            Debug.LogError("PhysSound Object " + gameObject.name + " has an audio container for an invalid Material Type! Select this object in the hierarchy to update its audio container list.");
                            continue;
                        }

                        audCont.Initialize(SoundMaterial);
                        _audioContainersDic.Add(audCont.KeyIndex, audCont);
                    }
                }
            }

            if (PlayClipAtPoint)
            {
                PhysSoundTempAudioPool.Create();
            }
            else if (ImpactAudio != null && !ImpactAudio.isActiveAndEnabled)
            {
                ImpactAudio = PhysSoundTempAudioPool.GetAudioSourceCopy(ImpactAudio, gameObject);
            }
        }