Exemplo n.º 1
0
        protected static unsafe uint PlaySource(EntityManager mgr, Entity e)
        {
            if (mgr.HasComponent <AudioSource>(e))
            {
                AudioSource audioSource = mgr.GetComponentData <AudioSource>(e);
                Entity      clipEntity  = audioSource.clip;
                DynamicBuffer <AudioClipUncompressed> audioClipUncompressed = mgr.GetBuffer <AudioClipUncompressed>(clipEntity);

                if (mgr.HasComponent <AudioNativeClip>(clipEntity))
                {
                    AudioNativeClip audioNativeClip = mgr.GetComponentData <AudioNativeClip>(clipEntity);
                    if (audioNativeClip.clipID > 0)
                    {
                        bool decompressOnPlay = false;
                        if (mgr.HasComponent <AudioClip>(clipEntity))
                        {
                            AudioClip audioClip = mgr.GetComponentData <AudioClip>(clipEntity);
                            decompressOnPlay = (audioClip.loadType == AudioClipLoadType.DecompressOnPlay);

                            if (decompressOnPlay && (audioClipUncompressed.Length == 0))
                            {
                                return(0);
                            }

                            audioClip.status = AudioClipStatus.Loaded;
                            mgr.SetComponentData <AudioClip>(clipEntity, audioClip);
                        }

                        // If there is an existing source, it should re-start.
                        // Do this with a Stop() and let it play below.
                        if (mgr.HasComponent <AudioSourceID>(e))
                        {
                            AudioSourceID ans = mgr.GetComponentData <AudioSourceID>(e);
                            AudioNativeCalls.Stop(ans.sourceID);
                        }

                        float volume = audioSource.volume;
                        float pan    = mgr.HasComponent <Audio2dPanning>(e) ? mgr.GetComponentData <Audio2dPanning>(e).pan : 0.0f;

                        // For 3d sounds, we start at volume zero because we don't know if this sound is close or far from the listener.
                        // It is much smoother to ramp up volume from zero than the alternative.
                        if (mgr.HasComponent <Audio3dPanning>(e))
                        {
                            volume = 0.0f;
                        }

                        uint sourceID = AudioNativeCalls.Play(audioNativeClip.clipID, volume, pan, audioSource.loop ? 1 : 0);
                        return(sourceID);
                    }
                }
            }

            return(0);
        }
Exemplo n.º 2
0
        protected override bool PlaySource(Entity e)
        {
            var mgr = EntityManager;

            if (mgr.HasComponent <AudioSource>(e))
            {
                AudioSource audioSource = mgr.GetComponentData <AudioSource>(e);

                Entity clipEntity = audioSource.clip;
                if (mgr.HasComponent <AudioNativeClip>(clipEntity))
                {
                    AudioNativeClip clip = mgr.GetComponentData <AudioNativeClip>(clipEntity);
                    if (clip.clipID > 0)
                    {
                        // If there is an existing source, it should re-start.
                        // Do this with a Stop() and let it play below.
                        if (mgr.HasComponent <AudioNativeSource>(e))
                        {
                            AudioNativeSource ans = mgr.GetComponentData <AudioNativeSource>(e);
                            AudioNativeCalls.Stop(ans.sourceID);
                        }

                        float volume = audioSource.volume;
                        float pan    = mgr.HasComponent <Audio2dPanning>(e) ? mgr.GetComponentData <Audio2dPanning>(e).pan : 0.0f;

                        // For 3d sounds, we start at volume zero because we don't know if this sound is close or far from the listener.
                        // It is much smoother to ramp up volume from zero than the alternative.
                        if (mgr.HasComponent <Audio3dPanning>(e))
                        {
                            volume = 0.0f;
                        }

                        uint sourceID = AudioNativeCalls.Play(clip.clipID, volume, pan, audioSource.loop);

                        AudioNativeSource audioNativeSource = new AudioNativeSource()
                        {
                            sourceID = sourceID
                        };
                        if (mgr.HasComponent <AudioNativeSource>(e))
                        {
                            mgr.SetComponentData(e, audioNativeSource);
                        }
                        else
                        {
                            mgr.AddComponentData(e, audioNativeSource);
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        protected override bool PlaySource(Entity e)
        {
            var mgr = EntityManager;

            if (mgr.HasComponent <AudioSource>(e))
            {
                AudioSource audioSource = mgr.GetComponentData <AudioSource>(e);

                if (mgr.HasComponent <AudioNativeSource>(e))
                {
                    // If there is a native source and it is IsPlaying() then
                    // can't play another, but we are done. (So return true.)
                    // Note that IsPlaying() is synchronous (which is what we want)
                    // as opposed to isPlaying which is async.
                    AudioNativeSource ans = mgr.GetComponentData <AudioNativeSource>(e);
                    if (AudioNativeCalls.IsPlaying(ans.sourceID))
                    {
                        return(true);
                    }
                }

                Entity clipEntity = audioSource.clip;
                if (mgr.HasComponent <AudioNativeClip>(clipEntity))
                {
                    AudioNativeClip clip = mgr.GetComponentData <AudioNativeClip>(clipEntity);
                    if (clip.clipID > 0)
                    {
                        uint sourceID = AudioNativeCalls.Play(clip.clipID, audioSource.volume, audioSource.loop);
                        AudioNativeSource audioNativeSource = new AudioNativeSource()
                        {
                            sourceID = sourceID
                        };
                        if (mgr.HasComponent <AudioNativeSource>(e))
                        {
                            mgr.SetComponentData(e, audioNativeSource);
                        }
                        else
                        {
                            PostUpdateCommands.AddComponent(e, audioNativeSource);
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 4
0
        protected override bool PlaySource(Entity e)
        {
            var mgr = EntityManager;

            if (mgr.HasComponent <AudioSource>(e))
            {
                AudioSource audioSource = mgr.GetComponentData <AudioSource>(e);

                Entity clipEntity = audioSource.clip;
                if (mgr.HasComponent <AudioNativeClip>(clipEntity))
                {
                    AudioNativeClip clip = mgr.GetComponentData <AudioNativeClip>(clipEntity);
                    if (clip.clipID > 0)
                    {
                        // If there is an existing source, it should re-start.
                        // Do this with a Stop() and let it play below.
                        if (mgr.HasComponent <AudioNativeSource>(e))
                        {
                            AudioNativeSource ans = mgr.GetComponentData <AudioNativeSource>(e);
                            AudioNativeCalls.Stop(ans.sourceID);
                        }

                        uint sourceID = AudioNativeCalls.Play(clip.clipID, audioSource.volume, audioSource.loop);

                        AudioNativeSource audioNativeSource = new AudioNativeSource()
                        {
                            sourceID = sourceID
                        };
                        if (mgr.HasComponent <AudioNativeSource>(e))
                        {
                            mgr.SetComponentData(e, audioNativeSource);
                        }
                        else
                        {
                            PostUpdateCommands.AddComponent(e, audioNativeSource);
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }