using Convention; using Demo.Game.Attr; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; namespace Demo.Game { namespace ConfigType { public class ParticleEffectConfig : IEffectHookObjectConfig { public Dictionary AssetBundles; public override void Deserialize(BinaryReader reader) { int count = BinarySerializeUtility.ReadInt(reader); while(count-->0) { var temp = BinarySerializeUtility.DeserializeStringArray(reader); var key = temp[0]; var value = temp[1..]; AssetBundles.Add(key, value); } base.Deserialize(reader); } public override void Serialize(BinaryWriter writer) { BinarySerializeUtility.WriteInt(writer, AssetBundles.Count); foreach (var (key, value) in AssetBundles) { string[] temp = new string[value.Length + 1]; temp[0] = key; Array.Copy(value, 0, temp, 1, value.Length); BinarySerializeUtility.SerializeArray(writer, value.ToArray()); } base.Serialize(writer); } } } [Scriptable] public class ParticleEffect : IEffectHookObject, IAssetBundleLoader { public static ParticleEffect Make() { return new GameObject().AddComponent(); } private int AssetBundleLoadingCounter = 0; private readonly Dictionary> AssetBundles = new(); private readonly List Prefabs = new(); protected override IEnumerator DoSomethingDuringApplyScript() { yield return base.DoSomethingDuringApplyScript(); yield return new WaitUntil(() => AssetBundleLoadingCounter == 0); } public override IEnumerator UnloadScript() { yield return base.UnloadScript(); foreach (var ab in AssetBundles) { yield return this.UnloadAssetBundle(ab.Key); } } /// /// 加载预制体作为子物体 /// /// /// [Convention.RScript.Variable.Attr.Method] public void Load(string ab, string prefab) { AssetBundleLoadingCounter++; ConventionUtility.StartCoroutine(this.LoadAssetBundle(ab, x => { GameObject sub = Instantiate(x.LoadAsset(prefab)); sub.SetActive(false); Prefabs.Add(sub); sub.transform.SetParent(transform); sub.transform.localPosition = Vector3.zero; if(AssetBundles.ContainsKey(ab)==false) AssetBundles.Add(ab, new()); AssetBundles[ab].Add(prefab); AssetBundleLoadingCounter--; })); } public override void OnInit() { foreach (var child in Prefabs) { child.SetActive(false); } } public override void OnBegin() { foreach (var child in Prefabs) { child.SetActive(true); } } public override void OnEnd() { foreach (var child in Prefabs) { child.SetActive(false); } } } }