Created
July 10, 2017 08:42
-
-
Save msyaifullah/5426faccff9e88a8cf289e7a72e74f42 to your computer and use it in GitHub Desktop.
Unity UI Selectable (Focus)
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 UnityEngine; | |
using System.Collections; | |
using UnityEngine.EventSystems; | |
public class SetFirstSelectable : MonoBehaviour | |
{ | |
public GameObject firstSelectable; | |
public void ApplyFirstSelectable(GameObject firstSelectable) | |
{ | |
this.firstSelectable = firstSelectable; | |
} | |
void OnEnable() | |
{ | |
StartCoroutine(SetSelectableHandle(firstSelectable)); | |
} | |
IEnumerator SetSelectableHandle(GameObject target) | |
{ | |
if (this.enabled) | |
{ | |
yield return new WaitForEndOfFrame(); | |
SetSelectable(target); | |
} | |
} | |
void SetSelectable(GameObject target) | |
{ | |
var pointer = new PointerEventData(EventSystem.current); | |
ExecuteEvents.Execute(EventSystem.current.currentSelectedGameObject, pointer, ExecuteEvents.pointerExitHandler); | |
EventSystem.current.SetSelectedGameObject(target, new BaseEventData(EventSystem.current)); | |
ExecuteEvents.Execute(target, pointer, ExecuteEvents.selectHandler); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment