Files
Convention-Unity-Demo/Assets/Scripts/Interaction/Effect/ParticleEffect.cs
2025-12-12 15:19:10 +08:00

84 lines
2.3 KiB
C#

using Convention;
using Demo.Attr;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Demo.Game
{
[Scriptable]
public class ParticleEffect : IEffectHookObject, IAssetBundleLoader
{
public static ParticleEffect Make()
{
return new GameObject().AddComponent<ParticleEffect>();
}
private Dictionary<string,bool> AssetBundleLoaders = new();
private readonly List<string> AssetBundles = new();
private readonly List<GameObject> Prefabs = new();
protected override IEnumerator DoSomethingDuringApplyScript()
{
yield return base.DoSomethingDuringApplyScript();
while (AssetBundleLoaders.Any(x => x.Value == false))
yield return null;
}
public override IEnumerator UnloadScript()
{
yield return base.UnloadScript();
foreach (var ab in AssetBundles)
{
yield return this.UnloadAssetBundle(ab);
}
}
/// <summary>
/// 加载预制体作为子物体
/// </summary>
/// <param name="ab"></param>
/// <param name="prefab"></param>
[Convention.RScript.Variable.Attr.Method]
public void Load(string ab, string prefab)
{
AssetBundleLoaders.TryAdd(ab, false);
ConventionUtility.StartCoroutine(this.LoadAssetBundle(ab, x =>
{
GameObject sub = Instantiate(x.LoadAsset<GameObject>(prefab));
sub.SetActive(false);
Prefabs.Add(sub);
sub.transform.SetParent(transform);
sub.transform.localPosition = Vector3.zero;
AssetBundles.Add(ab);
AssetBundleLoaders[ab] = true;
}));
}
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);
}
}
}
}