BITFALL/Assets/BITKit/Unity/Scripts/VFXManager/VFXService.cs

72 lines
2.1 KiB
C#
Raw Normal View History

2023-06-08 14:09:50 +08:00
using System.Collections;
using System.Collections.Generic;
2023-11-15 23:54:54 +08:00
using System.Linq;
2023-06-08 14:09:50 +08:00
using UnityEngine;
using BITKit;
using UnityEngine.Events;
using UnityEngine.Pool;
2023-11-15 23:54:54 +08:00
using UnityEngine.UIElements;
2023-06-08 14:09:50 +08:00
namespace BITKit
{
public struct VFXMessage
{
public string path;
public Location location;
}
public class VFXService : MonoBehaviour, IObjectMatcher<string, Transform>
{
2023-08-27 02:58:19 +08:00
private static VFXService sinleton;
2023-06-08 14:09:50 +08:00
public VFX[] vfxs;
2023-08-27 02:58:19 +08:00
private readonly Dictionary<string, UnityPool<Transform>> pools = new();
2023-11-15 23:54:54 +08:00
[SerializeField] private Optional<int> defaultCapacity;
2023-08-27 02:58:19 +08:00
private void Awake()
2023-06-08 14:09:50 +08:00
{
sinleton = this;
DI.Register(this);
}
2023-11-15 23:54:54 +08:00
public Transform Spawn(RaycastHit hit, params string[] keyWords)
{
return Spawn(new Location()
{
position = hit.point,
rotation = Quaternion.LookRotation(hit.normal),
forward = hit.normal,
}, keyWords);
}
2023-06-08 14:09:50 +08:00
public Transform Spawn(Location location, params string[] keyWords)
{
if (TryMatch(out var prefab, keyWords))
{
var pool = pools.Get(prefab.name);
2023-11-15 23:54:54 +08:00
if (defaultCapacity.Allow)
pool.DefaultCapacity = defaultCapacity.Value;
2023-10-20 19:31:12 +08:00
var instance = pool.Get(prefab, transform);
2023-06-08 14:09:50 +08:00
instance.SetPositionAndRotation(location, location);
2023-10-20 19:31:12 +08:00
if (location.forward.sqrMagnitude is not 0)
instance.forward = location.forward;
2023-06-08 14:09:50 +08:00
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;
}
}
}