在做项目的时候,尤其是在做一些小工具的时候,有时候需要做一些方便外面直接用调用的委托方法,又不想用脚本直接调用的的时候,就需要今天我所说的方法了,这个方法是我在看UGUI源码的时候看到的,其实就是Unity本身将InInspector面板的显示进行了封装。我们只要在定义变量之前将封装好的InInspector属性写在变量之前就好了。
下面是我写的一个Trigger触发器的工具,公开了一些需要的方法,其中包括触发开始,触发中,触发结束事件。还有就是触发开始后的一个倒计时事件,这个可以做一个触发延时执行的方法。其他的就不多说了,下面是代码。文章源自大腿Plus-https://www.zhaoshijun.com/archives/999
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
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面板的显示效果。文章源自大腿Plus-https://www.zhaoshijun.com/archives/999 文章源自大腿Plus-https://www.zhaoshijun.com/archives/999
我的微信
微信扫一扫
shijun_z
我的QQ
QQ扫一扫
846207670
评论