CubismPart.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. public 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. /// Parent part position in unmanaged arrays.
  75. /// </summary>
  76. public int UnmanagedParentIndex
  77. {
  78. get
  79. {
  80. if (UnmanagedIndex > 0)
  81. {
  82. // Pull data.
  83. return UnmanagedParts.ParentIndices[UnmanagedIndex];
  84. }
  85. return -1;
  86. }
  87. }
  88. /// <summary>
  89. /// Revives instance.
  90. /// </summary>
  91. /// <param name="unmanagedModel">TaskableModel to unmanaged unmanagedModel.</param>
  92. internal void Revive(CubismUnmanagedModel unmanagedModel)
  93. {
  94. UnmanagedParts = unmanagedModel.Parts;
  95. }
  96. /// <summary>
  97. /// Restores instance to initial state.
  98. /// </summary>
  99. /// <param name="unmanagedModel">TaskableModel to unmanaged unmanagedModel.</param>
  100. /// <param name="unmanagedIndex">Position in unmanaged arrays.</param>
  101. private void Reset(CubismUnmanagedModel unmanagedModel, int unmanagedIndex)
  102. {
  103. Revive(unmanagedModel);
  104. UnmanagedIndex = unmanagedIndex;
  105. name = Id;
  106. Opacity = UnmanagedParts.Opacities[unmanagedIndex];
  107. }
  108. }
  109. }