BITKit/Src/Unity/Scripts/VFXManager/VFXService.cs

92 lines
2.9 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System.Collections;
2024-04-16 04:06:46 +08:00
using System.Collections.Concurrent;
2023-06-05 19:57:17 +08:00
using System.Collections.Generic;
2023-11-06 01:17:23 +08:00
using System.Linq;
2024-03-31 23:31:00 +08:00
using System.Text;
2023-06-05 19:57:17 +08:00
using UnityEngine;
using BITKit;
using UnityEngine.Events;
using UnityEngine.Pool;
2023-11-15 23:55:06 +08:00
using UnityEngine.UIElements;
2023-06-05 19:57:17 +08:00
namespace BITKit
{
public struct VFXMessage
{
public string path;
public Location location;
}
public class VFXService : MonoBehaviour, IObjectMatcher<string, Transform>
{
2023-09-01 14:35:05 +08:00
private static VFXService sinleton;
2023-06-05 19:57:17 +08:00
public VFX[] vfxs;
2023-09-01 14:35:05 +08:00
private readonly Dictionary<string, UnityPool<Transform>> pools = new();
2024-04-16 04:06:46 +08:00
private readonly ConcurrentDictionary<int, Transform> cache = new();
2024-03-31 23:31:00 +08:00
[SerializeField] private bool debug;
2023-11-15 23:55:06 +08:00
[SerializeField] private Optional<int> defaultCapacity;
2023-09-01 14:35:05 +08:00
private void Awake()
2023-06-05 19:57:17 +08:00
{
2024-04-16 04:06:46 +08:00
cache.Clear();
2023-06-05 19:57:17 +08:00
sinleton = this;
DI.Register(this);
}
2023-11-06 01:17:23 +08:00
public Transform Spawn(RaycastHit hit, params string[] keyWords)
2023-06-05 19:57:17 +08:00
{
2023-11-06 01:17:23 +08:00
return Spawn(new Location()
2023-06-05 19:57:17 +08:00
{
2023-11-06 01:17:23 +08:00
position = hit.point,
rotation = Quaternion.LookRotation(hit.normal),
forward = hit.normal,
}, keyWords);
2023-06-05 19:57:17 +08:00
}
public Transform Spawn(Location location, params string[] keyWords)
{
2024-03-31 23:31:00 +08:00
StringBuilder reportBuilder = debug ? new() : null;
reportBuilder?.AppendLine(JsonHelper.Get(keyWords));
2023-06-05 19:57:17 +08:00
if (TryMatch(out var prefab, keyWords))
{
2024-03-31 23:31:00 +08:00
2023-06-05 19:57:17 +08:00
var pool = pools.Get(prefab.name);
2023-11-15 23:55:06 +08:00
if (defaultCapacity.Allow)
pool.DefaultCapacity = defaultCapacity.Value;
2023-10-24 23:38:22 +08:00
var instance = pool.Get(prefab, transform);
2023-06-05 19:57:17 +08:00
instance.SetPositionAndRotation(location, location);
2023-10-24 23:38:22 +08:00
if (location.forward.sqrMagnitude is not 0)
instance.forward = location.forward;
2024-03-31 23:31:00 +08:00
reportBuilder?.AppendLine($"生成:{prefab.name}");
if(reportBuilder is not null)
BIT4Log.Log<VFXService>(reportBuilder.ToString());
2023-06-05 19:57:17 +08:00
return instance;
}
else
{
BIT4Log.Log<VFXService>($"未找到:{JsonHelper.Get(keyWords)}");
return null;
}
}
public bool TryMatch(out Transform value, params string[] key)
{
2024-04-16 04:06:46 +08:00
value=cache.GetOrAdd(MathE.GetHash(key),Add);
return value is not null;
Transform Add(int arg)
2023-06-05 19:57:17 +08:00
{
2024-04-16 04:06:46 +08:00
foreach (var vfx in vfxs)
2023-06-05 19:57:17 +08:00
{
2024-04-16 04:06:46 +08:00
if (vfx.IsMatch(key))
{
var x = vfx.prefab;
return x;
}
2023-06-05 19:57:17 +08:00
}
2024-04-16 04:06:46 +08:00
return null;
2023-06-05 19:57:17 +08:00
}
}
2024-04-16 04:06:46 +08:00
2023-06-05 19:57:17 +08:00
}
}