- Home /
Simple headline / title for a Script in the Inspector
Hey,
imagine I have the following code:
public bool There;
public bool are;
public float a;
public int lot
public int of
public _________1;
public GameObject variables;
public Transform in;
public float my;
public ray Script;
This is the way I do a kind of headline / tile atm to get the code a little more clearly to see. I know that I should do this with the editorGUI, but I have to do so much more extra stuff for it.
Isn't there a simple way to just put a text / headline / title / label somwhere between the variables that are listed from a script in the Inspector without all that editorGUI stuff?
Answer by Stardog · Nov 17, 2014 at 01:06 AM
The answer here is no longer true. There are new PropertyDrawers/DecoratorDrawers in Unity 4.X (4.5?).
http://va.lent.in/interesting-things-in-unity-4-5-you-probably-didnt-know-about/
[Header("Hi there!")]
public string TheHeader = "Header!";
[Tooltip("This is THE VALUE!")]
[ContextMenuItem("Reset", "resetTheValue")]
public float TheValue = 42.0f;
[Space(50)]
public string TheString = "THE STRING";
[ContextMenu("Reset The Value")]
private void resetTheValue()
{
TheValue = 42;
}
Answer by Bilelmnasser · Jun 06, 2014 at 08:11 PM
yes you can, editor take custom class as 1 element and group her element under one group, but the class need to be serializable, that he can do save your in data in inspector. you can simply using your logical grouping of element organize your prefabs/objects/variables in public serializable classes like the code below attach it to any gameobject in your scene, inspector show organized elements in logical groups :)
using UnityEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
[Serializable]
public class person{
[SerializeField]
public GameObject Head ;
[SerializeField]
public Color SkinColor;
[SerializeField]
public GameObject[] BodyParts;
}
[Serializable]
public class world{
[SerializeField]
public GameObject center ;
[SerializeField]
public Transform terrain;
[SerializeField]
public GameObject[] WorldObjets;
}
public class NewBehaviourScript : MonoBehaviour {
public world WorldPrefab;
public person [] PersonPrefabs=new person[100];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Answer by Piflik · Jul 17, 2012 at 11:38 AM
In Javascript you can do something like this.
class ScriptNameAnything {
//variable declaration here
}
These are not really headlines, but a way to organize your variables in different groups. See the CharacterMotor script in the Character Controller asset package.
Not sure if/how this can be done in C#, though.
you mean simple comments, don't you?
Thx but I'm already using comments =D (I think I wouldn't even had to install unity if I didn't know how to use comments ^^ ) But I wanted something to see in the Inspector not in $$anonymous$$onoDevelop.
Still thx!
No, not comments, classes. If you have a look at the character$$anonymous$$otor script, you will see that it has several groups in the Inspector that can be opened and closed to reveal the variables that are part of that class.
I'll give you an example. The script itself doesn't do much (since it is just an abandoned test script), but when you add this to any object, you will see an expandable group called 'Variables' in the Inspector.
#pragma strict
class FPS_ControllerVariables {
var movSpeed : float;
var rotSpeed : float;
var gravity : float = 10.0;
var maxVelocityChange : float = 10.0;
}
var variables : FPS_ControllerVariables = FPS_ControllerVariables();
private var isGrounded : boolean = false;
function Awake() {
rigidbody.freezeRotation = true;
rigidbody.useGravity = false;
}
function FixedUpdate() {
if(isGrounded) {
var targetVelocity = transform.TransformDirection(Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))) * variables.movSpeed;
var velocity = rigidbody.velocity;
var velocityChange = targetVelocity - velocity;
velocityChange.x = $$anonymous$$athf.Clamp(velocityChange.x, -variables.maxVelocityChange, variables.maxVelocityChange);
velocityChange.y = 0;
velocityChange.z = $$anonymous$$athf.Clamp(velocityChange.z, -variables.maxVelocityChange, variables.maxVelocityChange);
rigidbody.AddForce(velocityChange, Force$$anonymous$$ode.VelocityChange);
//rigidbody.velocity = transform.TransformDirection((Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))) * movSpeed);
//transform.Translate(Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")) * movSpeed * Time.deltaTime);
transform.Rotate(Vector3(0, Input.GetAxis("$$anonymous$$ouse X"), 0) * variables.rotSpeed);
if(Input.GetButtonDown("Jump")) {
rigidbody.velocity.y = 20;
}
}
rigidbody.AddForce(Vector3(0, -variables.gravity * rigidbody.mass, 0));
isGrounded = false;
}
function OnCollisionStay() {
//if(floor.gameObject.tag == "floor") {
isGrounded = true;
//}
}
True, same for c-sharp. But you need to make that extra "values-container"-class Serializable though. Else Unity will not show those values in the inspector. Anyway in that case you are creating some ($$anonymous$$imal, but extra) overhead at runtime since it will need to do an extra lookup every time you access a value of that extra class, which is not optimal.
runtime?
I just want to have a more clearly variable list while making games =D