先上效果图
重点是这个Graphic
要清空,不然显示不能直接切换,会消失
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;public class SwitchToggle : MonoBehaviour
{public RectTransform handleRect;public float duration = 0.5f;private Vector2 handlePos;Toggle toggle;private void Awake(){toggle = GetComponent<Toggle>();handlePos = handleRect.anchoredPosition;toggle.onValueChanged.AddListener(OnSwitch);OnSwitch(toggle.isOn);}private void OnSwitch(bool on){handleRect.DOAnchorPos(on ? handlePos : -handlePos, duration).SetEase(Ease.InOutBack);}private void OnDestroy(){toggle.onValueChanged.RemoveListener(OnSwitch);}
}