CubismPhysicsController.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * Copyright(c) Live2D Inc. All rights reserved.
  3. *
  4. * Use of this source code is governed by the Live2D Open Software license
  5. * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
  6. */
  7. using Live2D.Cubism.Core;
  8. using UnityEngine;
  9. namespace Live2D.Cubism.Framework.Physics
  10. {
  11. /// <summary>
  12. /// Physics simulation controller.
  13. /// </summary>
  14. [CubismMoveOnReimportCopyComponentsOnly]
  15. public class CubismPhysicsController : MonoBehaviour, ICubismUpdatable
  16. {
  17. /// <summary>
  18. /// Simulation target rig.
  19. /// </summary>
  20. private CubismPhysicsRig Rig
  21. {
  22. get { return _rig; }
  23. set { _rig = value; }
  24. }
  25. [SerializeField]
  26. private CubismPhysicsRig _rig;
  27. /// <summary>
  28. /// Cubism model parameters for simulation.
  29. /// </summary>
  30. public CubismParameter[] Parameters { get; private set; }
  31. /// <summary>
  32. /// Model has update controller component.
  33. /// </summary>
  34. [HideInInspector]
  35. public bool HasUpdateController { get; set; }
  36. public int ExecutionOrder
  37. {
  38. get { return CubismUpdateExecutionOrder.CubismPhysicsController; }
  39. }
  40. public bool NeedsUpdateOnEditing
  41. {
  42. get { return false; }
  43. }
  44. public void OnLateUpdate()
  45. {
  46. var deltaTime = Time.deltaTime;
  47. // Use fixed delta time if required.
  48. if (CubismPhysics.UseFixedDeltaTime)
  49. {
  50. deltaTime = Time.fixedDeltaTime;
  51. }
  52. // Evaluate rig.
  53. Rig.Evaluate(deltaTime);
  54. }
  55. /// <summary>
  56. /// Sets rig and initializes <see langword="this"/>.
  57. /// </summary>
  58. /// <param name="rig"></param>
  59. public void Initialize(CubismPhysicsRig rig)
  60. {
  61. Rig = rig;
  62. Awake();
  63. }
  64. #region Unity Event Handling
  65. /// <summary>
  66. /// Called by Unity or <see cref="Initialize"/>. Initializes <see langword="this"/> if <see cref="Rig"/> exists.
  67. /// </summary>
  68. public void Awake()
  69. {
  70. // Check rig existence.
  71. if (Rig == null)
  72. {
  73. return;
  74. }
  75. // Initialize rig.
  76. Rig.Controller = this;
  77. for (var i = 0; i < Rig.SubRigs.Length; ++i)
  78. {
  79. Rig.SubRigs[i].Rig = Rig;
  80. }
  81. Parameters = this.FindCubismModel().Parameters;
  82. Rig.Initialize();
  83. }
  84. /// <summary>
  85. /// Called by Unity.
  86. /// </summary>
  87. public void Start()
  88. {
  89. // Get cubism update controller.
  90. HasUpdateController = (GetComponent<CubismUpdateController>() != null);
  91. }
  92. /// <summary>
  93. /// Called by Unity. Updates controller.
  94. /// </summary>
  95. /// <remarks>Must be call after animation update.</remarks>
  96. private void LateUpdate()
  97. {
  98. if (!HasUpdateController)
  99. {
  100. OnLateUpdate();
  101. }
  102. }
  103. #endregion
  104. }
  105. }