BITKit/Packages/Runtime/VFXManager/VFXService.cs

65 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BITKit;
using UnityEngine.Events;
using UnityEngine.Pool;
namespace BITKit
{
public struct VFXMessage
{
public string path;
public Location location;
}
[CreateOnStart(BITAppForUnity.Path.Services,nameof(VFXService))]
public class VFXService : MonoBehaviour, IObjectMatcher<string, Transform>
{
static VFXService sinleton;
public VFX[] vfxs;
Dictionary<string, UnityPool<Transform>> pools = new();
void Awake()
{
sinleton = this;
DI.Register(this);
}
public Transform Spawn(VFXMessage message)
{
if (Data.TryGetValue<Transform>(message.path, out var vfx))
{
return Instantiate(vfx, message.location, message.location);
}
return null;
}
public Transform Spawn(Location location, params string[] keyWords)
{
if (TryMatch(out var prefab, keyWords))
{
var pool = pools.Get(prefab.name);
var instance = pool.Get(prefab,transform);
instance.SetPositionAndRotation(location, location);
instance.forward = location.forward;
return instance;
}
else
{
BIT4Log.Log<VFXService>($"未找到:{JsonHelper.Get(keyWords)}");
return null;
}
}
public bool TryMatch(out Transform value, params string[] key)
{
foreach (var vfx in vfxs)
{
if (vfx.IsMatch(key))
{
value = vfx.prefab;
return true;
}
}
value = null;
return false;
}
}
}