CubismEyeBlinkController.cs 3.4 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
  10. {
  11. /// <summary>
  12. /// <see cref="CubismEyeBlinkParameter"/> controller.
  13. /// </summary>
  14. public sealed class CubismEyeBlinkController : MonoBehaviour, ICubismUpdatable
  15. {
  16. /// <summary>
  17. /// Blend mode.
  18. /// </summary>
  19. [SerializeField]
  20. public CubismParameterBlendMode BlendMode = CubismParameterBlendMode.Multiply;
  21. /// <summary>
  22. /// Opening of the eyes.
  23. /// </summary>
  24. [SerializeField, Range(0f, 1f)]
  25. public float EyeOpening = 1f;
  26. /// <summary>
  27. /// Eye blink parameters cache.
  28. /// </summary>
  29. private CubismParameter[] Destinations { get; set; }
  30. /// <summary>
  31. /// Model has update controller component.
  32. /// </summary>
  33. [HideInInspector]
  34. public bool HasUpdateController { get; set; }
  35. /// <summary>
  36. /// Refreshes controller. Call this method after adding and/or removing <see cref="CubismEyeBlinkParameter"/>s.
  37. /// </summary>
  38. public void Refresh()
  39. {
  40. var model = this.FindCubismModel();
  41. // Fail silently...
  42. if (model == null)
  43. {
  44. return;
  45. }
  46. // Cache destinations.
  47. var tags = model
  48. .Parameters
  49. .GetComponentsMany<CubismEyeBlinkParameter>();
  50. Destinations = new CubismParameter[tags.Length];
  51. for (var i = 0; i < tags.Length; ++i)
  52. {
  53. Destinations[i] = tags[i].GetComponent<CubismParameter>();
  54. }
  55. // Get cubism update controller.
  56. HasUpdateController = (GetComponent<CubismUpdateController>() != null);
  57. }
  58. /// <summary>
  59. /// Called by cubism update controller. Order to invoke OnLateUpdate.
  60. /// </summary>
  61. public int ExecutionOrder
  62. {
  63. get { return CubismUpdateExecutionOrder.CubismEyeBlinkController; }
  64. }
  65. /// <summary>
  66. /// Called by cubism update controller. Needs to invoke OnLateUpdate on Editing.
  67. /// </summary>
  68. public bool NeedsUpdateOnEditing
  69. {
  70. get { return false; }
  71. }
  72. /// <summary>
  73. /// Called by cubism update controller. Updates controller.
  74. /// </summary>
  75. public void OnLateUpdate()
  76. {
  77. // Fail silently.
  78. if (!enabled || Destinations == null)
  79. {
  80. return;
  81. }
  82. // Apply value.
  83. Destinations.BlendToValue(BlendMode, EyeOpening);
  84. }
  85. #region Unity Event Handling
  86. /// <summary>
  87. /// Called by Unity. Makes sure cache is initialized.
  88. /// </summary>
  89. private void Start()
  90. {
  91. // Initialize cache.
  92. Refresh();
  93. }
  94. /// <summary>
  95. /// Called by Unity.
  96. /// </summary>
  97. private void LateUpdate()
  98. {
  99. if(!HasUpdateController)
  100. {
  101. OnLateUpdate();
  102. }
  103. }
  104. #endregion
  105. }
  106. }