当前位置:网站首页>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
边栏推荐
- 深度学习------不同方法实现Inception-10
- Circle center technology, very anxious?
- redis集群概念
- Issue SSL certificate with IP address
- National Museum of Singapore - give you spiritual and physical satisfaction
- Lambda&Stream
- Have a heart beating Valentine's day in Singapore
- Solution to Autowired annotation warning
- [control] multi agent system summary. 5. system consolidation.
- PS1 Contemporary Art Center, Museum of modern art, New York
猜你喜欢
Force buckle 59 Spiral matrix II
Qos(Quality of Service)
Foreign SSL certificate
Singapore must visit these scenic spots during the Spring Festival
Software digital signature certificate
Dual domain SSL certificate
Preorder traversal of Li Kou 589:n fork tree
Bean creation process and lazy init delay loading mechanism
SSL update method
Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge
随机推荐
Software digital signature certificate
z-index属性在什么情况下会失效?
Malignant bug: 1252 of unit MySQL export
Circle center technology, very anxious?
Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge
Li Kou 2049: count the number of nodes with the highest score
Issue SSL certificate with IP address
Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium
Unity automatic pathfinding
Redis实现短信登入功能(二)Redis实现登入功能
Create transfer generation point
HTC vive cosmos development - handle button event
力扣周赛293题解
Is the Flink connector JDBC open source? Where can I download it
C # equipment synthesis
JS 数组的排序 sort方法详解
SSL universal domain name certificate
Collective system
Pourquoi l'ordinateur n'a - t - il pas de réseau après l'ouverture du Hotspot win10?
[control] multi agent system summary. 1. system model. 2. control objectives. 3. model transformation.