以下方法纯属我YY,切勿当真!!!
给 Sprite添加点击事件步骤:
1. 创建一个 Sprite
2. 给Sprite添加一个 Box Collider 2D
3. 将如果脚本放到Sprite上:
using UnityEngine;
using System.Collections;
/// <summary>
/// 主要是按钮:
/// a. 默认的点击效果
/// b. 切换点击的图片
/// </summary>
public class ButtonSprite : MonoBehaviour{
public enum BTN_TYPE
{
DEFAULT,
NORMAL,
};
public string name = "ButtonSprite";
public BTN_TYPE btnType = BTN_TYPE.DEFAULT;
private Sprite defauleSprite;
public Sprite clickedSprite;
private SpriteRenderer render;
void Start(){
render = GetComponent<SpriteRenderer> ();
defauleSprite = render.sprite;
}
void OnMouseDown() {
if (btnType == BTN_TYPE.DEFAULT) {
render.color = Color.gray;
} else if (btnType == BTN_TYPE.NORMAL) {
render.sprite = clickedSprite;
}
}
void OnMouseUp(){
if (btnType == BTN_TYPE.DEFAULT) {
render.color = Color.white;
}else if(btnType == BTN_TYPE.NORMAL){
render.sprite = defauleSprite;
}
Debug.Log ("btn clicked!!!");
}
}
Over!!!