CubismMoc.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 System;
  8. using Live2D.Cubism.Core.Unmanaged;
  9. using UnityEngine;
  10. #if UNITY_EDITOR
  11. using UnityEditor;
  12. #endif
  13. namespace Live2D.Cubism.Core
  14. {
  15. /// <summary>
  16. /// Cubism moc asset.
  17. /// </summary>
  18. public sealed class CubismMoc : ScriptableObject
  19. {
  20. #region Factory Methods
  21. /// <summary>
  22. /// Checks consistency of a moc.
  23. /// </summary>
  24. public static bool HasMocConsistency(byte[] moc3)
  25. {
  26. return CubismUnmanagedMoc.HasMocConsistency(moc3);
  27. }
  28. /// <summary>
  29. /// Creates a <see cref="CubismMoc"/> asset from raw bytes.
  30. /// </summary>
  31. /// <param name="moc3">Source.</param>
  32. /// <param name="shouldCheckMocConsistency">Use the verification function?</param>
  33. /// <returns>Instance.</returns>
  34. public static CubismMoc CreateFrom(byte[] moc3, bool shouldCheckMocConsistency = true)
  35. {
  36. if (shouldCheckMocConsistency && !HasMocConsistency(moc3))
  37. {
  38. Debug.LogError("This Moc3 is Invalid. This model generation process is aborted and prefab is not created.\n" +
  39. $"Please check Model's Moc version. This CubismCore supported latest Moc version is `{LatestVersion}`.");
  40. return null;
  41. }
  42. var moc = CreateInstance<CubismMoc>();
  43. moc.Bytes = moc3;
  44. return moc;
  45. }
  46. #endregion
  47. /// <summary>
  48. /// Resets native handle.
  49. /// </summary>
  50. /// <param name="moc"></param>
  51. public static void ResetUnmanagedMoc(CubismMoc moc)
  52. {
  53. moc.UnmanagedMoc = null;
  54. moc.Revive();
  55. }
  56. /// <summary>
  57. /// Gets the latest .moc3 file version.
  58. /// </summary>
  59. public static uint LatestVersion
  60. {
  61. get
  62. {
  63. return CubismCoreDll.GetLatestMocVersion();
  64. }
  65. }
  66. /// <summary>
  67. /// <see cref="Bytes"/> backing field.
  68. /// </summary>
  69. [SerializeField, HideInInspector]
  70. private byte[] _bytes;
  71. /// <summary>
  72. /// Raw moc bytes.
  73. /// </summary>
  74. private byte[] Bytes
  75. {
  76. get { return _bytes; }
  77. set { _bytes = value; }
  78. }
  79. private CubismUnmanagedMoc UnmanagedMoc { get; set; }
  80. private int ReferenceCount { get; set; }
  81. #if UNITY_EDITOR
  82. private static int CoreNotFoundCallCount { get; set; }
  83. #endif
  84. /// <summary>
  85. /// True if instance is revived.
  86. /// </summary>
  87. public bool IsRevived
  88. {
  89. get
  90. {
  91. return UnmanagedMoc != null;
  92. }
  93. }
  94. /// <summary>
  95. /// Acquires native handle.
  96. /// </summary>
  97. /// <returns>Valid handle on success; <see cref="IntPtr.Zero"/> otherwise.</returns>
  98. public CubismUnmanagedMoc AcquireUnmanagedMoc()
  99. {
  100. ++ReferenceCount;
  101. #if UNITY_EDITOR
  102. try
  103. {
  104. #endif
  105. Revive();
  106. #if UNITY_EDITOR
  107. }
  108. catch (DllNotFoundException)
  109. {
  110. if (CoreNotFoundCallCount == 0)
  111. {
  112. EditorUtility.DisplayDialog("Live2D CubismCore is not loaded", "Please reboot this Unity project if it is just after import of the SDK. If it's not, please check if platform settings of dll is correct. dll cannot be used on platform which is different from its own build settings.", "ok", "cancel");
  113. }
  114. ++CoreNotFoundCallCount;
  115. }
  116. #endif
  117. return UnmanagedMoc;
  118. }
  119. /// <summary>
  120. /// Releases native handle.
  121. /// </summary>
  122. public void ReleaseUnmanagedMoc()
  123. {
  124. -- ReferenceCount;
  125. // Release instance of unmanaged moc in case the instance isn't referenced any longer.
  126. if (ReferenceCount == 0)
  127. {
  128. UnmanagedMoc.Release();
  129. UnmanagedMoc = null;
  130. }
  131. // Deal with invalid reference counts.
  132. else if (ReferenceCount < 0)
  133. {
  134. ReferenceCount = 0;
  135. }
  136. }
  137. /// <summary>
  138. /// Revives instance without acquiring it.
  139. /// </summary>
  140. private void Revive()
  141. {
  142. // Return if already revived.
  143. if (IsRevived)
  144. {
  145. return;
  146. }
  147. // Return if no bytes are available.
  148. if (Bytes == null)
  149. {
  150. return;
  151. }
  152. // Try revive.
  153. UnmanagedMoc = CubismUnmanagedMoc.FromBytes(Bytes);
  154. }
  155. public uint Version
  156. {
  157. get
  158. {
  159. return UnmanagedMoc.MocVersion;
  160. }
  161. }
  162. }
  163. }