블로그 이미지
Every unexpected event is a path to learning for you. blueasa

카테고리

분류 전체보기 (2797)
Unity3D (853)
Programming (479)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (185)
협업 (61)
3DS Max (3)
Game (12)
Utility (68)
Etc (98)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (55)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday

InGame Button

Unity3D/Script / 2012. 12. 14. 22:27
추가 : 내가 잘못 쓰는지는 모르겠지만, 게임 내 어디에 있냐에 따라서 클릭이 되고 안되고 해서..좀 더 알아봐야 될 것 같다..

Button

Author: Jonathan Czeck (aarku)

Contents

 [hide

Description

This script uses a GUITexture and Unity mouse events to implement a regular push button that behaves properly like Mac OS X.

Warning: As of Unity 3.0.0, this script does not work on iOS devices, as OnMouseUp functions and the like do not work on iOS devices. Use the iPhone compatible version further below.

Usage

Attach this script to a GUITexture object. Add a ButtonPressed function to the object pointed to by the messageevariable to catch when the button has been pressed. (You can change the name of the function by changing themessage variable.)

JavaScript - Button.js

 var normalTexture : Texture;
 var hoverTexture : Texture;
 var pressedTexture : Texture;
 var messagee : GameObject;
 var message = "ButtonPressed";
 
 private var state = 0;
 private var myGUITexture : GUITexture;
 
 myGUITexture = GetComponent(GUITexture); 
 
 function OnMouseEnter()
 {
    state++;
    if (state == 1)
        myGUITexture.texture = hoverTexture;
 }
 
 function OnMouseDown()
 {
    state++;
    if (state == 2)
        myGUITexture.texture = pressedTexture;
 }
 
 function OnMouseUp()
 {
    if (state == 2)
    {
        state--;
        if (messagee)
            messagee.SendMessage(message, gameObject);
    }
    else
    {
        state --;
        if (state < 0)
            state = 0;
    }
    myGUITexture.texture = normalTexture;
 }
 
 function OnMouseExit()
 {
    if (state > 0)
        state--;
    if (state == 0)
        myGUITexture.texture = normalTexture;
 }

C# - Button.cs

The C# version is a bit different from the JS version. It shows off some new features in Unity 1.2 for automatically attaching the GUITexture component when attaching the Button (using the RequireComponent attribute) and placing the Button in a custom submenu in the Components menu (using the AddComponentMenu attribute.)

It also groups the textures together into a separate object and has a separate method for changing the texture to ease customisation. Note that some properties and methods are marked protected and/or virtual to make it possible to customise the Button class by creating a new subclass. See the ToggleButton snippet for an example on how to subclass the Button class.

using UnityEngine;
using System.Collections;
 
public enum ButtonState {
    normal,
    hover,
    armed
}
 
[System.Serializable] // Required so it shows up in the inspector 
public class ButtonTextures {
    public Texture normal=null;
    public Texture hover=null;
    public Texture armed=null;
    public ButtonTextures() {}
 
    public Texture this [ButtonState state] {
        get {
            switch(state) {
                case ButtonState.normal:
                    return normal;
                case ButtonState.hover:
                    return hover;
                case ButtonState.armed:
                    return armed;
                default:
                    return null;
            }
        }
    }
}
 
 
[RequireComponent(typeof(GUITexture))]
[AddComponentMenu ("GUI/Button")]    
public class Button : MonoBehaviour {
 
    public GameObject messagee;
    public string message = "ButtonPressed";
    public ButtonTextures textures;
 
    protected int state = 0;
    protected GUITexture myGUITexture;
 
    protected virtual void SetButtonTexture(ButtonState state) {
        myGUITexture.texture=textures[state];
    }
 
    public virtual void Reset() {
        messagee = gameObject;
        message = "ButtonPressed";
    }
 
    public virtual void Start() {
        myGUITexture = GetComponent(typeof(GUITexture)) as GUITexture; 
        SetButtonTexture(ButtonState.normal);
    }
 
    public virtual void OnMouseEnter()
    {
        state++;
        if (state == 1)
            SetButtonTexture(ButtonState.hover);
    }
 
    public virtual void OnMouseDown()
    {
        state++;
        if (state == 2)
            SetButtonTexture(ButtonState.armed);
    }
 
    public virtual void OnMouseUp()
    {
        if (state == 2)
        {
            state--;
            if (messagee != null)
                messagee.SendMessage(message, this);
        }
        else
        {
            state --;
            if (state < 0)
                state = 0;
        }
        SetButtonTexture(ButtonState.normal);
    }
 
    public virtual void OnMouseExit()
    {
        if (state > 0)
            state--;
        if (state == 0)
            SetButtonTexture(ButtonState.normal);
    }
}


C# - Button.cs (iPhone Compatible)

Same as the C# version above, but added support for recognizing touch input from iPhone or Android devices. Also adds callback function for when the button is double-clicked/double-tapped.

using UnityEngine;
using System.Collections;
 
public enum ButtonState
{
    normal,
    hover,
    armed
}
 
[System.Serializable] // Required so it shows up in the inspector 
public class ButtonTextures
{
    public Texture normal=null;
    public Texture hover=null;
    public Texture armed=null;
    public ButtonTextures() {}
 
    public Texture this [ButtonState state]
	{
        get
		{
            switch(state)
			{
                case ButtonState.normal:
                    return normal;
                case ButtonState.hover:
                    return hover;
                case ButtonState.armed:
                    return armed;
                default:
                    return null;
            }
        }
    }
}
 
 
[RequireComponent(typeof(GUITexture))]
[AddComponentMenu ("GUI/Button")]    
public class GuiButton : MonoBehaviour
{
    public GameObject messagee;
    public string message = "";
	public string messageDoubleClick = "";
    public ButtonTextures textures;
 
    protected int state = 0;
    protected GUITexture myGUITexture;
 
	private int clickCount = 1;
	private float lastClickTime = 0.0f;
	static private float doubleClickSensitivity = 0.5f;
 
    protected virtual void SetButtonTexture(ButtonState state)
	{
		if (textures[state] != null)
		{
        	myGUITexture.texture = textures[state];
		}
    }
 
    public virtual void Reset()
	{
        messagee = gameObject;
        message = "";
		messageDoubleClick = "";
    }
 
	public bool HitTest(Vector2 pos)
	{
		return myGUITexture.HitTest(new Vector3(pos.x, pos.y, 0));
	}
 
    public virtual void Start()
	{
        myGUITexture = GetComponent(typeof(GUITexture)) as GUITexture; 
        SetButtonTexture(ButtonState.normal);
    }
 
    public virtual void OnMouseEnter()
    {
        state++;
        if (state == 1)
            SetButtonTexture(ButtonState.hover);
    }
 
    public virtual void OnMouseDown()
    {
        state++;
        if (state == 2)
            SetButtonTexture(ButtonState.armed);
    }
 
    public virtual void OnMouseUp()
    {
		if (Time.time - lastClickTime <= doubleClickSensitivity)
		{
			++clickCount;
		}
		else
		{
			clickCount = 1;
		}
 
        if (state == 2)
        {
            state--;
			if (clickCount == 1)
			{
				if (messagee != null && message != "")
				{
					messagee.SendMessage(message, this);
				}
			}
			else
			{
				if (messagee != null && messageDoubleClick != "")
				{
					messagee.SendMessage(messageDoubleClick, this);
				}
			}
        }
        else
        {
            state --;
            if (state < 0)
                state = 0;
        }
        SetButtonTexture(ButtonState.normal);
		lastClickTime = Time.time;
    }
 
    public virtual void OnMouseExit()
    {
        if (state > 0)
            state--;
        if (state == 0)
            SetButtonTexture(ButtonState.normal);
    }
 
#if (UNITY_IPHONE || UNITY_ANDROID)
	void Update()
	{
		int count = Input.touchCount;
		for (int i = 0; i < count; i++)
		{
			Touch touch = Input.GetTouch(i);
			if (HitTest(touch.position))
			{
				if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
				{
					SetButtonTexture(ButtonState.normal);
				}
				else
				{
					SetButtonTexture(ButtonState.armed);
				}
				if (touch.phase == TouchPhase.Began)
				{
					if (touch.tapCount == 1)
					{
						if (messagee != null && message != "")
						{
							messagee.SendMessage(message, this);
						}
					}
					else if (touch.tapCount == 2)
					{
						if (messagee != null && messageDoubleClick != "")
						{
							messagee.SendMessage(messageDoubleClick, this);
						}
					}
				}
				break;
			}
		}
	}
#endif
}



출처 : http://wiki.unity3d.com/index.php/Button

반응형
Posted by blueasa
, |