图片按钮的两态变化鼠标左键按下down和弹起up所触发的事件
编写两个脚本文件
一个是点击按钮的图片变化(UI代码
using UnityEngine;
using System.Collections;
public class MyUIButton : MonoBehaviour
{
public Texture2D[] btn;
public string doUpMethodName;
public string doDownMethodName;
Rect rc;
int index = 0;
void Update()
{
if(Input.GetMouseButtonUp(0))
{
rc = guiTexture.pixelInset;
rc.x += transform.position.x * Screen.width;
rc.y += transform.position.y * Screen.height;
if(rc.Contains(Input.mousePosition))
{
index = 1;
SendMessage(doUpMethodName,index);//按钮弹起时的事件触发
}
}
if(Input.GetMouseButtonDown(0))
{
rc = guiTexture.pixelInset;
rc.x += transform.position.x * Screen.width;
rc.y += transform.position.y * Screen.height;
if(rc.Contains(Input.mousePosition))
{
index = 0;
SendMessage(doDownMethodName);//按钮按下时的事件触发
}
}
guiTexture.texture = btn[index];
}
}
一个是点击按钮触发的事件(逻辑代码)
using UnityEngine;
using System.Collections;
public class DoAction : MonoBehaviour
{
void DoDownMethodName()
{
Debug.Log("down.down");
}
void DoUpMethodName()
{
Debug.Log("up.up");
}
}
将这两个脚本都绑定到一个GameObject上,并附加相应的变量属性值