当前位置:网站首页>Four methods of unity ugui button binding events
Four methods of unity ugui button binding events
2022-06-30 04:52:00 【LintonL】
UGUI Visual creation and correlation of events are convenient , Dynamic creation can take advantage of the created Prefab instantiate , It's just a little complicated in terms of related events , This article summarizes several ways of associating events to buttons .
1. Visual creation and event binding #
Step 1 : adopt Hierarchy Panel creation UI > Button.
Step 2 : Create a script TestClick.cs, Defined a Click Of public Method .
Step 3 : Choose Hierarchy Medium Button, Add Component Script TestClick.cs
Step 4 : stay Button(Script) relation TestClick In the script Click Method .
Step 5 : Done.
TestClick.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestClick : MonoBehaviour { public void Click(){ Debug.Log ("Button Clicked. TestClick."); } } 2. Bind events by directly binding scripts #
Step 1 : adopt Hierarchy Panel creation UI > Button.
Step 2 : Create a ClickHandler.cs Script , Defines a private method OnClick(), And in Start() The method is Button Add listening for click events , Pass in as a parameter OnClick Method .
Step 3 : take ClickHandler Binding in Button On the object .
Step 4 : Done.
ClickHandler.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ClickHandler : MonoBehaviour { void Start () { Button btn = this.GetComponent<Button> (); btn.onClick.AddListener (OnClick); } private void OnClick(){ Debug.Log ("Button Clicked. ClickHandler."); } } 3. adopt EventTrigger Implement the button click event #
UGUI In the system Button By default, only OnClick Call method of , Sometimes we need to listen for mouse entry events (MouseIn) And mouse out events (MouseOut). We need help from UI In the system EventTrigger Script to achieve .
Step 1 : adopt Hierarchy Panel creation UI > Button.
Step 2 : Create a EventTriggerHandler.cs Script , utilize UnityEngine.EventSystems.EventTrigger Add listening Events .
Step 3 : binding EventTriggerHandler.cs Script to Button On .
Step 4 : Done.
EventTriggerHandler.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; // need EventTrigger Script support [RequireComponent(typeof(UnityEngine.EventSystems.EventTrigger))] public class EventTriggerHandler : MonoBehaviour { // Use this for initialization void Start () { Button btn = this.GetComponent<Button> (); EventTrigger trigger = btn.gameObject.GetComponent<EventTrigger> (); EventTrigger.Entry entry = new EventTrigger.Entry (); // Mouse click event entry.eventID = EventTriggerType.PointerClick; // Mouse in event entry.eventID = EventTriggerType.PointerEnter; // Mouse slide out event entry.eventID = EventTriggerType.PointerExit; entry.callback = new EventTrigger.TriggerEvent (); entry.callback.AddListener (OnClick); // entry.callback.AddListener (OnMouseEnter); trigger.triggers.Add (entry); } private void OnClick(BaseEventData pointData){ Debug.Log ("Button Clicked. EventTrigger.."); } private void OnMouseEnter(BaseEventData pointData){ Debug.Log ("Button Enter. EventTrigger.."); } } 4. adopt MonoBehaviour Implement the event class interface to realize event listening #
Step 1 : adopt Hierarchy Panel creation UI > Button.
Step 2 : Create a EventHandler.cs Script .
Step 3 : Bind the script to Button On the object .
Step 4 : Done.
EventHandler.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class EventHandler : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IDragHandler { public void OnPointerClick(PointerEventData eventData){ if(eventData.pointerId == -1){ Debug.Log ("Left Mouse Clicked."); } else if(eventData.pointerId == -2){ Debug.Log ("Right Mouse Clicked."); } } public void OnPointerEnter(PointerEventData eventData){ Debug.Log ("Pointer Enter.."); } public void OnPointerExit(PointerEventData eventData){ Debug.Log ("Pointer Exit.."); } public void OnPointerDown(PointerEventData eventData){ Debug.Log ("Pointer Down.."); } public void OnDrag(PointerEventData eventData){ Debug.Log ("Dragged.."); } } UGUI How to determine UI Which button of the mouse is the element clicked on , In the above code, we can according to eventData.pointerId To monitor whether the left mouse button or the right mouse button . But every one of them UI Elements create a MonoBehaviour It's obviously not good to monitor events , The following is through the use of Delegate and Event To make a general class UIEventListener To deal with Events ( Observer mode ).
UIEventListener.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class UIEventListener : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler { // Define event proxies public delegate void UIEventProxy(GameObject gb); // Mouse click event public event UIEventProxy OnClick; // Mouse in event public event UIEventProxy OnMouseEnter; // Mouse slide out event public event UIEventProxy OnMouseExit; public void OnPointerClick(PointerEventData eventData){ if (OnClick != null) OnClick (this.gameObject); } public void OnPointerEnter(PointerEventData eventData){ if (OnMouseEnter != null) OnMouseEnter (this.gameObject); } public void OnPointerExit(PointerEventData eventData){ if (OnMouseExit != null) OnMouseExit (this.gameObject); } } TestEvent.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TestEvent : MonoBehaviour { void Start () { Button btn = this.GetComponent<Button> (); UIEventListener btnListener = btn.gameObject.AddComponent<UIEventListener> (); btnListener.OnClick += delegate(GameObject gb) { Debug.Log(gb.name + " OnClick"); }; btnListener.OnMouseEnter += delegate(GameObject gb) { Debug.Log(gb.name + " OnMouseEnter"); }; btnListener.OnMouseExit += delegate(GameObject gb) { Debug.Log(gb.name + " OnMOuseExit"); }; } } TestEvent Script binding in Button You can go up. .
Project structure
from :http://www.cnblogs.com/isayes/p/6370168.html
If you don't understand, please click to read the original text
边栏推荐
- Have a heart beating Valentine's day in Singapore
- Issue SSL certificate with IP address
- Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium
- [fpga] implementation of IIC read / write EEPROM
- Preorder traversal of Li Kou 589:n fork tree
- How to repair expired SSL certificates?
- Unity is associated with vs. there is a compiler problem when opening
- Difference between request forwarding and redirection
- Keywords implements and @override
- What is SQL injection and how to avoid it?
猜你喜欢

Using the command line to convert JSON to dart file in fluent

IIS request SSL certificate

Bean creation process and lazy init delay loading mechanism

Force buckle 59 Spiral matrix II

Meet in Bangkok for a romantic trip on Valentine's Day

Approaching history, introduction to the London Guard Museum

力扣589:N 叉树的前序遍历

Universal Studios Singapore: a good place for a one-day parent-child tour in Singapore

为什么win10开热点后电脑没有网络?

EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据
随机推荐
brew安装nvm报nvm command not found解决方案
What to do when the alicloud SSL certificate expires
Unity realizes rotation and Revolution
Connect to the database and run node JS running database shows that the database is missing
The subsystem implementing transaction persistence in DBMS is ()
Output directory of log files after unity3d packaging
Is the Flink connector JDBC open source? Where can I download it
Unity a* road finding force planning
Redis实现短信登入功能(一)传统的Session登入
How to repair expired SSL certificates?
harbor api 2.0查询
Oracle-数据的基本操作
A collection of errors encountered in machine learning with unity
Li Kou 2049: count the number of nodes with the highest score
Check London attractions suitable for parents and children in winter vacation
Harbor API 2.0 query
Webots notes day 2
【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator
Redis implements SMS login function (I) traditional session login
【Paper】2019_ Consensus Control of Multiple AUVs Recovery System Under Switching Topologies and Time D