/** * 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 { /// /// controller. /// public sealed class CubismEyeBlinkController : MonoBehaviour, ICubismUpdatable { /// /// Blend mode. /// [SerializeField] public CubismParameterBlendMode BlendMode = CubismParameterBlendMode.Multiply; /// /// Opening of the eyes. /// [SerializeField, Range(0f, 1f)] public float EyeOpening = 1f; /// /// Eye blink parameters cache. /// private CubismParameter[] Destinations { get; set; } /// /// Model has update controller component. /// [HideInInspector] public bool HasUpdateController { get; set; } /// /// Refreshes controller. Call this method after adding and/or removing s. /// public void Refresh() { var model = this.FindCubismModel(); // Fail silently... if (model == null) { return; } // Cache destinations. var tags = model .Parameters .GetComponentsMany(); Destinations = new CubismParameter[tags.Length]; for (var i = 0; i < tags.Length; ++i) { Destinations[i] = tags[i].GetComponent(); } // Get cubism update controller. HasUpdateController = (GetComponent() != null); } /// /// Called by cubism update controller. Order to invoke OnLateUpdate. /// public int ExecutionOrder { get { return CubismUpdateExecutionOrder.CubismEyeBlinkController; } } /// /// Called by cubism update controller. Needs to invoke OnLateUpdate on Editing. /// public bool NeedsUpdateOnEditing { get { return false; } } /// /// Called by cubism update controller. Updates controller. /// public void OnLateUpdate() { // Fail silently. if (!enabled || Destinations == null) { return; } // Apply value. Destinations.BlendToValue(BlendMode, EyeOpening); } #region Unity Event Handling /// /// Called by Unity. Makes sure cache is initialized. /// private void Start() { // Initialize cache. Refresh(); } /// /// Called by Unity. /// private void LateUpdate() { if(!HasUpdateController) { OnLateUpdate(); } } #endregion } }