CubismPart.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.Unmanaged;
  8. using Live2D.Cubism.Framework;
  9. using UnityEngine;
  10. namespace Live2D.Cubism.Core
  11. {
  12. /// <summary>
  13. /// Single <see cref="CubismModel"/> part.
  14. /// </summary>
  15. [CubismDontMoveOnReimport]
  16. public sealed class CubismPart : MonoBehaviour
  17. {
  18. #region Factory Methods
  19. /// <summary>
  20. /// Creates parts for a <see cref="CubismModel"/>.
  21. /// </summary>
  22. /// <param name="unmanagedModel">Handle to unmanaged model.</param>
  23. /// <returns>Parts root.</returns>
  24. internal static GameObject CreateParts(CubismUnmanagedModel unmanagedModel)
  25. {
  26. var root = new GameObject("Parts");
  27. // Create parts.
  28. var unmanagedParts = unmanagedModel.Parts;
  29. var buffer = new CubismPart[unmanagedParts.Count];
  30. for (var i = 0; i < buffer.Length; ++i)
  31. {
  32. var proxy = new GameObject();
  33. buffer[i] = proxy.AddComponent<CubismPart>();
  34. buffer[i].transform.SetParent(root.transform);
  35. buffer[i].Reset(unmanagedModel, i);
  36. }
  37. return root;
  38. }
  39. #endregion
  40. /// <summary>
  41. /// Unmanaged parts from unmanaged model.
  42. /// </summary>
  43. private CubismUnmanagedParts UnmanagedParts { get; set; }
  44. /// <summary>
  45. /// <see cref="UnmanagedIndex"/> backing field.
  46. /// </summary>
  47. [SerializeField, HideInInspector]
  48. private int _unmanagedIndex = -1;
  49. /// <summary>
  50. /// Position in unmanaged arrays.
  51. /// </summary>
  52. internal int UnmanagedIndex
  53. {
  54. get { return _unmanagedIndex; }
  55. private set { _unmanagedIndex = value; }
  56. }
  57. /// <summary>
  58. /// Copy of Id.
  59. /// </summary>
  60. public string Id
  61. {
  62. get
  63. {
  64. // Pull data.
  65. return UnmanagedParts.Ids[UnmanagedIndex];
  66. }
  67. }
  68. /// <summary>
  69. /// Current opacity.
  70. /// </summary>
  71. [SerializeField, HideInInspector]
  72. public float Opacity;
  73. /// <summary>
  74. /// Revives instance.
  75. /// </summary>
  76. /// <param name="unmanagedModel">TaskableModel to unmanaged unmanagedModel.</param>
  77. internal void Revive(CubismUnmanagedModel unmanagedModel)
  78. {
  79. UnmanagedParts = unmanagedModel.Parts;
  80. }
  81. /// <summary>
  82. /// Restores instance to initial state.
  83. /// </summary>
  84. /// <param name="unmanagedModel">TaskableModel to unmanaged unmanagedModel.</param>
  85. /// <param name="unmanagedIndex">Position in unmanaged arrays.</param>
  86. private void Reset(CubismUnmanagedModel unmanagedModel, int unmanagedIndex)
  87. {
  88. Revive(unmanagedModel);
  89. UnmanagedIndex = unmanagedIndex;
  90. name = Id;
  91. Opacity = UnmanagedParts.Opacities[unmanagedIndex];
  92. }
  93. }
  94. }