C#GUI纹理按钮脚本

时间:2011-10-04 09:05:46

标签: c# user-interface button unity3d

我终于找到了一个可以与IOS项目中的GUI Button一起使用的脚本。我正在使用Unity3d游戏引擎。我对JavaScript按钮和动画有点熟悉,但对C#并不熟悉。我的问题是不知道当触摸按钮时编写将在C#按钮脚本中播放排队动画的函数。下面是IOS按钮脚本的副本,然后是我必须播放排队动画的代码。

using UnityEngine;
using System.Collections;

public enum Btn
{
    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
}

这大部分似乎都处理按钮状态,我的触摸按钮只有一个状态,即'正常'。应该删除引用“悬停”和武装吗?我在控制台中也收到错误说; “无法找到类型或命名空间”按钮状态“。您是否缺少using指令或程序集引用?”

C#GUI按钮播放排队动画的代码我要插入如下所示的读取:

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Update() {
        if (Input.GetButtonDown("Btn"))
            animation.PlayQueued("shoot", QueueMode.PlayNow);

    }
}

我想是排队的动画脚本片段; Input.GetButtondown ....将改为

void Update() {    
      if(Input.GetTouch("Btn"))
         animation.PlayQueued("shoot", QueueMode.PlayNow);
} 

并插入GUI Button脚本的第148行。如果可以,请帮助我,我 心情失落。认真!任何帮助重新格式化此脚本将不胜感激 并用作模板,因为我有两个其他GUI按钮来设置。它可能要求很多或者天堂是什么?

此致,

Digital D

一个模拟人, 在数字世界中

1 个答案:

答案 0 :(得分:0)

好的,脚本中有很多内容,但它看起来是设计的,所以你不必修改它。 您需要做的是转到将要播放动画的对象,并在其上创建一个您希望在单击按钮时调用的函数。

就像(如果你愿意,你可以在JS中这样做):

public class ShootyMan : MonoBehaviour {
  public void Shoot() {
    animation.PlayQueued("shoot", QueueMode.PlayNow);
  }
}

现在,查看检查器中的按钮。我不确定你是否熟悉Unity中的消息传递方式,但基本上按钮会向“messagee”发送“消息”。 (即,它将在附加到messagee的任何脚本上调用name消息的函数)。 使用“拍摄”功能将“messagee”设置为GameObject。并将消息设置为字符串“Shoot”。