开发者

C# GUI Texture Button Script

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


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".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜