| using UnityEngine;  | 
| using System.Collections.Generic;  | 
|   | 
| using System;  | 
| using System.Collections;  | 
|   | 
|   | 
| public class SFXPlayUtility : SingletonMonobehaviour<SFXPlayUtility>  | 
| {  | 
|   | 
|     private Dictionary<int, ObjectPool<SFXController>> m_PoolDict = new Dictionary<int, ObjectPool<SFXController>>();  | 
|   | 
|     public SFXController PlayEffectAsync(int id, Transform parent, float scale = 1)  | 
|     {  | 
|         var config = EffectConfig.Get(id);  | 
|         var bundleName = StringUtility.Contact(ResourcesPath.EFFECT_Folder_Name, config.packageName);  | 
|         if (AssetSource.effectFromEditor || AssetBundleUtility.Instance.JudgeExistAsset(bundleName, config.fxName))  | 
|         {  | 
|             return PlayBattleEffect(id, parent, scale);  | 
|         }  | 
|         else  | 
|         {  | 
|             AssetBundleUtility.Instance.Co_LoadAsset(bundleName, config.fxName, OnEffectLoaded);  | 
|             return null;  | 
|         }  | 
|     }  | 
|   | 
|     public SFXController PlayEffectAsync(int id, GActor parent, float scale = 1)  | 
|     {  | 
|         var config = EffectConfig.Get(id);  | 
|         var bundleName = StringUtility.Contact(ResourcesPath.EFFECT_Folder_Name, config.packageName);  | 
|         if (AssetSource.effectFromEditor || AssetBundleUtility.Instance.JudgeExistAsset(bundleName, config.fxName))  | 
|         {  | 
|             //Debug.Log("已经存在此特效了...这里进行播放...");  | 
|             return PlayBattleEffect(id, parent, scale);  | 
|         }  | 
|         else  | 
|         {  | 
|             //Debug.LogFormat("不存在此特效, 这里异步加载...  {0} - {1}", bundleName, config.fxName);  | 
|             AssetBundleUtility.Instance.Co_LoadAsset(bundleName, config.fxName, OnEffectLoaded);  | 
|             return null;  | 
|         }  | 
|     }  | 
|   | 
|     private void OnEffectLoaded(bool result, UnityEngine.Object obj)  | 
|     {  | 
|         if (result)  | 
|         {  | 
|             var _prefab = obj as GameObject;  | 
|             if (_prefab)  | 
|             {  | 
|                 //Debug.LogFormat("异步加载...  {0} ....成功.", obj.name);  | 
|                 GameObjectPoolManager.Instance.CacheGameObject(_prefab, 1, false);  | 
|             }  | 
|         }  | 
|     }  | 
|   | 
|     public SFXController PlayBattleEffect(int id, Transform parent, float scale = 1f)  | 
|     {  | 
|         if (parent == null)  | 
|         {  | 
|             return null;  | 
|         }  | 
|         SFXController _controller = Play(id, parent, scale);  | 
|         return _controller;  | 
|     }  | 
|   | 
|     public SFXController PlayBattleEffect(int id, GActor actor, float scale = 1f)  | 
|     {  | 
|         EffectConfig _effectModel = EffectConfig.Get(id);  | 
|   | 
|         if (_effectModel == null)  | 
|         {  | 
|             return null;  | 
|         }  | 
|   | 
|         Transform _parent = null;  | 
|   | 
|         if (!string.IsNullOrEmpty(_effectModel.nodeName))  | 
|         {  | 
|             _parent = actor.Root.GetChildTransformDeeply(_effectModel.nodeName);  | 
|         }  | 
|   | 
|         if (!_parent)  | 
|         {  | 
|             if (_effectModel.setParent == 2)  | 
|             {  | 
|                 _parent = actor.MP_Name;  | 
|             }  | 
|             else if (_effectModel.setParent == 3)  | 
|             {  | 
|                 _parent = actor.MP_Hit;  | 
|             }  | 
|             else if (_effectModel.setParent == 4)  | 
|             {  | 
|                 if (actor.MP_Weapon)  | 
|                 {  | 
|                     _parent = actor.MP_Weapon;  | 
|                 }  | 
|                 else  | 
|                 {  | 
|                     _parent = actor.Root;  | 
|                 }  | 
|             }  | 
|             else if (_effectModel.setParent == 1  | 
|                   || _effectModel.setParent == 0)  | 
|             {  | 
|                 _parent = actor.Root;  | 
|             }  | 
|         }  | 
|   | 
|         return PlayBattleEffect(id, _parent, scale);  | 
|     }  | 
|   | 
|     public SFXController PlayBattleEffect(int id, Vector3 position, Vector3 forward, float scale = 1f)  | 
|     {  | 
|         SFXController _controller = Play(id, position, forward, scale);  | 
|   | 
|         return _controller;  | 
|     }  | 
|   | 
|     public SFXController PlayEffectAsync(int id, Vector3 position, Vector3 forward, float scale = 1)  | 
|     {  | 
|         var config = EffectConfig.Get(id);  | 
|         var bundleName = StringUtility.Contact(ResourcesPath.EFFECT_Folder_Name, config.packageName);  | 
|         if (AssetSource.effectFromEditor || AssetBundleUtility.Instance.JudgeExistAsset(bundleName, config.fxName))  | 
|         {  | 
|             return PlayBattleEffect(id, position, forward, scale);  | 
|         }  | 
|         else  | 
|         {  | 
|             // Debug.LogFormat("[{0}] 开始异步加载: {1}", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), config.fxName);  | 
|             AssetBundleUtility.Instance.Co_LoadAsset(bundleName, config.fxName, OnEffectLoaded);  | 
|             return null;  | 
|         }  | 
|     }  | 
|   | 
|     public SFXController Play(int id, Transform parent, float scale = 1f)  | 
|     {  | 
|         if (parent == null)  | 
|             return null;  | 
|   | 
|         SFXController _controller = Request(id);  | 
|   | 
|         if (_controller == null)  | 
|         {  | 
|             return null;  | 
|         }  | 
|   | 
|         EffectConfig _effectModel = EffectConfig.Get(id);  | 
|   | 
|         if (_effectModel.setParent > 0)  | 
|         {  | 
|             _controller.transform.SetParent(parent);  | 
|             _controller.transform.localPosition = Vector3.zero;  | 
|             _controller.transform.localRotation = Quaternion.identity;  | 
|             _controller.transform.localScale = Vector3.one * scale;  | 
|         }  | 
|         else  | 
|         {  | 
|             _controller.transform.position = parent.position;  | 
|             _controller.transform.forward = parent.forward;  | 
|             _controller.transform.localScale = Vector3.one * scale;  | 
|         }  | 
|   | 
|         _controller.startTime = 0;  | 
|         _controller.effectId = id;  | 
|   | 
|         _controller.gameObject.SetActive(true);  | 
|   | 
|         var config = EffectConfig.Get(id);  | 
|         if (config != null)  | 
|         {  | 
|             SoundPlayer.Instance.PlayUIAudio(config.audio);  | 
|         }  | 
|   | 
|         return _controller;  | 
|     }  | 
|   | 
|     public SFXController Play(int id, Vector3 position, Vector3 forward, float scale = 1f)  | 
|     {  | 
|         SFXController _controller = Request(id);  | 
|   | 
|         if (_controller == null)  | 
|         {  | 
|             return null;  | 
|         }  | 
|   | 
|         _controller.startTime = 0;  | 
|         _controller.effectId = id;  | 
|         _controller.transform.position = position;  | 
|         if (forward != Vector3.zero)  | 
|         {  | 
|             _controller.transform.forward = forward;  | 
|         }  | 
|         _controller.transform.localScale = Vector3.one * scale;  | 
|         _controller.gameObject.SetActive(true);  | 
|   | 
|         var config = EffectConfig.Get(id);  | 
|         if (config != null)  | 
|         {  | 
|             SoundPlayer.Instance.PlayUIAudio(config.audio);  | 
|         }  | 
|   | 
|         return _controller;  | 
|     }  | 
|   | 
|     public SFXController PlayWithEulerAngle(int id, Vector3 position, Vector3 eulerAngle, float scale = 1f)  | 
|     {  | 
|         SFXController _controller = Request(id);  | 
|   | 
|         if (_controller == null)  | 
|         {  | 
|             return null;  | 
|         }  | 
|         if (1044 == id)  | 
|         {  | 
|             DebugEx.Log("PlayWithEulerAngle: " + 1044);  | 
|         }  | 
|         _controller.startTime = 0;  | 
|         _controller.effectId = id;  | 
|         _controller.transform.position = position;  | 
|         _controller.transform.eulerAngles = eulerAngle;  | 
|         _controller.transform.localScale = Vector3.one * scale;  | 
|         _controller.gameObject.SetActive(true);  | 
|   | 
|         var config = EffectConfig.Get(id);  | 
|         if (config != null)  | 
|         {  | 
|             SoundPlayer.Instance.PlayUIAudio(config.audio);  | 
|         }  | 
|   | 
|         return _controller;  | 
|     }  | 
|   | 
|     public SFXController Request(int id)  | 
|     {  | 
|         ObjectPool<SFXController> _pool = null;  | 
|   | 
|         if (m_PoolDict.TryGetValue(id, out _pool) == false)  | 
|         {  | 
|             _pool = new ObjectPool<SFXController>(OnGetSFX, OnReleaseSFX);  | 
|             m_PoolDict.Add(id, _pool);  | 
|         }  | 
|   | 
|         SFXController _controller = null;  | 
|   | 
|         if (_pool.inactivedCount == 0)  | 
|         {  | 
|             _controller = Create(id);  | 
|         }  | 
|         else  | 
|         {  | 
|             _controller = _pool.Get();  | 
|         }  | 
|   | 
|         if (_controller != null)  | 
|         {  | 
|             _controller.transform.SetParent(null);  | 
|         }  | 
|   | 
|         return _controller;  | 
|     }  | 
|   | 
|     public void Release(SFXController sfx)  | 
|     {  | 
|         if (sfx == null)  | 
|         {  | 
|             return;  | 
|         }  | 
|   | 
|         ObjectPool<SFXController> _pool = null;  | 
|   | 
|         if (m_PoolDict.TryGetValue(sfx.effectId, out _pool) == false)  | 
|         {  | 
|             _pool = new ObjectPool<SFXController>(OnGetSFX, OnReleaseSFX);  | 
|             m_PoolDict.Add(sfx.effectId, _pool);  | 
|         }  | 
|   | 
|         sfx.SetAnimatorSpeed(1);  | 
|         sfx.SetPaticleSystemSpeed(1);  | 
|         _pool.Release(sfx);  | 
|     }  | 
|   | 
|     public void ReleaseAndDestroy(SFXController sfx)  | 
|     {  | 
|         if (sfx == null)  | 
|         {  | 
|             return;  | 
|         }  | 
|   | 
|         ObjectPool<SFXController> _pool = null;  | 
|   | 
|         if (m_PoolDict.TryGetValue(sfx.effectId, out _pool))  | 
|         {  | 
|             _pool.Clear();  | 
|         }  | 
|   | 
|   | 
|     }  | 
|   | 
|     private SFXController Create(int id)  | 
|     {  | 
|         GameObject _prefab = InstanceResourcesLoader.LoadEffect(id);  | 
|   | 
|         if (_prefab == null)  | 
|         {  | 
|             return null;  | 
|         }  | 
|   | 
|         GameObject _sfx = GameObjectPoolManager.Instance.RequestGameObject(_prefab);  | 
|   | 
|         SFXController _controller = _sfx.AddMissingComponent<SFXController>();  | 
|         _controller.config = EffectConfig.Get(id);  | 
|   | 
|         return _controller;  | 
|     }  | 
|   | 
|     private void OnGetSFX(SFXController controller)  | 
|     {  | 
|         controller.enabled = true;  | 
|         controller.SetActive(true);  | 
|         controller.DelayFrameCount = 0;  | 
|         controller.startTime = 0;  | 
|     }  | 
|   | 
|     private void OnReleaseSFX(SFXController controller)  | 
|     {  | 
|         controller.SetActive(false);  | 
|         controller.DelayFrameCount = 0;  | 
|         controller.transform.SetParent(transform);  | 
|         controller.transform.position = Constants.Special_Hide_Position;  | 
|         controller.enabled = false;  | 
|     }  | 
|   | 
|     public void Unitialize()  | 
|     {  | 
|         //foreach (var _pool in m_PoolDict.Values)  | 
|         //{  | 
|         //    _pool.Clear();  | 
|         //}  | 
|   | 
|         //m_PoolDict.Clear();  | 
|     }  | 
|   | 
| }  |