/** * 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 Live2D.Cubism.Core; using UnityEngine; namespace Live2D.Cubism.Framework { /// /// Cubism parameter store. /// public class CubismParameterStore : MonoBehaviour, ICubismUpdatable { /// /// Parameters cache. /// private CubismParameter[] DestinationParameters { get; set; } /// /// Parts cache. /// private CubismPart[] DestinationParts { get; set; } /// /// For storage parameters value. /// private float[] _parameterValues; /// /// For storage parts opacity. /// private float[] _partOpacities; /// /// Model has cubism update controller component. /// [HideInInspector] public bool HasUpdateController { get; set; } /// /// Called by cubism update controller. Order to invoke OnLateUpdate. /// public int ExecutionOrder { get { return CubismUpdateExecutionOrder.CubismParameterStoreSaveParameters; } } /// /// Called by cubism update controller. Needs to invoke OnLateUpdate on Editing. /// public bool NeedsUpdateOnEditing { get { return false; } } public void Refresh() { if (DestinationParameters == null) { DestinationParameters = this.FindCubismModel().Parameters; } if (DestinationParts == null) { DestinationParts = this.FindCubismModel().Parts; } // Get cubism update controller. HasUpdateController = (GetComponent() != null); SaveParameters(); } /// /// Called by cubism update controller. Updates controller. /// /// /// Make sure this method is called after any animations are evaluated. /// public void OnLateUpdate() { if (!HasUpdateController) { return; } SaveParameters(); } /// /// Save model parameters value and parts opacity. /// public void SaveParameters() { // Fail silently... if(!enabled) { return; } // save parameters value if(DestinationParameters != null && _parameterValues == null) { _parameterValues = new float[DestinationParameters.Length]; } if(_parameterValues != null) { for(var i = 0; i < _parameterValues.Length; ++i) { _parameterValues[i] = DestinationParameters[i].Value; } } // save parts opacity if(DestinationParts != null && _partOpacities == null) { _partOpacities = new float[DestinationParts.Length]; } if(_partOpacities != null) { for(var i = 0; i < _partOpacities.Length; ++i) { _partOpacities[i] = DestinationParts[i].Opacity; } } } /// /// Restore model parameters value and parts opacity. /// public void RestoreParameters() { // Fail silently... if(!enabled) { return; } // restore parameters value if(_parameterValues != null) { for(var i = 0; i < _parameterValues.Length; ++i) { DestinationParameters[i].Value = _parameterValues[i]; } } // restore parts opacity if(_partOpacities != null) { for(var i = 0; i < _partOpacities.Length; ++i) { DestinationParts[i].Opacity = _partOpacities[i]; } } } /// /// Called by Unity. /// private void OnEnable() { // Initialize cache. Refresh(); } } }