using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.Serialization; public class ShaderControlValue : MonoBehaviour { [Space(10)] public List nameOn; public List values_Init; public List values_Play; [Space(10)] public bool isPlay = false; public bool LOOP = false; [Space(10)] [Range(0, 5)] public float duration = 3f; Material material; float durationSum = 0; float timeCurve; [Space(10)] public Material mat; Material matCopy; public Material InitPlay() { if (matCopy == null && mat != null) matCopy = new Material(mat); if (matCopy) { InitPlay(matCopy); return matCopy; } return null; } public void InitPlay(Material mat) { material = mat; Play(); } public void Init(Material mat, bool PLAY = false, bool LOOP = false) { material = mat; if (PLAY) { this.LOOP = LOOP; Play(); } } void Update() { if (isPlay && material != null) { durationSum += Time.deltaTime; timeCurve = durationSum / duration; for (int i = 0; i < values_Play.Count; i++) material.SetFloat($"{values_Play[i].name}", values_Play[i].value * values_Play[i].curve.Evaluate(timeCurve)); if (isPlay && duration < durationSum) { durationSum = 0; if (!LOOP) { if (OnEnd != null) OnEnd(); if (KEEP) { isPlay = false; enabled = isPlay; } else { Play(false, false, null); OnEndPlay(); } } } } } System.Action OnEnd; bool KEEP = false; public void Play(bool isPlay, bool LOOP, System.Action OnEnd, bool KEEP = false) { this.isPlay = isPlay; this.LOOP = LOOP; this.OnEnd = OnEnd; this.KEEP = KEEP; enabled = isPlay; } public void Play() { isPlay = true; enabled = isPlay; } public void Stop() { if (isPlay) { if (OnEnd != null) OnEnd(); isPlay = false; enabled = isPlay; } } [Serializable] public class EndEvent : UnityEvent { } [FormerlySerializedAs("onEnd")] [SerializeField] private EndEvent m_OnEnd = new EndEvent(); public EndEvent OnPlayEnd { get { return m_OnEnd; } set { m_OnEnd = value; } } public void OnEndPlay() { OnPlayEnd.Invoke(); } }