Unity3D编辑器:类似Button组件按钮委托事件的拖拽样式

在做项目的时候,尤其是在做一些小工具的时候,有时候需要做一些方便外面直接用调用的委托方法,又不想用脚本直接调用的的时候,就需要今天我所说的方法了,这个方法是我在看UGUI源码的时候看到的,其实就是Unity本身将InInspector面板的显示进行了封装。我们只要在定义变量之前将封装好的InInspector属性写在变量之前就好了。

下面是我写的一个Trigger触发器的工具,公开了一些需要的方法,其中包括触发开始,触发中,触发结束事件。还有就是触发开始后的一个倒计时事件,这个可以做一个触发延时执行的方法。其他的就不多说了,下面是代码。

using UnityEngine;
using UnityEngine.Events;
using System;
using UnityEngine.Serialization;
using System.Collections;
using UnityEngine.UI;

[RequireComponent(typeof(Collider))]
[RequireComponent(typeof(Rigidbody))]
public class TriggerEvents : MonoBehaviour
{
    [Header("OnTrigger TimeDown Event")]
    [Space]
    //[HideInInspector]
    public float delayTime = 0f;
    
    [Serializable]
    public class TimeDownStartEvent : UnityEvent<float> { }
    //[HideInInspector]
    // Event delegates triggered.
    [FormerlySerializedAs("onTimeDownStart")]
    [SerializeField]
    private TimeDownStartEvent m_OnTimeDownStart = new TimeDownStartEvent();
    public TimeDownStartEvent timeDownStart
    {
        get { return m_OnTimeDownStart; }
        set { m_OnTimeDownStart = value; }
    }
    [Serializable]
    public class TimeDownEndEvent : UnityEvent { }
    //[HideInInspector]
    // Event delegates triggered.
    [FormerlySerializedAs("onTimeDownEnd")]
    [SerializeField]
    private TimeDownEndEvent m_OnTimeDownEnd = new TimeDownEndEvent();
    public TimeDownEndEvent timeDownEnd
    {
        get { return m_OnTimeDownEnd; }
        set { m_OnTimeDownEnd = value; }
    }

    [Serializable]
    public class TriggerEvent : UnityEvent<GameObject, GameObject> { }

    // Event delegates triggered.
    [FormerlySerializedAs("onEnter")]
    [Header("OnTrigger Event")]
    [Space]
    [SerializeField]
    private TriggerEvent m_OnEnter = new TriggerEvent();
    public TriggerEvent triggerEnter
    {
        get { return m_OnEnter; }
        set { m_OnEnter = value; }
    }

    // Event delegates triggered.
    [FormerlySerializedAs("onStay")]
    [SerializeField]
    private TriggerEvent m_OnStay = new TriggerEvent();
    public TriggerEvent triggerStay
    {
        get { return m_OnStay; }
        set { m_OnStay = value; }
    }

    // Event delegates triggered.
    [FormerlySerializedAs("onExit")]
    [SerializeField]
    private TriggerEvent m_OnExit = new TriggerEvent();
    public TriggerEvent triggerExit
    {
        get { return m_OnExit; }
        set { m_OnExit = value; }
    }
    // Use this for initialization
    void Awake()
    {
        Rigidbody rigidbody = GetComponent<Rigidbody>();
        rigidbody.useGravity = false;
    }
    // 开始接触
    void OnTriggerEnter(Collider collider)
    {
        StartCoroutine(TimeOut());
        timeDownStart.Invoke(delayTime);
        m_OnEnter.Invoke(gameObject, collider.gameObject);
    }
    // 接触持续中
    void OnTriggerStay(Collider collider)
    {
        m_OnStay.Invoke(gameObject, collider.gameObject);
    }
    // 接触结束
    void OnTriggerExit(Collider collider)
    {
        StopAllCoroutines();
        m_OnTimeDownEnd.Invoke();
        m_OnExit.Invoke(gameObject, collider.gameObject);
    }
    IEnumerator TimeOut()
    {
        yield return new WaitForSeconds(delayTime);
        m_OnTimeDownEnd.Invoke();
    }
}

上面的图片是InInspector面板的显示效果。

You May Also Like

About the Author: 大腿Plus

发表评论