x


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!

more ▼

asked Dec 17 '10 at 12:10 PM

Airship Games gravatar image

Airship Games
257 49 69 87

Not a very good title, make it as descriptive as possible

Dec 17 '10 at 01:42 PM TowerOfBricks
(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

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.

more ▼

answered Dec 17 '10 at 01:49 PM

TowerOfBricks gravatar image

TowerOfBricks
3.2k 17 25 50

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 28 '10 at 09:05 PM

slkjdfv gravatar image

slkjdfv
330 24 28 32

That is not how you cast, angle brackets are for generics. To remove decimal places you shouldn't cast anyway, instead use Mathf.FloorToInt. Also you should format your code.

Dec 28 '10 at 09:39 PM _Petroz

thank you i meant to put parenthesis, its all edited now.

Dec 30 '10 at 06:41 AM slkjdfv
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5276
x3816
x356
x351

asked: Dec 17 '10 at 12:10 PM

Seen: 1135 times

Last Updated: Dec 17 '10 at 01:50 PM