unity3d⾃定义Toggle组件,解决设置isOn⾃动调⽤⽅法
当我们在使⽤unity⾃带的Toggle组件的时候,我们是通过Toggle组件上的isOn属性得知是开启我们的功能⾯板还是关闭,这时候通常我们会给toggle绑定⼀个⽅法,当我们点击toggle的时候,调⽤⽅法,同时播放点击⾳效,可是有些时候,需要我们在切换界⾯的时候通过其它⽅法来关闭我们的属性isOn为false,⽅便我们下次点击的时候,isOn为true,这时候,我们单独对toggle的isOn属性赋值时会调⽤⼀次我们的⽅法,同时还会播放⼀次声⾳,恶⼼的事情就此发⽣,所以在项⽬中不得不重写了此组件,以⽅便项⽬中多处使⽤,下⾯是MyToggle脚本,挂载到对象⾝上即可,和unity⾃带的Toggle⽤法⼀样,只是在⼿动设置isOn属性的时候不会⾃动调⽤⽅法。
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;
/// <summary>
/// ⾃定义Toggle组件
/// </summary>
[RequireComponent(typeof(RectTransform))]
public class MyToggle : Selectable, IEventSystemHandler, IPointerClickHandler, ISubmitHandler, ICanvasElement
{
/// <summary>
/// toggle为true时切换显⽰的底图
/// </summary>
public Graphic m_ChangeImage;
[SerializeField]
[FormerlySerializedAs("m_IsActive")]
private bool m_IsOn;
public MyToggle.ToggleTransition toggleTransition = MyToggle.ToggleTransition.Fade;
public Toggle.ToggleEvent onValueChanged = new Toggle.ToggleEvent();
/// <summary>
/// 设置ison属性(⾃动调⽤⼀次⽅法)
/// </summary>
public bool isOn
{
get
{
return this.m_IsOn;
}
set
{
this.Set(value);
}
}
/// <summary>
/// 设置ison属性(不会⾃动调⽤⽅法)
/// </summary>
public bool IsOn
{
get
{
return this.m_IsOn;
}
set
{
this.Set(value, value, value);
}
}
}
protected MyToggle()
{
}
public virtual void Rebuild(CanvasUpdate executing)
{
if (executing != CanvasUpdate.Prelayout)
return;
}
public virtual void LayoutComplete()
{
}
public virtual void GraphicUpdateComplete()
{
}
protected override void OnEnable()
{
base.OnEnable();
this.PlayEffect(true);
this.IsOn = false;
}
protected override void OnDisable()
{
base.OnDisable();
this.IsOn = false;
}
private void Set(bool value)
{
this.Set(value, true);
}
private void Set(bool value, bool sendCallback)
{
if (this.m_IsOn == value)
return;
this.m_IsOn = value;
this.leTransition == MyToggle.ToggleTransition.None);
if (!sendCallback)
return;
}
private void Set(bool value, bool sendCallback, bool call)
{
if (this.m_IsOn == value)
return;
this.m_IsOn = value;
this.leTransition == MyToggle.ToggleTransition.None);
if (!sendCallback)
return;
return;
}
private void InternalToggle()
{
if (!this.IsActive() || !this.IsInteractable())
return;
this.isOn = !this.isOn;
}
/// <summary>
/// 点击事件
/// </summary>
/// <param name="eventData">Current event.</param>
public virtual void OnPointerClick(PointerEventData eventData)
{
if (eventData.button != PointerEventData.InputButton.Left)
return;
this.InternalToggle();
}
public virtual void OnSubmit(BaseEventData eventData)
{
this.InternalToggle();
}unity3d animation
public enum ToggleTransition
{
None,
Fade,
}
[Serializable]
public class ToggleEvent : UnityEvent<bool>
{
}
protected override void OnDidApplyAnimationProperties()
{
if ((UnityEngine.Object)this.m_ChangeImage != (UnityEngine.Object)null)
{
bool flag = !Mathf.Approximately(this.m_ChangeImage.canvasRenderer.GetColor().a, 0.0f);            if (this.m_IsOn != flag)
{
this.m_IsOn = flag;
this.Set(!flag);
}
}
base.OnDidApplyAnimationProperties();
}
private void PlayEffect(bool instant)
{
if ((UnityEngine.Object)this.m_ChangeImage == (UnityEngine.Object)null)
return;
if (!Application.isPlaying)
this.m_ChangeImage.canvasRenderer.SetAlpha(!this.m_IsOn ? 0.0f : 1f);
else
this.m_ChangeImage.CrossFadeAlpha(!this.m_IsOn ? 0.0f : 1f, !instant ? 0.1f : 0.0f, true);    }
protected override void Start()
{
this.PlayEffect(true);
}
}
protected override void OnValidate()
{
base.OnValidate();
this.Set(this.m_IsOn, false);
this.leTransition == MyToggle.ToggleTransition.None);
if (PrefabUtility.GetPrefabType((UnityEngine.Object)this) == PrefabType.Prefab || Application.isPlaying)            return;
CanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuild((ICanvasElement)this);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。