/** * 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.ParameretRepeat { public class CubismRepeatController : MonoBehaviour, ICubismUpdatable { #region Variable /// /// Model has cubism update controller component. /// [HideInInspector] public bool HasUpdateController { get; set; } /// /// Destinations. /// private CubismParameter[] Destinations { get; set; } #endregion #region Function /// /// Refreshes the controller. Call this method after adding and/or removing s. /// public void Refresh() { var model = GetComponent(); if (model == null) { return; } Destinations = model.Parameters; // Get cubism update controller. HasUpdateController = (GetComponent() != null); } /// /// Called by cubism update controller. Order to invoke OnLateUpdate. /// public int ExecutionOrder { get { return CubismUpdateExecutionOrder.CubismRepeatController; } } /// /// Called by cubism update controller. Needs to invoke OnLateUpdate on Editing. /// public bool NeedsUpdateOnEditing { get { return false; } } /// /// Called by cubism update controller. Updates controller. /// /// /// Make sure this method is called after any animations are evaluated. /// public void OnLateUpdate() { // Fail silently. if (!enabled || Destinations == null) { return; } for (var i = 0; i < Destinations.Length; i++) { var parameter = Destinations[i]; if (parameter.IsRepeat()) { parameter.Value = parameter.GetParameterRepeatValue(parameter.Value); } else { parameter.Value = parameter.GetParameterClampValue(parameter.Value); } } } #endregion #region Unity Events Handling /// /// Initializes instance. /// private void OnEnable() { // Initialize cache. Refresh(); } /// /// Called by Unity. /// private void LateUpdate() { if (!HasUpdateController) { OnLateUpdate(); } } #endregion } }