首先是button的简单应用,从界面添加,到设置button属性。再添加text,为Text制定动画,再为text添加移除脚本,将text制成prefab,再实现点击button动态创建一个text。
再给button添加一个响应动作,代码如下,最后在OnClick中选择刚写的响应动作,完成简单的button应用。
public class MyButtonEvent : MonoBehaviour {public Text myText;private Text tempText;// Use this for initializationpublic void creatMyText(string message){tempText = (Text)Instantiate (myText);tempText.GetComponent
}
接下来是实现用事件来响应。
给button添加一个代码,如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;[RequireComponent(typeof(UnityEngine.EventSystems.EventTrigger))]
public class EventTriggerHandler : MonoBehaviour {void Start () {Button btn = this.GetComponent
但如果每次添加新的响应事件和响应函数都要重新写和实例化,于是我新建一个类用来对其进行封装。后面就只需要引用命名空间和类方法写入对应的量就可以了,封装类代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;namespace ButtonEvent{
public class EventTriggerFZ : MonoBehaviour {public EventTriggerFZ(){}public static void EventTriggerUse(GameObject obj,EventTriggerType x,UnityEngine.Events.UnityAction
}
后面添加在button上的代码大大缩减。代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using ButtonEvent;
public class EventTriggerHandler : MonoBehaviour {private GameObject button ;
void Start () {button = this.gameObject;EventTriggerFZ.EventTriggerUse (button, EventTriggerType.PointerClick, OnClick);}private void OnClick(BaseEventData pointData){Debug.Log ("Button1 Clicked.EventTrigger..");}}
接下来将更加深入的学习继承和事件。
EventTrigger事件触发的可执行函数要求:
1必须为Public 2函数返回为void 3只有一个输入参数 4 输入参数包括:int,float,string以及Object。
EventSystem事件管理器的使用:
事件接口大概分为这几类:
Pointer 鼠标指针类:IPointerClickHandler,IPointerDownHandler,IPointerEnterHandler,IPointerExitHandler,IPointerUpHandler (点击完(不可移出范围),点下,区域进入,移出,抬起(可移出范围))
Drag&Drop 拖拽类:IBeginDragHandler,IInitializePotentialDragHandler,IDragHandler,IEndDragHandler,IDropHandler
Selecr 点选类:IUpdateSelectedHandler,ISelectHandler,IDeselectHandler
Input 输入类:IScrollHandler,IMoveHandler,ISubmitHandler,ICancelHandler