mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-13 22:09:53 -05:00
Unity 2023.1以降のバージョンでUnityEngine.Object.FindObjectsOfType系APIがObsoleteになり、次の警告が発生していました。 ``` Assets/VRM10_Samples/VRM10Viewer/VRM10ViewerUI.cs(250,24): warning CS0618: 'Object.FindObjectOfType<T>()' is obsolete: 'Object.FindObjectOfType has been deprecated. Use Object.FindFirstObjectByType instead or if finding any instance is acceptable the faster Object.FindAnyObjectByType' Assets/VRM10_Samples/VRM10Viewer/VRM10ViewerUI.cs(208,31): warning CS0618: 'Object.FindObjectsOfType<T>()' is obsolete: 'Object.FindObjectsOfType has been deprecated. Use Object.FindObjectsByType instead which lets you decide whether you need the results sorted or not. FindObjectsOfType sorts the results by InstanceID but if you do not need this using FindObjectSortMode.None is considerably faster.' ``` 代わりにFindObjectsBy系APIを使うようにしました。これはUnity 2021.3にも存在しているので、そのまま使うことができました。 FindObjectOfType()とFindFirstObjectByType()の違いに関してはドキュメントからは読み取れませんでしたが、 Unity-Technologiesgが公開しているUnity 6000のソースコードを見る限り、動作は同一に見えるためそのまま置き換えました。 https://github.com/Unity-Technologies/UnityCsReference/blob/6000.0/Runtime/Export/Scripting/UnityEngineObject.bindings.cs#L586-L602
102 lines
3.0 KiB
C#
102 lines
3.0 KiB
C#
using UnityEngine;
|
|
|
|
namespace UniVRM10.FastSpringBones.System
|
|
{
|
|
[DefaultExecutionOrder(11010)]
|
|
/// <summary>
|
|
/// VRM-1.0 ではコンポーネントの処理順が規定されている
|
|
///
|
|
/// 1. ヒューマノイドボーンを解決
|
|
/// 2. 頭の位置が決まるのでLookAtを解決
|
|
/// 3. ExpressionUpdate
|
|
/// 4. コンストレイントを解決
|
|
/// 5. SpringBoneを解決
|
|
///
|
|
/// 1~4 は Vrm10Runtime が管理し LateUpdate で処理される。
|
|
/// このクラスは DefaultExecutionOrder(11000) によりその後ろにまわる。
|
|
///
|
|
/// # [Manual update]
|
|
///
|
|
/// foreach(var vrmInstance in allVrm)
|
|
/// {
|
|
/// vrmInstance.UpdateType = None;
|
|
/// vrmInstance.Runtime.Process();
|
|
/// }
|
|
/// FastSpringBoneService.Instance.UpdateType = Manual;
|
|
/// FastSpringBoneService.Instance.ManualUpdate();
|
|
///
|
|
/// </summary>
|
|
public sealed class FastSpringBoneService : MonoBehaviour
|
|
{
|
|
public enum UpdateTypes
|
|
{
|
|
Manual,
|
|
LateUpdate,
|
|
}
|
|
|
|
[SerializeField, Header("Runtime")]
|
|
public UpdateTypes UpdateType = UpdateTypes.LateUpdate;
|
|
|
|
|
|
public FastSpringBoneBufferCombiner BufferCombiner { get; private set; }
|
|
private FastSpringBoneScheduler _fastSpringBoneScheduler;
|
|
|
|
private static FastSpringBoneService _instance;
|
|
|
|
public static FastSpringBoneService Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance) return _instance;
|
|
|
|
_instance = FindFirstObjectByType<FastSpringBoneService>();
|
|
if (_instance) return _instance;
|
|
|
|
var gameObject = new GameObject("FastSpringBone Service");
|
|
DontDestroyOnLoad(gameObject);
|
|
_instance = gameObject.AddComponent<FastSpringBoneService>();
|
|
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 専有しているインスタンスを破棄する
|
|
/// </summary>
|
|
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()
|
|
{
|
|
if (UpdateType == UpdateTypes.LateUpdate)
|
|
{
|
|
_fastSpringBoneScheduler.Schedule(Time.deltaTime).Complete();
|
|
}
|
|
}
|
|
|
|
public void ManualUpdate(float deltaTime)
|
|
{
|
|
if (UpdateType != UpdateTypes.Manual)
|
|
{
|
|
throw new global::System.ArgumentException("require UpdateTypes.Manual");
|
|
}
|
|
_fastSpringBoneScheduler.Schedule(deltaTime).Complete();
|
|
}
|
|
}
|
|
}
|