129 lines
2.6 KiB
C#
129 lines
2.6 KiB
C#
using UnityEngine;
|
|||
|
|
using System.Collections;
|
||
|
|
|
||
|
|
|
||
|
|
public delegate void ButtonDelegate(Button sender);
|
||
|
|
|
||
|
|
[ExecuteInEditMode]
|
||
|
|
public class Button : MonoBehaviour {
|
||
|
|
public Texture2D image;
|
||
|
|
public Texture2D mask;
|
||
|
|
public Vector2 size;
|
||
|
|
public Material mat;
|
||
|
|
public Color baseColor;
|
||
|
|
private bool pressed;
|
||
|
|
private Rect buttonRect;
|
||
|
|
public event ButtonDelegate EventDown;
|
||
|
|
public event ButtonDelegate EventUp;
|
||
|
|
public event ButtonDelegate EventClick;
|
||
|
|
private int fingerId = -1;
|
||
|
|
|
||
|
|
private void OnDown(){
|
||
|
|
if (EventDown != null) {
|
||
|
|
EventDown(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnUp(){
|
||
|
|
if (EventUp != null) {
|
||
|
|
EventUp(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnClick(){
|
||
|
|
if (EventClick != null) {
|
||
|
|
EventClick(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool Pressed{
|
||
|
|
get{return pressed; }
|
||
|
|
}
|
||
|
|
|
||
|
|
// Use this for initialization
|
||
|
|
void Start () {
|
||
|
|
EventClick += new ButtonDelegate (clickDelegate);
|
||
|
|
}
|
||
|
|
|
||
|
|
void clickDelegate(Button sender){
|
||
|
|
}
|
||
|
|
|
||
|
|
bool PointInButton(Vector2 point){
|
||
|
|
return buttonRect.Contains(point);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update is called once per frame
|
||
|
|
void Update () {
|
||
|
|
buttonRect = new Rect (
|
||
|
|
transform.position.x - size.x / 2,
|
||
|
|
transform.position.y - size.y / 2,
|
||
|
|
size.x, size.y);
|
||
|
|
|
||
|
|
if (pressed) {
|
||
|
|
Vector2 releasePoint = Vector2.zero;
|
||
|
|
if (IsReleased (ref releasePoint)) {
|
||
|
|
if (PointInButton (releasePoint)) {
|
||
|
|
OnUp ();
|
||
|
|
OnClick ();
|
||
|
|
}
|
||
|
|
pressed = false;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
if(IsPressing()){
|
||
|
|
pressed = true;
|
||
|
|
OnDown();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool IsReleased(ref Vector2 point){
|
||
|
|
#if !UNITY_WP8 || UNITY_EDITOR
|
||
|
|
|
||
|
|
point = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
|
||
|
|
return Input.GetKeyUp (KeyCode.Mouse0);
|
||
|
|
#else
|
||
|
|
foreach (var t in Input.touches) {
|
||
|
|
if(t.fingerId == fingerId){
|
||
|
|
if((t.phase == TouchPhase.Canceled) || (t.phase == TouchPhase.Ended)){
|
||
|
|
point = new Vector2(t.position.x, Screen.height - t.position.y);
|
||
|
|
return true;
|
||
|
|
}else{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
point = new Vector2(-1, -1);
|
||
|
|
return true;
|
||
|
|
#endif
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
bool IsPressing(){
|
||
|
|
#if !UNITY_WP8 || UNITY_EDITOR
|
||
|
|
if (Input.GetKeyDown (KeyCode.Mouse0)) {
|
||
|
|
Vector2 position = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
|
||
|
|
return buttonRect.Contains(position);
|
||
|
|
}
|
||
|
|
#else
|
||
|
|
foreach(var t in Input.touches){
|
||
|
|
Vector2 position = new Vector2(t.position.x, Screen.height - t.position.y);
|
||
|
|
if(buttonRect.Contains(position)){
|
||
|
|
fingerId = t.fingerId;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnGUI(){
|
||
|
|
if (Event.current.type == EventType.Repaint) {
|
||
|
|
mat.SetColor("_TintColor", baseColor);
|
||
|
|
Graphics.DrawTexture (buttonRect, image, mat);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|