当前位置:网站首页>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
边栏推荐
- What to do when the alicloud SSL certificate expires
- Collective system
- 【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai
- Check London attractions suitable for parents and children in winter vacation
- Connect to the database and run node JS running database shows that the database is missing
- Static keyword
- One command to run rancher
- [control] multi agent system summary. 4. control agreement.
- Spring Festival Tourism Strategy: welcome the new year in Bangkok, Thailand
- Unreal 4 learning notes - data storage using blueprints
猜你喜欢

Wildcard SSL certificate issuing time

EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据

Geotrustov wildcard

Photon pun refresh hall room list

力扣977. 有序数组的平方

Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)

Foreign SSL certificate

【Paper】2021_ Analysis of the Consensus Protocol of Heterogeneous Agents with Time-Delays

Redis implements SMS login function (II) redis implements login function

Check London attractions suitable for parents and children in winter vacation
随机推荐
Solution to Autowired annotation warning
Wildcard SSL certificate issuing time
Exploration of unity webgl
深度学习------不同方法实现Inception-10
【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai
Webots notes day 2
力扣349. 两个数组的交集
图的一些表示方式、邻居和度的介绍
【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter
Servlet lifecycle
力扣周赛293题解
Webots learning notes
力扣27. 移除元素
One interview question a day the difference between B tree and b+ tree
Why does the computer have no network after win10 is turned on?
圆心科技,很焦虑?
Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
Output directory of log files after unity3d packaging
One interview question every day to talk about the process of TCP connection and disconnection
Harbor API 2.0 query