CubismRepeatController.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.ParameretRepeat
  10. {
  11. public class CubismRepeatController : MonoBehaviour, ICubismUpdatable
  12. {
  13. #region Variable
  14. /// <summary>
  15. /// Model has cubism update controller component.
  16. /// </summary>
  17. [HideInInspector]
  18. public bool HasUpdateController { get; set; }
  19. /// <summary>
  20. /// Destinations.
  21. /// </summary>
  22. private CubismParameter[] Destinations { get; set; }
  23. #endregion
  24. #region Function
  25. /// <summary>
  26. /// Refreshes the controller. Call this method after adding and/or removing <see cref="CubismFadeParameter"/>s.
  27. /// </summary>
  28. public void Refresh()
  29. {
  30. var model = GetComponent<CubismModel>();
  31. if (model == null)
  32. {
  33. return;
  34. }
  35. Destinations = model.Parameters;
  36. // Get cubism update controller.
  37. HasUpdateController = (GetComponent<CubismUpdateController>() != null);
  38. }
  39. /// <summary>
  40. /// Called by cubism update controller. Order to invoke OnLateUpdate.
  41. /// </summary>
  42. public int ExecutionOrder
  43. {
  44. get { return CubismUpdateExecutionOrder.CubismRepeatController; }
  45. }
  46. /// <summary>
  47. /// Called by cubism update controller. Needs to invoke OnLateUpdate on Editing.
  48. /// </summary>
  49. public bool NeedsUpdateOnEditing
  50. {
  51. get { return false; }
  52. }
  53. /// <summary>
  54. /// Called by cubism update controller. Updates controller.
  55. /// </summary>
  56. /// <remarks>
  57. /// Make sure this method is called after any animations are evaluated.
  58. /// </remarks>
  59. public void OnLateUpdate()
  60. {
  61. // Fail silently.
  62. if (!enabled || Destinations == null)
  63. {
  64. return;
  65. }
  66. for (var i = 0; i < Destinations.Length; i++)
  67. {
  68. var parameter = Destinations[i];
  69. if (parameter.IsRepeat())
  70. {
  71. parameter.Value = parameter.GetParameterRepeatValue(parameter.Value);
  72. }
  73. else
  74. {
  75. parameter.Value = parameter.GetParameterClampValue(parameter.Value);
  76. }
  77. }
  78. }
  79. #endregion
  80. #region Unity Events Handling
  81. /// <summary>
  82. /// Initializes instance.
  83. /// </summary>
  84. private void OnEnable()
  85. {
  86. // Initialize cache.
  87. Refresh();
  88. }
  89. /// <summary>
  90. /// Called by Unity.
  91. /// </summary>
  92. private void LateUpdate()
  93. {
  94. if (!HasUpdateController)
  95. {
  96. OnLateUpdate();
  97. }
  98. }
  99. #endregion
  100. }
  101. }