基础内容
必要插件安装 缓动曲线和动画基础 ElementFolder,Track与其次级模块,PathNode重构
This commit is contained in:
75
Assets/Modern UI Pack/Scripts/Toggle/CustomToggle.cs
Normal file
75
Assets/Modern UI Pack/Scripts/Toggle/CustomToggle.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Michsky.MUIP
|
||||
{
|
||||
[RequireComponent(typeof(Toggle))]
|
||||
[RequireComponent(typeof(Animator))]
|
||||
public class CustomToggle : MonoBehaviour
|
||||
{
|
||||
[HideInInspector] public Toggle toggleObject;
|
||||
[HideInInspector] public Animator toggleAnimator;
|
||||
|
||||
[Header("Settings")]
|
||||
public bool invokeOnAwake;
|
||||
bool isInitialized = false;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (toggleObject == null) { toggleObject = gameObject.GetComponent<Toggle>(); }
|
||||
if (toggleAnimator == null) { toggleAnimator = toggleObject.GetComponent<Animator>(); }
|
||||
if (invokeOnAwake == true) { toggleObject.onValueChanged.Invoke(toggleObject.isOn); }
|
||||
|
||||
toggleObject.onValueChanged.AddListener(UpdateState);
|
||||
UpdateState();
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (isInitialized == false)
|
||||
return;
|
||||
|
||||
UpdateState();
|
||||
}
|
||||
|
||||
public void UpdateState()
|
||||
{
|
||||
if (gameObject.activeInHierarchy == true)
|
||||
{
|
||||
StopCoroutine("DisableAnimator");
|
||||
StartCoroutine("DisableAnimator");
|
||||
}
|
||||
|
||||
else { return; }
|
||||
|
||||
toggleAnimator.enabled = true;
|
||||
|
||||
if (toggleObject.isOn) { toggleAnimator.Play("On Instant"); }
|
||||
else { toggleAnimator.Play("Off Instant"); }
|
||||
}
|
||||
|
||||
public void UpdateState(bool value)
|
||||
{
|
||||
if (gameObject.activeInHierarchy == true)
|
||||
{
|
||||
StopCoroutine("DisableAnimator");
|
||||
StartCoroutine("DisableAnimator");
|
||||
}
|
||||
|
||||
else { return; }
|
||||
|
||||
toggleAnimator.enabled = true;
|
||||
|
||||
if (toggleObject.isOn) { toggleAnimator.Play("Toggle On"); }
|
||||
else { toggleAnimator.Play("Toggle Off"); }
|
||||
}
|
||||
|
||||
IEnumerator DisableAnimator()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(0.6f);
|
||||
toggleAnimator.enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user