123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- /**
- * Copyright(c) Live2D Inc. All rights reserved.
- *
- * Use of this source code is governed by the Live2D Open Software license
- * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
- */
- using System;
- using Live2D.Cubism.Core.Unmanaged;
- using UnityEngine;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- namespace Live2D.Cubism.Core
- {
- /// <summary>
- /// Cubism moc asset.
- /// </summary>
- public sealed class CubismMoc : ScriptableObject
- {
- #region Factory Methods
- /// <summary>
- /// Creates a <see cref="CubismMoc"/> asset from raw bytes.
- /// </summary>
- /// <param name="moc3">Source.</param>
- /// <returns>Instance.</returns>
- public static CubismMoc CreateFrom(byte[] moc3)
- {
- var moc = CreateInstance<CubismMoc>();
- moc.Bytes = moc3;
- return moc;
- }
- #endregion
- /// <summary>
- /// Resets native handle.
- /// </summary>
- /// <param name="moc"></param>
- public static void ResetUnmanagedMoc(CubismMoc moc)
- {
- moc.UnmanagedMoc = null;
- moc.Revive();
- }
- /// <summary>
- /// <see cref="Bytes"/> backing field.
- /// </summary>
- [SerializeField]
- private byte[] _bytes;
- /// <summary>
- /// Raw moc bytes.
- /// </summary>
- private byte[] Bytes
- {
- get { return _bytes; }
- set { _bytes = value; }
- }
- private CubismUnmanagedMoc UnmanagedMoc { get; set; }
- private int ReferenceCount { get; set; }
- #if UNITY_EDITOR
- private static int CoreNotFoundCallCount { get; set; }
- #endif
- /// <summary>
- /// True if instance is revived.
- /// </summary>
- public bool IsRevived
- {
- get
- {
- return UnmanagedMoc != null;
- }
- }
- /// <summary>
- /// Acquires native handle.
- /// </summary>
- /// <returns>Valid handle on success; <see cref="IntPtr.Zero"/> otherwise.</returns>
- public CubismUnmanagedMoc AcquireUnmanagedMoc()
- {
- ++ReferenceCount;
- #if UNITY_EDITOR
- try
- {
- #endif
- Revive();
- #if UNITY_EDITOR
- }
- catch (DllNotFoundException)
- {
- if (CoreNotFoundCallCount == 0)
- {
- 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");
- }
- ++CoreNotFoundCallCount;
- }
- #endif
- return UnmanagedMoc;
- }
- /// <summary>
- /// Releases native handle.
- /// </summary>
- public void ReleaseUnmanagedMoc()
- {
- -- ReferenceCount;
- // Release instance of unmanaged moc in case the instance isn't referenced any longer.
- if (ReferenceCount == 0)
- {
- UnmanagedMoc.Release();
- UnmanagedMoc = null;
- }
- // Deal with invalid reference counts.
- else if (ReferenceCount < 0)
- {
- ReferenceCount = 0;
- }
- }
- /// <summary>
- /// Revives instance without acquiring it.
- /// </summary>
- private void Revive()
- {
- // Return if already revived.
- if (IsRevived)
- {
- return;
- }
- // Return if no bytes are available.
- if (Bytes == null)
- {
- return;
- }
- // Try revive.
- UnmanagedMoc = CubismUnmanagedMoc.FromBytes(Bytes);
- }
- }
- }
|