Skip to content

Instantly share code, notes, and snippets.

@msyaifullah
Created July 10, 2017 08:42
Show Gist options
  • Save msyaifullah/5426faccff9e88a8cf289e7a72e74f42 to your computer and use it in GitHub Desktop.
Save msyaifullah/5426faccff9e88a8cf289e7a72e74f42 to your computer and use it in GitHub Desktop.
Unity UI Selectable (Focus)
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