当前位置:网站首页>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
边栏推荐
- 力扣2049:统计最高分的节点数目
- Issue SSL certificate with IP address
- Error about the new version of UE4: unavigationsystemv1:: simplemovetoactor has been deprecated
- Preorder traversal of Li Kou 589:n fork tree
- Royal Albert Hall, a popular landmark in London
- Exploration of unity webgl
- Webots learning notes
- Unity automatic pathfinding
- Unity lens making
- amd锐龙CPU A320系列主板如何安装win7
猜你喜欢

A must see cruise experience in Bangkok: visit the Mekong River and enjoy the scenery on both sides of the river

UE4 method of embedding web pages

Autowired注解警告的解决办法

Singapore parent-child tour, these popular attractions must be arranged

Cheap SSL certificate abroad

Issue SSL certificate with IP address
![[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example](/img/d7/7bf43437edb87b69cdc5ae858f44e1.jpg)
[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example

HTC vive cosmos development - handle button event

Oracle-数据的基本操作

The most comprehensive summary notes of redis foundation + advanced project in history
随机推荐
Efficiency test of adding and querying ArrayList and LinkedList
Qos(Quality of Service)
[fpga] implementation of IIC read / write EEPROM
amd锐龙CPU A320系列主板如何安装win7
MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field
Oracle-数据的基本操作
力扣977. 有序数组的平方
Check London attractions suitable for parents and children in winter vacation
Exploration of unity webgl
Window10 jar double click to run without response
National Museum of Singapore - give you spiritual and physical satisfaction
【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai
Unreal 4 learning notes - data storage using blueprints
Brew install NVM command not found solution
brew安装nvm报nvm command not found解决方案
Collective system
Meet in Bangkok for a romantic trip on Valentine's Day
力扣704. 二分查找
Force buckle 59 Spiral matrix II
svg和canvas的区别