CubismMoc.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /// Creates a <see cref="CubismMoc"/> asset from raw bytes.
  23. /// </summary>
  24. /// <param name="moc3">Source.</param>
  25. /// <returns>Instance.</returns>
  26. public static CubismMoc CreateFrom(byte[] moc3)
  27. {
  28. var moc = CreateInstance<CubismMoc>();
  29. moc.Bytes = moc3;
  30. return moc;
  31. }
  32. #endregion
  33. /// <summary>
  34. /// Resets native handle.
  35. /// </summary>
  36. /// <param name="moc"></param>
  37. public static void ResetUnmanagedMoc(CubismMoc moc)
  38. {
  39. moc.UnmanagedMoc = null;
  40. moc.Revive();
  41. }
  42. /// <summary>
  43. /// <see cref="Bytes"/> backing field.
  44. /// </summary>
  45. [SerializeField]
  46. private byte[] _bytes;
  47. /// <summary>
  48. /// Raw moc bytes.
  49. /// </summary>
  50. private byte[] Bytes
  51. {
  52. get { return _bytes; }
  53. set { _bytes = value; }
  54. }
  55. private CubismUnmanagedMoc UnmanagedMoc { get; set; }
  56. private int ReferenceCount { get; set; }
  57. #if UNITY_EDITOR
  58. private static int CoreNotFoundCallCount { get; set; }
  59. #endif
  60. /// <summary>
  61. /// True if instance is revived.
  62. /// </summary>
  63. public bool IsRevived
  64. {
  65. get
  66. {
  67. return UnmanagedMoc != null;
  68. }
  69. }
  70. /// <summary>
  71. /// Acquires native handle.
  72. /// </summary>
  73. /// <returns>Valid handle on success; <see cref="IntPtr.Zero"/> otherwise.</returns>
  74. public CubismUnmanagedMoc AcquireUnmanagedMoc()
  75. {
  76. ++ReferenceCount;
  77. #if UNITY_EDITOR
  78. try
  79. {
  80. #endif
  81. Revive();
  82. #if UNITY_EDITOR
  83. }
  84. catch (DllNotFoundException)
  85. {
  86. if (CoreNotFoundCallCount == 0)
  87. {
  88. 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");
  89. }
  90. ++CoreNotFoundCallCount;
  91. }
  92. #endif
  93. return UnmanagedMoc;
  94. }
  95. /// <summary>
  96. /// Releases native handle.
  97. /// </summary>
  98. public void ReleaseUnmanagedMoc()
  99. {
  100. -- ReferenceCount;
  101. // Release instance of unmanaged moc in case the instance isn't referenced any longer.
  102. if (ReferenceCount == 0)
  103. {
  104. UnmanagedMoc.Release();
  105. UnmanagedMoc = null;
  106. }
  107. // Deal with invalid reference counts.
  108. else if (ReferenceCount < 0)
  109. {
  110. ReferenceCount = 0;
  111. }
  112. }
  113. /// <summary>
  114. /// Revives instance without acquiring it.
  115. /// </summary>
  116. private void Revive()
  117. {
  118. // Return if already revived.
  119. if (IsRevived)
  120. {
  121. return;
  122. }
  123. // Return if no bytes are available.
  124. if (Bytes == null)
  125. {
  126. return;
  127. }
  128. // Try revive.
  129. UnmanagedMoc = CubismUnmanagedMoc.FromBytes(Bytes);
  130. }
  131. }
  132. }