CubismAutoMouthInput.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 UnityEngine;
  8. namespace Live2D.Cubism.Framework.MouthMovement
  9. {
  10. /// <summary>
  11. /// Automatic mouth movement.
  12. /// </summary>
  13. public sealed class CubismAutoMouthInput : MonoBehaviour
  14. {
  15. /// <summary>
  16. /// Timescale.
  17. /// </summary>
  18. [SerializeField]
  19. public float Timescale = 10f;
  20. /// <summary>
  21. /// Target controller.
  22. /// </summary>
  23. private CubismMouthController Controller { get; set; }
  24. /// <summary>
  25. /// Current time.
  26. /// </summary>
  27. private float T { get; set; }
  28. /// <summary>
  29. /// Resets the input.
  30. /// </summary>
  31. public void Reset()
  32. {
  33. T = 0f;
  34. }
  35. #region Unity Event Handling
  36. /// <summary>
  37. /// Called by Unity. Initializes input.
  38. /// </summary>
  39. private void Start()
  40. {
  41. Controller = GetComponent<CubismMouthController>();
  42. }
  43. /// <summary>
  44. /// Called by Unity. Updates controller.
  45. /// </summary>
  46. /// <remarks>
  47. /// Make sure this method is called after any animations are evaluated.
  48. /// </remarks>
  49. private void LateUpdate()
  50. {
  51. // Fail silently.
  52. if (Controller == null)
  53. {
  54. return;
  55. }
  56. // Progress time.
  57. T += (Time.deltaTime * Timescale);
  58. // Evaluate.
  59. Controller.MouthOpening = Mathf.Abs(Mathf.Sin(T));
  60. }
  61. #endregion
  62. }
  63. }