- Home /
Advanced Scripting Features
Hi everyone,
I am working on a project and I have a couple of scripts with variables that need often change for tweaking purposes. My question is this: how do I go about and display on screen the variables in a GUI? Where do I have to look for that, I am not sure how to search for this in documentation (hence this long question); On the same scripts in the Inspector tab I want to display images instead of text; This is just to replace a long list of items and make it nice for the developer. That coped with what's visible on screen I am hoping to create a nice education way of presenting various script setups.
Thanks!
Not a very good title, make it as descriptive as possible
Answer by TowerOfBricks · Dec 17, 2010 at 01:49 PM
To be able to display values in the game GUI and be able to change them is not very hard, here's an example (not tested)
public float myVariable = 5; public bool myOtherVariable = false;
public void OnGUI () { myVariable = GUILayout.HorizontalSlider (myVariable,0,100); myOtherVariable = GUILayout.Toggle (myOtherVariable); }
This will display the values on screen. There is no good way to display numbers-only fields in the game gui though, so I used a slider in the example instead.
To display stuff in the editor is a bit more tricky (or at least not as common or well documented) But you can read on here to get started. Extending The Editor - Unity Reference Manual
Hope it will help you get started.
Answer by slkjdfv · Dec 28, 2010 at 09:05 PM
this is another example on how to display a variable.
Using UnityEngine
Using System.Collections
public class exampleScript : MonoBehavior
{
public float myVariable = 5;
public void OnGUI () { GUILayout.Label((int)myVariable)
}
}
what (int) does is typecasts myVariable to an integer, in other words it just drops the decimal point and doesn't round. I find type casting comes in handy when you need a float and integers wont work or visa versa.
That is not how you cast, angle brackets are for generics. To remove decimal places you shouldn't cast anyway, ins$$anonymous$$d use $$anonymous$$athf.FloorToInt. Also you should format your code.
thank you i meant to put parenthesis, its all edited now.