0

I am trying to add controls through mouse in place of keyboard in the game. I added 4 movement keys and 1 fire button through the GUI texture in unity. In the game there is already a player controller which controls the player through keyboard strokes

I didnt get, how to make the player move if the direction button (GUITexture) is clicked

Button Script

using UnityEngine; using System.Collections;

public class RightButton : MonoBehaviour {

public Texture2D bgTexture;
public Texture2D airBarTexture;
public int iconWidth = 32;
public Vector2 airOffset = new Vector2(10, 10);


void start(){
    }

void OnGUI(){
    int percent = 100;

    DrawMeter (airOffset.x, airOffset.y, airBarTexture, bgTexture, percent);
}

void DrawMeter(float x, float y, Texture2D texture, Texture2D background, float percent){
    var bgW = background.width;
    var bgH = background.height;

    GUI.DrawTexture (new Rect (x, y, bgW, bgH), background);

    var nW = ((bgW - iconWidth) * percent) + iconWidth;

    GUI.BeginGroup (new Rect (x, y, nW, bgH));
    GUI.DrawTexture (new Rect (0, 0, bgW, bgH), texture);
    GUI.EndGroup ();


}

}

I am unable to add the GUI Button in place of GUI.DrawTexture, its giving invalid argument error and so i am unable to add how to check the button is clicked or not

Thanks

1 Answer 1

1

GUITexture is part of the legacy GUI system. An example of how to get this to work as a button is here.

using UnityEngine;
using System.Collections;

public class RightButton : MonoBehaviour {

    public Texture bgTexture;
    public Texture airBarTexture;
    public int iconWidth = 32;
    public Vector2 airOffset = new Vector2(10, 10);


    void start(){
    }

    void OnGUI(){
        int percent = 100;

        DrawMeter (airOffset.x, airOffset.y, airBarTexture, bgTexture, percent);
    }

    void DrawMeter(float x, float y, Texture texture, Texture background, float percent){
        var bgW = background.width;
        var bgH = background.height;

        if (GUI.Button (new Rect (x, y, bgW, bgH), background)){
            // Handle button click event here
        }

        var nW = ((bgW - iconWidth) * percent) + iconWidth;

        GUI.BeginGroup (new Rect (x, y, nW, bgH));
        GUI.DrawTexture (new Rect (0, 0, bgW, bgH), texture);
        GUI.EndGroup ();
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

I am unable to change my code in GUI Button its giving error
If the error is because of the last argument, with the background texture parameter, redefine the background type to Texture instead of Texture2D and see if that works. For example: public Texture2D bgTexture; public Texture2D airBarTexture; public int iconWidth = 32; public Vector2 airOffset = new Vector2(10, 10); void start(){.... (Code formatting doesn't seem to work in comments)
I ran out of time messing with the code formatting. Change any reference to "Texture2D" to "Texture"
Thanks it worked, but how could i make it to work for when ever the mouse is pressed and not have to keep clicking again and again
and i wanted the event in the button to be inherited from player controller which is giving error "can`t find game object"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.