CubismParameterStore.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /// Cubism parameter store.
  13. /// </summary>
  14. public class CubismParameterStore : MonoBehaviour, ICubismUpdatable
  15. {
  16. /// <summary>
  17. /// Parameters cache.
  18. /// </summary>
  19. private CubismParameter[] DestinationParameters { get; set; }
  20. /// <summary>
  21. /// Parts cache.
  22. /// </summary>
  23. private CubismPart[] DestinationParts { get; set; }
  24. /// <summary>
  25. /// For storage parameters value.
  26. /// </summary>
  27. private float[] _parameterValues;
  28. /// <summary>
  29. /// For storage parts opacity.
  30. /// </summary>
  31. private float[] _partOpacities;
  32. /// <summary>
  33. /// Model has cubism update controller component.
  34. /// </summary>
  35. [HideInInspector]
  36. public bool HasUpdateController { get; set; }
  37. /// <summary>
  38. /// Called by cubism update controller. Order to invoke OnLateUpdate.
  39. /// </summary>
  40. public int ExecutionOrder
  41. {
  42. get { return CubismUpdateExecutionOrder.CubismParameterStoreSaveParameters; }
  43. }
  44. /// <summary>
  45. /// Called by cubism update controller. Needs to invoke OnLateUpdate on Editing.
  46. /// </summary>
  47. public bool NeedsUpdateOnEditing
  48. {
  49. get { return false; }
  50. }
  51. public void Refresh()
  52. {
  53. if (DestinationParameters == null)
  54. {
  55. DestinationParameters = this.FindCubismModel().Parameters;
  56. }
  57. if (DestinationParts == null)
  58. {
  59. DestinationParts = this.FindCubismModel().Parts;
  60. }
  61. // Get cubism update controller.
  62. HasUpdateController = (GetComponent<CubismUpdateController>() != null);
  63. SaveParameters();
  64. }
  65. /// <summary>
  66. /// Called by cubism update controller. Updates controller.
  67. /// </summary>
  68. /// <remarks>
  69. /// Make sure this method is called after any animations are evaluated.
  70. /// </remarks>
  71. public void OnLateUpdate()
  72. {
  73. if (!HasUpdateController)
  74. {
  75. return;
  76. }
  77. SaveParameters();
  78. }
  79. /// <summary>
  80. /// Save model parameters value and parts opacity.
  81. /// </summary>
  82. public void SaveParameters()
  83. {
  84. // Fail silently...
  85. if(!enabled)
  86. {
  87. return;
  88. }
  89. // save parameters value
  90. if(DestinationParameters != null && _parameterValues == null)
  91. {
  92. _parameterValues = new float[DestinationParameters.Length];
  93. }
  94. if(_parameterValues != null)
  95. {
  96. for(var i = 0; i < _parameterValues.Length; ++i)
  97. {
  98. _parameterValues[i] = DestinationParameters[i].Value;
  99. }
  100. }
  101. // save parts opacity
  102. if(DestinationParts != null && _partOpacities == null)
  103. {
  104. _partOpacities = new float[DestinationParts.Length];
  105. }
  106. if(_partOpacities != null)
  107. {
  108. for(var i = 0; i < _partOpacities.Length; ++i)
  109. {
  110. _partOpacities[i] = DestinationParts[i].Opacity;
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// Restore model parameters value and parts opacity.
  116. /// </summary>
  117. public void RestoreParameters()
  118. {
  119. // Fail silently...
  120. if(!enabled)
  121. {
  122. return;
  123. }
  124. // restore parameters value
  125. if(_parameterValues != null)
  126. {
  127. for(var i = 0; i < _parameterValues.Length; ++i)
  128. {
  129. DestinationParameters[i].Value = _parameterValues[i];
  130. }
  131. }
  132. // restore parts opacity
  133. if(_partOpacities != null)
  134. {
  135. for(var i = 0; i < _partOpacities.Length; ++i)
  136. {
  137. DestinationParts[i].Opacity = _partOpacities[i];
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// Called by Unity.
  143. /// </summary>
  144. private void OnEnable()
  145. {
  146. // Initialize cache.
  147. Refresh();
  148. }
  149. }
  150. }