【问题标题】:C# GUI Texture Button ScriptC# GUI 纹理按钮脚本
【发布时间】:2011-11-30 12:37:06
【问题描述】:

我终于找到了一个可以在 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);

    }
}

我想是排队动画脚本 sn-p; Input.GetButtondown....将更改为

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

并插入到 GUI Button 脚本的大约第 148 行。如果可以的话请帮助我,我是 情绪低落。严重地!任何重新格式化此脚本的帮助将不胜感激 并用作模板,因为我还有另外两个 GUI 按钮要设置。它可能要求很多,或者天堂有什么用?

尊敬的,

数字D

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

【问题讨论】:

    标签: c# user-interface button unity3d


    【解决方案1】:

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

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

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

    现在,查看检查器中的按钮。我不确定您是否熟悉 Unity 中消息传递的工作方式,但基本上该按钮会将“消息”发送给“消息接收者”。 (即,它将在附加到消息的任何脚本上调用名称消息的功能)。 使用“Shoot”功能将“messagee”设置为 GameObject。并将消息设置为字符串“Shoot”。

    【讨论】:

      猜你喜欢
      • 2014-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多