Files
Convention-Unity-Demo/Assets/Scripts/Interaction/Effect/ParticleEffect.cs

120 lines
3.7 KiB
C#
Raw Normal View History

2025-12-12 15:19:10 +08:00
using Convention;
2025-12-15 17:20:55 +08:00
using Demo.Game.Attr;
2025-12-16 17:54:51 +08:00
using System;
2025-09-25 19:04:05 +08:00
using System.Collections;
using System.Collections.Generic;
2025-12-16 17:54:51 +08:00
using System.IO;
using System.Linq;
2025-09-25 19:04:05 +08:00
using UnityEngine;
namespace Demo.Game
{
2025-12-16 17:54:51 +08:00
namespace ConfigType
{
public class ParticleEffectConfig : IEffectHookObjectConfig
{
public Dictionary<string, string[]> 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);
}
}
}
2025-12-12 15:19:10 +08:00
[Scriptable]
2025-09-25 19:04:05 +08:00
public class ParticleEffect : IEffectHookObject, IAssetBundleLoader
{
public static ParticleEffect Make()
{
return new GameObject().AddComponent<ParticleEffect>();
}
2025-12-16 17:54:51 +08:00
private int AssetBundleLoadingCounter = 0;
private readonly Dictionary<string,List<string>> AssetBundles = new();
private readonly List<GameObject> Prefabs = new();
2025-09-25 19:04:05 +08:00
protected override IEnumerator DoSomethingDuringApplyScript()
{
yield return base.DoSomethingDuringApplyScript();
2025-12-16 17:54:51 +08:00
yield return new WaitUntil(() => AssetBundleLoadingCounter == 0);
}
2025-09-25 19:04:05 +08:00
public override IEnumerator UnloadScript()
{
yield return base.UnloadScript();
foreach (var ab in AssetBundles)
{
2025-12-16 17:54:51 +08:00
yield return this.UnloadAssetBundle(ab.Key);
2025-09-25 19:04:05 +08:00
}
}
/// <summary>
/// 加载预制体作为子物体
/// </summary>
/// <param name="ab"></param>
/// <param name="prefab"></param>
[Convention.RScript.Variable.Attr.Method]
public void Load(string ab, string prefab)
2025-09-25 19:04:05 +08:00
{
2025-12-16 17:54:51 +08:00
AssetBundleLoadingCounter++;
ConventionUtility.StartCoroutine(this.LoadAssetBundle(ab, x =>
2025-09-25 19:04:05 +08:00
{
GameObject sub = Instantiate(x.LoadAsset<GameObject>(prefab));
sub.SetActive(false);
Prefabs.Add(sub);
sub.transform.SetParent(transform);
sub.transform.localPosition = Vector3.zero;
2025-12-16 17:54:51 +08:00
if(AssetBundles.ContainsKey(ab)==false)
AssetBundles.Add(ab, new());
AssetBundles[ab].Add(prefab);
AssetBundleLoadingCounter--;
}));
2025-09-25 19:04:05 +08:00
}
2025-10-08 00:35:50 +08:00
public override void OnInit()
{
foreach (var child in Prefabs)
{
child.SetActive(false);
}
}
2025-09-25 19:04:05 +08:00
public override void OnBegin()
{
foreach (var child in Prefabs)
{
child.SetActive(true);
}
}
public override void OnEnd()
{
foreach (var child in Prefabs)
{
child.SetActive(false);
}
}
}
}