Example #1
0
        public bool FadeOut(VolumeFadeData data)
        {
            //Debug.Log("FadeOut:"+ data.au.AssetName);
            if (data.au == null || data.au.audioSource == null || data.au.audioSource.clip == null)
            {
                data.au.Volume  = 0;
                data.tempVolume = 0;
                return(true);
            }

            float oldVolume = data.tempVolume;

            float speed = data.au.GetMaxRealVolume() / data.fadeTime;

            oldVolume = oldVolume - speed * Time.unscaledDeltaTime;
            //Debug.Log("FadeOut:data.au.Volume " + data.au.Volume + "  dowm :" + oldVolume);
            //Debug.Log(" FadeOut fade State :" + data.fadeState);
            data.au.Volume  = oldVolume;
            data.tempVolume = oldVolume;

            if (oldVolume > 0)
            {
                return(false);
            }
            else
            {
                data.au.Volume = 0;

                return(true);
            }
        }
Example #2
0
        /// <summary>
        /// 声音淡入
        /// </summary>
        /// <param name="data"></param>
        /// <returns>返回true,淡入完成</returns>
        private bool FadeIn(VolumeFadeData data)
        {
            //Debug.Log("FadeIn");
            if (data.au == null || data.au.audioSource == null || data.au.audioSource.clip == null)
            {
                data.au.ResetVolume();
                data.tempVolume = 1;
                return(true);
            }
            float oldVolume = data.tempVolume;
            //Debug.Log("FadeIn:data.au.Volume " + data.au.Volume + "  dowm :" + oldVolume);
            float speed = data.au.GetMaxRealVolume() / data.fadeTime * 2f;

            oldVolume = oldVolume + speed * Time.unscaledDeltaTime;

            data.au.Volume  = oldVolume;
            data.tempVolume = oldVolume;

            if (oldVolume < data.au.GetMaxRealVolume())
            {
                return(false);
            }
            else
            {
                data.au.ResetVolume();

                return(true);
            }
        }
Example #3
0
        public void UpdateFade()
        {
            //Debug.LogWarning("UpdateFade Count: " + fadeData.Count);
            if (fadeData.Count > 0)
            {
                foreach (var item in fadeData.Values)
                {
                    bool isComplete = false;
                    switch (item.fadeType)
                    {
                    case VolumeFadeType.FadeIn:
                        isComplete = FadeIn(item);
                        break;

                    case VolumeFadeType.FadeOut:
                        isComplete = FadeOut(item);
                        break;

                    case VolumeFadeType.FadeOut2In:
                        //Debug.Log("FadeOut2In");
                        isComplete = FadeOut2In(item);
                        break;
                    }
                    if (isComplete)
                    {
                        //Debug.Log("isComplete");
                        deleteAssets.Add(item.au);
                    }
                }

                if (deleteAssets.Count > 0)
                {
                    for (int i = 0; i < deleteAssets.Count; i++)
                    {
                        AudioAsset     au   = deleteAssets[i];
                        VolumeFadeData data = fadeData[au];
                        if (data.fadeCompleteCallBack != null)
                        {
                            data.fadeCompleteCallBack(au);
                        }

                        data.fadeCompleteCallBack    = null;
                        data.fadeOutCompleteCallBack = null;
                        catcheData.Enqueue(data);
                        //Debug.Log("Remove");
                        fadeData.Remove(au);
                    }

                    deleteAssets.Clear();
                }
            }
        }
Example #4
0
        public bool FadeOut2In(VolumeFadeData data)
        {
            //Debug.Log("FadeOut2In:data.au.Volume " + data.au.Volume + "  dowm :" + data.tempVolume+ " data.fadeState:"+ data.fadeState+ "  data.fadeType:" + data.fadeType+" :"+JsonUtils.ToJson(data));

            if (data.fadeState == VolumeFadeStateType.FadeOut)
            {
                if (FadeOut(data))
                {
                    data.fadeState = VolumeFadeStateType.Delay;

                    if (data.fadeOutCompleteCallBack != null)
                    {
                        data.fadeOutCompleteCallBack(data.au);
                    }
                    return(false);
                }
            }
            else if (data.fadeState == VolumeFadeStateType.Delay)
            {
                data.delayTime -= Time.unscaledDeltaTime;
                //Debug.Log("Delay : data.delayTime:" + data.delayTime + "  Time.unscaledDeltaTime:" + Time.unscaledDeltaTime);
                if (data.delayTime <= 0)
                {
                    data.fadeState = VolumeFadeStateType.FadeIn;
                    return(false);
                }
            }
            else if (data.fadeState == VolumeFadeStateType.FadeIn)
            {
                if (FadeIn(data))
                {
                    data.fadeState = VolumeFadeStateType.Complete;
                    return(true);
                }
            }

            return(false);
        }
Example #5
0
        public void AddFade(AudioAsset au, VolumeFadeType fadeType, float fadeTime, float delay, string flag, CallBack <AudioAsset> fadeCompleteCallBack, CallBack <AudioAsset> fadeOutCompleteCallBack)
        {
            VolumeFadeData data = null;

            if (fadeData.ContainsKey(au))
            {
                data = fadeData[au];
            }
            else
            {
                if (catcheData.Count > 0)
                {
                    data = catcheData.Dequeue();
                }
                else
                {
                    data = new VolumeFadeData();
                }
                fadeData.Add(au, data);
                //data.tempVolume = 0;
                //Debug.Log("Add");
            }
            if (data.fadeOutCompleteCallBack != null)
            {
                data.fadeOutCompleteCallBack(data.au);
            }
            if (data.fadeCompleteCallBack != null)
            {
                data.fadeCompleteCallBack(data.au);
            }

            data.fadeCompleteCallBack    = null;
            data.fadeOutCompleteCallBack = null;
            data.au = au;
            if (flag != null)
            {
                au.flag = flag;
            }
            data.fadeType = fadeType;
            data.fadeTime = fadeTime;
            if (data.fadeTime <= 0)
            {
                data.fadeTime = 0.000001f;
            }
            data.fadeCompleteCallBack    = fadeCompleteCallBack;
            data.fadeOutCompleteCallBack = fadeOutCompleteCallBack;

            switch (data.fadeType)
            {
            case VolumeFadeType.FadeIn:
                data.au.Volume = 0;
                data.fadeState = VolumeFadeStateType.FadeIn;
                break;

            case VolumeFadeType.FadeOut:
                data.fadeState = VolumeFadeStateType.FadeOut;
                data.au.ResetVolume();
                break;

            case VolumeFadeType.FadeOut2In:
                data.fadeState = VolumeFadeStateType.FadeOut;
                data.au.ResetVolume();
                break;
            }
            data.tempVolume = data.au.Volume;
            data.delayTime  = delay;
            //Debug.Log("AddFade:"+ data.fadeType);
            //Debug.Log("Add  AddFade:" + JsonUtils.ToJson(data));
        }