using UniGLTF.SpringBoneJobs; using UnityEngine; using UnityEngine.Profiling; namespace VRM.SpringBoneJobs { /// /// シングルトン。FastSpringBone の ServiceLocator 兼 EntryPoint。 /// シーンすべてのVRMのSpringBoneをバッチングしてまとめて実行する。 /// [DefaultExecutionOrder(11000)] public class FastSpringBoneService : MonoBehaviour { private CustomSampler _updateSampler = CustomSampler.Create("FastSpringBone(Update)"); public FastSpringBoneBufferCombiner BufferCombiner { get; private set; } private FastSpringBoneScheduler _fastSpringBoneScheduler; private static FastSpringBoneService _instance; public static FastSpringBoneService Instance { get { if (_instance) return _instance; #if UNITY_2022_3_OR_NEWER _instance = FindFirstObjectByType(); #else _instance = FindObjectOfType(); #endif if (_instance) return _instance; var gameObject = new GameObject("FastSpringBone Service"); DontDestroyOnLoad(gameObject); _instance = gameObject.AddComponent(); return _instance; } } /// /// 専有しているインスタンスを破棄する /// public static void Free() { Destroy(_instance.gameObject); _instance = null; } private void OnEnable() { BufferCombiner = new FastSpringBoneBufferCombiner(); _fastSpringBoneScheduler = new FastSpringBoneScheduler(BufferCombiner); } private void OnDisable() { BufferCombiner.Dispose(); _fastSpringBoneScheduler.Dispose(); } private void LateUpdate() { _updateSampler.Begin(); _fastSpringBoneScheduler.Schedule(Time.deltaTime).Complete(); _updateSampler.End(); } } }