2012年7月3日 星期二

UNITY3D - 多點觸控Button


Unity3D自帶的GUI元件並不支援多點觸控,但若針對每個要有多點觸控的Button額外寫程式也很煩人,於是寫了這隻程式,使用方法跟GUI.Button一樣,只是改成用CustomGUI.Button。


public class CustomGUI {

public static bool Button (Rect position, string text) {
GUI.Button(position, text);
position.y = Screen.height - position.y - position.height;

if(Input.GetMouseButtonUp(0) && position.Contains(Input.mousePosition))
{
return true;
}

foreach (Touch touch in Input.touches) {
            if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
if(position.Contains(touch.position))
{
return true;
}
}
}

return false;
}

public static bool Button (Rect position, Texture image) {
GUI.Button(position, image);
position.y = Screen.height - position.y - position.height;

if(Input.GetMouseButtonUp(0) && position.Contains(Input.mousePosition))
{
return true;
}

foreach (Touch touch in Input.touches) {
            if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
if(position.Contains(touch.position))
{
return true;
}
}
}

return false;
}

public static bool Button (Rect position, GUIContent content) {
GUI.Button(position, content);
position.y = Screen.height - position.y - position.height;

if(Input.GetMouseButtonUp(0) && position.Contains(Input.mousePosition))
{
return true;
}

foreach (Touch touch in Input.touches) {
            if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
if(position.Contains(touch.position))
{
return true;
}
}
}

return false;
}

public static bool Button (Rect position, string text, GUIStyle style) {
GUI.Button(position, text, style);
position.y = Screen.height - position.y - position.height;
if(Input.GetMouseButtonUp(0) && position.Contains(Input.mousePosition))
{
return true;
}

foreach (Touch touch in Input.touches) {
            if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
if(position.Contains(touch.position))
{
return true;
}
}
}

return false;
}

public static bool Button (Rect position, Texture image, GUIStyle style) {
GUI.Button(position, image, style);
position.y = Screen.height - position.y - position.height;

if(Input.GetMouseButtonUp(0) && position.Contains(Input.mousePosition))
{
return true;
}

foreach (Touch touch in Input.touches) {
            if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
if(position.Contains(touch.position))
{
return true;
}
}
}

return false;
}

public static bool Button (Rect position, GUIContent content, GUIStyle style) {
GUI.Button(position, content, style);
position.y = Screen.height - position.y - position.height;

if(Input.GetMouseButtonUp(0) && position.Contains(Input.mousePosition))
{
return true;
}

foreach (Touch touch in Input.touches) {
            if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
if(position.Contains(touch.position))
{
return true;
}
}
}

return false;
}
}

沒有留言:

張貼留言