Unity common code function summary

1. Singleton mode code

// singleton mode Static Resource _instance; Void Awake() { _instance = this; } Public static Resource Instance { Get { // no need to check if the variable is null Return _instance; } }

2.Touch

using UnityEngine.Events; using UnityEngine.EventSystems;

//Add touch monitor class Protected void AddTriggersListener(GameObject obj, EventTriggerType eventType, UnityAction<BaseEventData> action) { / / First determine whether the object already has EventTrigger component, if not then need to add EventTrigger trigger = obj.GetComponent<EventTrigger>(); If (trigger == null) { Trigger = obj.AddComponent<EventTrigger>(); } // instantiate delegates If (trigger.triggers.Count == 0) { Trigger.triggers = new List<EventTrigger.Entry>(); } / / Define the type of event to be bound EventTrigger.Entry entry = new EventTrigger.Entry(); / / Set the event type entry.eventID = eventType; / / Define the callback function UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(action); / / Set the callback function entry.callback.AddListener(callback); / / Add event trigger record to the event trigger component of GameObject trigger.triggers.Add(entry); }

AddTriggersListener(gameObject, EventTriggerType.PointerDown, HiddenOther);

//Function implementation Protected void HiddenOther(BaseEventData eventData) { / / Specific features }