CubismUpdateController.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 System.Collections.Generic;
  9. using UnityEngine;
  10. namespace Live2D.Cubism.Framework
  11. {
  12. [ExecuteInEditMode]
  13. public class CubismUpdateController : MonoBehaviour
  14. {
  15. /// <summary>
  16. /// The action of cubism component late update.
  17. /// </summary>
  18. private System.Action _onLateUpdate;
  19. /// <summary>
  20. /// Refresh delegate manager.
  21. /// </summary>
  22. public void Refresh()
  23. {
  24. var model = this.FindCubismModel();
  25. // Fail silently...
  26. if (model == null)
  27. {
  28. return;
  29. }
  30. // Set the null value when refreshed UpdateController to avoid duplicated registering.
  31. _onLateUpdate = null;
  32. // Set delegate.
  33. var components = model.GetComponents<ICubismUpdatable>();
  34. var sortedComponents = new List<ICubismUpdatable>(components);
  35. CubismUpdateExecutionOrder.SortByExecutionOrder(sortedComponents);
  36. foreach(var component in sortedComponents)
  37. {
  38. #if UNITY_EDITOR
  39. if (!Application.isPlaying && !component.NeedsUpdateOnEditing)
  40. {
  41. continue;
  42. }
  43. #endif
  44. _onLateUpdate += component.OnLateUpdate;
  45. }
  46. }
  47. #region Unity Event Handling
  48. /// <summary>
  49. /// Called by Unity.
  50. /// </summary>
  51. private void Start()
  52. {
  53. Refresh();
  54. }
  55. /// <summary>
  56. /// Called by Unity.
  57. /// </summary>
  58. private void LateUpdate()
  59. {
  60. // Cubism late update.
  61. if(_onLateUpdate != null)
  62. {
  63. _onLateUpdate();
  64. }
  65. }
  66. #endregion
  67. }
  68. }