How to convert GUI Texture in Button??

Hi,

I am a beginner learning the c#, trying to make GUI Texture behave like the default GUI Button of unity.

I have already made the GUI Texture assign as public variable(Dragged Texture in it) in script but now what next :face_with_spiral_eyes:

How to use like we are use default button by ā€œif (GUI.Button(new Rectā€¦ā€??

Also I am using state machine calling this script from another state script.

Thanks In advance…

You could do this:

using UnityEngine;
using System.Collections;

public class GUI_Menu : MonoBehaviour {

	//Variable Start

	public GUISkin customSkin;

    }

void OnGUI()
	{
		GUI.skin = customSkin;
    }

Then you could just use the unity GUI to add a texture to a button.

Thank…But stuck again…

I have added this code in GameData script which is attached to GameManager object I want this button in my BeginState script. Now I am adding a button using" if (GUILayout.Button(manager.gameDataRef.customSkin))" in BeginState… I am getting compiler errors as

Assets/Code/States/BeginState.cs(32,39): error CS1502: The best overloaded method match for `UnityEngine.GUILayout.Button(UnityEngine.Texture, params UnityEngine.GUILayoutOption[ ])’ has some invalid arguments

Assets/Code/States/BeginState.cs(32,39): error CS1503: Argument #1' cannot convert UnityEngine.GUISkin’ expression to type `UnityEngine.Texture’

Please tell me the correct method to achieve this…

https://www.youtube.com/watch?v=CfqM55K0rfU

Check this tut out on GUI skin. This may help.

OK…I have got it…used GUIStyle…It worked…

also learning how to get information from Scripting Reference…:slight_smile:

Thanks…had seen that video but I stuck at how to write a GUI.Button or GUILayout etc…to call that from another script…

yep GUIStyle would be another way. I use both in my project so that I can different button with different textures.

OK…now hit with another problem…:frowning:

Button works perfectly in editor…but when I connect with remote in android phone…I found a problem that button works only if my mouse pointer is on the button in editor…now whats that:face_with_spiral_eyes:

If you want to have a lot of custom buttons, each of them using a different texture, create some GUITextures, then use on script attached to them : OnMouseDown, OnMouseEnter and OnMouseExit functions. attach the script to the GUITexture, create prefab and instantiate/destroy as needeed.

Can you please tell me that how to write code in OnMouseDown() call it from another script in "if " statement:face_with_spiral_eyes:??

You can’t call OnMouseDown from another script. On mouse down is called if the mouse button is pressed and that the mouse cursor is on the GUITexture that contain the OnMouseDown function. But you can put some ā€œifā€ statement inside the OnMouseDown function Or call functions, that can be static if you want them in another script, if you want the said button to work only if something. Or you can instantiate the button only if something (from another script) and destroy it when you don’t want it on screen.
But to be honest, i don’t understand why you would like to call OnMouseDown from another script. And you can’t.

function OnMouseDown(){
     if(something){
     DoSomething();
     }
}

Actually I am working in state machine so I have all splashscreens, buttons in one script which I want to use in another states scripts …I am using splashscreens, GUI.Button using dot syntax but I am not sure about your idea of attaching OnMoseDown to GUITexture using it wherever required:face_with_spiral_eyes: