//加入靜態特效至設定 public void AddStaticEffect(string effectName, GameObject prefab, Transform parent) { Canvas parentCanvas = parent.GetComponentInParent <Canvas>(); //建立新的靜態特效設定 StaticParticleObject _sta = new StaticParticleObject(effectName, parentCanvas.sortingOrder, prefab, parent); for (int i = 0; i < staticEffectSetting.Count; i++) { if (staticEffectSetting[i].effectName == effectName) //若已經存在相同名稱的設定 { staticEffectSetting[i] = _sta; //取代原有設定 return; } } staticEffectSetting.Add(_sta); //追加至設定列表 }
//設定靜態特效 public void SetStaticEffect(string effectName, bool onOff) { StaticParticleObject _sta = new StaticParticleObject(); int index = 0; //指定特效索引 //取得指定名稱的特效 for (int i = 0; i < staticEffectSetting.Count; i++) { if (staticEffectSetting[i].effectName == effectName) { _sta = staticEffectSetting[i]; index = i; break; } if (i == staticEffectSetting.Count - 1) { return; //搜尋不到結果時, 直接結束程序 } } if (_sta.effectGo == null) { return; //未設定物件參考時, 直接結束程序 } foreach (ParticleSystemRenderer renderer in _sta.effectGo.GetComponentsInChildren <ParticleSystemRenderer>()) //設定顯示層級 { renderer.sortingOrder = _sta.sortingOrder; } //開關特效Lambda方法 System.Action <bool, ParticleSystem> SetEffectState = (bool b, ParticleSystem p) => { if (b) //開啟特效 { var parMain = p.main; parMain.loop = true; p.Play(); } else //關閉特效 { var parMain = p.main; parMain.loop = false; } }; //特效行為邏輯實現 ParticleSystem ps; if (!_sta.effectGo.activeInHierarchy) //物件不存在在編輯器上 { GameObject insGo = Instantiate(_sta.effectGo, _sta.parentHolder); //創建物件 _sta.ReplacePrefab(insGo); //以實例物件取代預置體 staticEffectSetting[index] = _sta; //更新設定 ps = insGo.GetComponent <ParticleSystem>(); SetEffectState(onOff, ps); } else //正常執行開啟程序 { ps = _sta.effectGo.GetComponent <ParticleSystem>(); SetEffectState(onOff, ps); } }