question about GUI styles and skins

Hi,

I have two sample code:

The first one is a Icons.cs

using UnityEngine;
using System.Collections;

public class Icons {
	
	private GUISkin _firstSkin;
	private GUISkin _secondSkin;
	
	public GUISkin ReturnFirstSkin() {
		return _firstSkin;
	}
	
	public GUISkin ReturnSecondSkin() {
		return _secondSkin;
	}

	public Icons () {
		// Not sure how to set the skins
	}
}

And the second script file is GUIControl.cs

using UnityEngine;
using System.Collections;

public class GUIControl : Icons {
	
	public Icons _myIcons;
	public GUISkin _useThisStyle;
	
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnGUI() {
		
		_useThisStyle = _myIcons.ReturnFirstSkin();
		GUI.skin = _useThisStyle;
		GUI.Label( new Rect( 10, 10, 100, 100), "Test" );
		
		_useThisStyle = _myIcons.ReturnSecondSkin();
		GUI.skin = _useThisStyle;
		GUI.Label( new Rect( 10, 300, 100, 100 ), "Test #2" );
	}
}

I know how to create a GUI Skin in the project folder, but I do not know how to set a GUI Skin which is an asset in a project’s folder using ingame script. Since GUIControl inherits Icons, “_firstSkin” and “_secondSkin” does not show up in the inspector window, but only _myIcons and _useThisStyle shows up so I am not able to click and drag it into the the respective variables.

Is it possible to set a GUI Skin via ingame code? If not, what is the best way to achieve the same thing?

Mayb you can use somethign like “public var guiStyle : GUIStyle” ? http://unity3d.com/support/documentation/ScriptReference/GUIStyle.html

unfortunately GUIStyle doesn’t work either. If it is in a class file which is inherited, I have no idea how to set the GUIStyle or GUISkin that I want. Since I am trying to create different buttons for an RTS type game, this means that I would have to declare all my buildings/units icons which I want to use as a button in the GUIControl.cs instead of a Icons class which is inherited. Just means my code will be messier as I cannot seem to seperate it out.

Hello jacksmith

You can set a GUI Skin like this

// Only variables having "public" access modifier will show up in the inspector view except static variables

public GUISKin guiskin;

void OnGUI ()
{
  GUI.skin=guiskin; 

 //Whatsoever style you provide in your skin will reflect in your GUIs. Read the manual for different overloads of GUIs for more customization.
 GUI.Label( new Rect( 10, 10, 100, 100), "Test" );
}

There is a package viz. MenuPackage under my signature which may help you to understand GUIs better as
it implements GUI content, GUI style and other valuable functionalities which a new bie would need to learn.

Hi,

I was aware of that. I guess it is kinda hard to explain, so I will have to find some other way to do what I want. Thanks though.

Here is the sample copy of what I was trying to do:

http://members.iinet.net.au/~sanlitimiar/GUITest.zip

If you load that up as a project, and click on Main Camera, you will see that I have clicked and dragged GUIControl.cs onto the Main Camera.
If you look at Icons.cs, you will see that I have declared the following:

private GUISkin _firstSkin;
private GUISkin _secondSkin;

GuiControl inherits Icons class.

If you check the unity3d Inspector window for main camera, you should notice that _firstSkin and _secondSkin is not there for me to drag FirstIcon and SecondIcon (which are GUISkin assets in the Icons folder) onto. How will I assign my asset FirstIcon to _firstSkin? or is it not possible in inherited classes?

Then probably i misinterpreted your post.

Okay i went through your project and scripts as well. I changed the private members to public and the skins can now be assigned in the inspector view without changing the scenario. I mean you don’t need to assign “Icons.cs” script to any gameObject and at the same time you can assign your skins to _firstSkin and _secondSkin variables in the inspector view on GUIControl.cs script which is in turn inheriting Icons.cs.
I hope this would help you.

There are other ways to assign your skins. You can find out your skins via Resources.LoadAll() by keeping your skins under Resources folder and then assigning those to your variables but i think its kinda cumbersome process.

By the way could you please explain your purpose of using this scenario?

I was going to use it in a RTS game scenario. I would select something like an engineer, and it would load up all the icons of a building for example, and each icon will be a GUI.Button. The same would happen but units will show up instead if I select something like a factory instead of engineer.

Thanks for your help. I was not aware of the Resources.LoadAll() function. that would help me greatly.