Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C GUI纹理按钮脚本_C#_User Interface_Button_Unity3d - Fatal编程技术网

C# C GUI纹理按钮脚本

C# C GUI纹理按钮脚本,c#,user-interface,button,unity3d,C#,User Interface,Button,Unity3d,我终于找到了一个可以与IOS项目中的GUI按钮一起使用的脚本。我正在使用Unity3d游戏引擎。我对JavaScript按钮和动画有点熟悉,但对C一点也不熟悉。我的问题是不知道我们是否要编写一个函数,在触摸按钮时在C按钮脚本中播放排队的动画。下面是IOS按钮脚本的副本,然后是我必须播放排队动画的代码 using UnityEngine; using System.Collections; public enum Btn { normal, hover, armed }

我终于找到了一个可以与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
}
我假设排队的动画脚本片段;Input.GetButtondown…将更改为

void Update() {    
      if(Input.GetTouch("Btn"))
         animation.PlayQueued("shoot", QueueMode.PlayNow);
} 
并插入GUI按钮脚本的第148行。如果可以,请帮帮我,我很抱歉 情绪低落。认真地如果您能帮助重新格式化此脚本,我们将不胜感激 并用作模板,因为我还有另外两个GUI按钮要设置。它可能要求很多,或者天堂是为了什么

恭敬地

数字D

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

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

因此,如果您愿意,可以在JS中这样做:

public class ShootyMan : MonoBehaviour {
  public void Shoot() {
    animation.PlayQueued("shoot", QueueMode.PlayNow);
  }
}
现在,看看inspector中的按钮。我不确定您是否熟悉Unity中的消息传递工作方式,但基本上,该按钮将向messagee发送消息。它将在附加到messagee的任何脚本上调用name message函数。 使用shot功能将messagee设置为游戏对象。并将消息设置为字符串拍摄

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