C# GUI 纹理按钮脚本

发布于 2024-12-08 03:36:07 字数 5406 浏览 0 评论 0原文

我终于找到了一个可以在 IOS 项目中与 GUI 按钮一起使用的脚本。我正在使用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
}

其中大部分似乎与按钮状态有关,其中我的触摸按钮只有一种状态,即“正常”。是否应该删除“悬停”和武装的提及?我还在控制台中收到错误消息: “找不到类型或命名空间“Button State”。您是否缺少 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 按钮脚本的第 148 行左右。如果可以的话请帮助我,我是 心情低落。严重地!任何重新格式化此脚本的帮助将不胜感激 并用作模板,因为我还有另外两个 GUI 按钮需要设置。也许它要求很多,或者天堂有什么用?

尊敬的

Digital D

模拟人, 在数字世界中

I've finally found a script that can be used with a GUI Button in an IOS project. I am using Unity3d game engine. I'm a little familiar with JavaScript buttons and animation but am not familiar at all with C#. My problem is not knowing were to write the function that will play a queued animation in the C# button script when the button is touched. Below is copy of the IOS Button Script and then the code I have to play the queued animation.

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
}

Most of this seems to deal with button states, where my touch button only has one state, which is 'normal'. Should references to 'hover' and armed just be deleted? I also get an error in the console saying; "the type or namespace "Button State" could not be found. Are you missing a using directive or an assembly reference?"

The code for C# GUI Button play queued animation I want to insert reads like this:

using UnityEngine;
using System.Collections;

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

    }
}

I suppose of queued animation script snippet; Input.GetButtondown....would change to

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

and be inserted at about line 148 of the GUI Button script. Please help me if you can, I'm
feeling down. Seriously! Any help in reformatting this script would be greatly appreciated
and used as a template as I have two other GUI buttons to setup. It maybe asking a lot or what's a heaven for?

Respectfully,

Digital D

an analog man,
in a digital world

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

负佳期 2024-12-15 03:36:07

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

所以类似(如果您愿意,您可以在 JS 中执行此操作):

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

现在,查看检查器中的按钮。我不确定您是否熟悉 Unity 中消息传递的工作原理,但基本上按钮会向“消息接收者”发送“消息”。 (即,它将在附加到消息的任何脚本上调用名称消息的函数)。
使用“射击”功能将“消息接收者”设置为游戏对象。并将消息设置为字符串“Shoot”。

Okay, there's a lot going on in the script, but it appears to be designed so you don't have to modify it.
What you need to do is go to the object that will be playing the animation, and create a function on it that you wish to be called when the button is clicked.

So something like (you can do this in JS if you like):

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

Now, look at the button in the inspector. I'm not sure if you're familiar with how messaging works in Unity, but basically the button will send a "message" to a "messagee". (ie. it will call a function of name message on any script attached to the messagee).
Set the "messagee" to the GameObject with the "Shoot" function. And set the message to the string "Shoot".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文