Пример #1
0
        /// <summary>
        /// 加载音效资源
        /// </summary>
        /// <returns></returns>
        IEnumerator LoadAudio_dynamic(DynamicAudioItem item)
        {
            if (item == null)
            {
                yield break;
            }
            if (item.is_destroy)
            {
                yield break;
            }

            GameObject go = ObjCachePoolMgr.Instance.LoadFromCache(ObjCachePoolType.SMALL_PREFAB, UISoundTemplate) as GameObject;

            if (go == null)
            {
                PrefabResource ar_prefab = new PrefabResource();

                yield return(ResourceLoader.Instance.StartCoroutine(ResourceLoader.Instance.load_prefab(UISoundTemplate, ar_prefab)));

                if (ar_prefab.obj_ == null)
                {
                    StopAudio_dynamic(item.unique_id);
                    yield break;
                }

                GameObject obj = ar_prefab.obj_;
                if (item.is_destroy)
                {
                    GameObject.DestroyImmediate(obj);
                    yield break;
                }

                PoolGameObject pg = obj.AddComponent <PoolGameObject>();
                if (pg != null)
                {
                    pg.poolType = ObjCachePoolType.SMALL_PREFAB;
                    pg.key      = UISoundTemplate;
                }
                go = obj;
            }

            item.audioSource = go.GetComponent <AudioSource>();
            if (item.audioSource == null)
            {
                item.audioSource = go.AddComponent <AudioSource>();
            }
            go.SetActive(true);
            Camera cam = Camera.main;

            if (cam == null)
            {
                cam = GameObject.FindObjectOfType(typeof(Camera)) as Camera;
            }
            if (cam != null)
            {
                item.audioSource.transform.SetParent(cam.transform);
                item.audioSource.transform.localScale       = Vector3.one;
                item.audioSource.transform.localPosition    = Vector3.zero;
                item.audioSource.transform.localEulerAngles = Vector3.zero;
            }

            AssetResource ar = new AssetResource();

            yield return(ResourceLoader.Instance.StartCoroutine(ResourceLoader.Instance.load_asset(item.res_path, typeof(Object), ar)));

            if (ar.asset_ == null)
            {
                GameDebug.LogError(string.Format("the res {0} does not exist", item.res_path));
                StopAudio_dynamic(item.unique_id);
                yield break;
            }
            if (item.is_destroy)
            {
                ar.destroy();
                yield break;
            }
            AudioClip audio_clip = ar.asset_ as AudioClip;

            if (audio_clip == null)
            {
                GameDebug.LogError(string.Format("the res {0} is not audio clip", item.res_path));
                ar.destroy();
                yield break;
            }

            item.clip             = audio_clip;
            item.audioSource.clip = item.clip;
            item.audioSource.loop = item.is_loop;
            float volume = GlobalSettings.GetInstance().SFXVolume;

            item.audioSource.volume = volume * item.volume;
            item.audioSource.mute   = GlobalSettings.GetInstance().SFXMute;
            item.audioSource.Play();
            UnityAction cbFunc = () =>
            {
                if (item != null)
                {
                    StopAudio_dynamic(item.unique_id);
                }
            };

            if (item.is_loop == false)
            {
                xc.MainGame.GetGlobalMono().StartCoroutine(DelayedCallback(item.clip.length, cbFunc));
            }
        }