Unity3D new UI convienience extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using UnityEngine; | |
using System; | |
using System.Collections.Generic; | |
using Debug = UnityEngine.Debug; | |
using Object = UnityEngine.Object; | |
using Random = UnityEngine.Random; | |
using UnityEngine.EventSystems; | |
using UnityEngine.Events; | |
public static class Extensions | |
{ | |
public static void AddListener(this EventTrigger eventTrigger, EventTriggerType type, UnityAction<BaseEventData> action) | |
{ | |
if (eventTrigger.delegates == null) | |
{ | |
eventTrigger.delegates = new List<EventTrigger.Entry>(); | |
} | |
var entry = eventTrigger.delegates.Find(e => e.eventID == type); | |
if (entry == null) | |
{ | |
entry = new EventTrigger.Entry(); | |
entry.eventID = type; | |
entry.callback = new EventTrigger.TriggerEvent(); | |
eventTrigger.delegates.Add(entry); | |
} | |
entry.callback.AddListener(action); | |
} | |
public static void SetSelected(this EventSystem eventSystem, MonoBehaviour selected) | |
{ | |
var pointer = new BaseEventData(eventSystem); | |
eventSystem.SetSelectedGameObject(selected.gameObject, pointer); | |
} | |
public static void SetSelected(this EventSystem eventSystem, GameObject selected) | |
{ | |
var pointer = new BaseEventData(eventSystem); | |
eventSystem.SetSelectedGameObject(selected, pointer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment