Tuesday, May 26, 2015

Unity uGUI 模擬Button按下

 var pointer = new PointerEventData(EventSystem.current);
 ExecuteEvents.Execute(button.gameObject, pointer, ExecuteEvents. submitHandler);


Android Back Button應用:

using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;

public class UIBackButton : MonoBehaviour {

    public static UIBackButton currentBack;
    static List<UIBackButton> backList = new List<UIBackButton>();

    void OnEnable () {
        if(currentBack != null) {
            backList.Insert (0, currentBack);
        } else {
            backList.Clear();
        }
        currentBack = this;
    }

    void OnDisable(){
        if(backList.Count > 0) {
            currentBack = backList[0];
            backList.RemoveAt(0);
        } else {
            currentBack = null;
        }
    }

    
    void Update(){
        if(currentBack == this) {
            //Android Back Button
            if (Input.GetKeyDown(KeyCode.Escape)) {

                var pointer = new PointerEventData(EventSystem.current);
                ExecuteEvents.Execute(currentBack.gameObject, pointer, ExecuteEvents.submitHandler);

            }
        }
    }


}





參考:

http://answers.unity3d.com/questions/820599/simulate-button-presses-through-code-unity-46-gui.html

No comments: