Reputation: 15
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
Upvotes: 0
Views: 1344
Reputation: 709
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 ();
}
}
Upvotes: 1