unity怎么让ui按钮响应事件

如题所述

图片按钮的两态变化鼠标左键按下down和弹起up所触发的事件
编写两个脚本文件
一个是点击按钮的图片变化(UI代码)

1 using UnityEngine;
2 using System.Collections;
3
4 public class MyUIButton : MonoBehaviour
5 {
6 public Texture2D[] btn;
7 public string doUpMethodName;
8 public string doDownMethodName;
9 Rect rc;
10 int index = 0;
11
12 void Update()
13 {
14 if(Input.GetMouseButtonUp(0))
15 {
16 rc = guiTexture.pixelInset;
17 rc.x += transform.position.x * Screen.width;
18 rc.y += transform.position.y * Screen.height;
19 if(rc.Contains(Input.mousePosition))
20 {
21 index = 1;
22 SendMessage(doUpMethodName,index);//按钮弹起时的事件触发
23 }
24 }
25
26 if(Input.GetMouseButtonDown(0))
27 {
28 rc = guiTexture.pixelInset;
29 rc.x += transform.position.x * Screen.width;
30 rc.y += transform.position.y * Screen.height;
31 if(rc.Contains(Input.mousePosition))
32 {
33 index = 0;
温馨提示:答案为网友推荐,仅供参考