CubismMouthController.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.MouthMovement
  10. {
  11. /// <summary>
  12. /// Controls <see cref="CubismMouthParameter"/>s.
  13. /// </summary>
  14. public sealed class CubismMouthController : MonoBehaviour, ICubismUpdatable
  15. {
  16. /// <summary>
  17. /// The blend mode.
  18. /// </summary>
  19. [SerializeField]
  20. public CubismParameterBlendMode BlendMode = CubismParameterBlendMode.Multiply;
  21. /// <summary>
  22. /// The opening of the mouth.
  23. /// </summary>
  24. [SerializeField, Range(0f, 1f)]
  25. public float MouthOpening = 1f;
  26. /// <summary>
  27. /// Mouth parameters.
  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="CubismMouthParameter"/>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<CubismMouthParameter>();
  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.CubismMouthController; }
  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. /// <remarks>
  76. /// Make sure this method is called after any animations are evaluated.
  77. /// </remarks>
  78. public void OnLateUpdate()
  79. {
  80. // Fail silently.
  81. if (!enabled || Destinations == null)
  82. {
  83. return;
  84. }
  85. // Apply value.
  86. Destinations.BlendToValue(BlendMode, MouthOpening);
  87. }
  88. #region Unity Events Handling
  89. /// <summary>
  90. /// Called by Unity. Makes sure cache is initialized.
  91. /// </summary>
  92. private void Start()
  93. {
  94. // Initialize cache.
  95. Refresh();
  96. }
  97. /// <summary>
  98. /// Called by Unity.
  99. /// </summary>
  100. private void LateUpdate()
  101. {
  102. if(!HasUpdateController)
  103. {
  104. OnLateUpdate();
  105. }
  106. }
  107. #endregion
  108. }
  109. }