- Home /
give info in inspector
hallo I wish have some info in the inspector about the script. like this
I took a look in folder, to learn how is the script but have not found
how I can do it?
thanks to all can help me :)
Answer by ScroodgeM · Jul 28, 2012 at 08:46 PM
http://docs.unity3d.com/Documentation/Components/gui-ExtendingEditor.html
example
using UnityEngine; using UnityEditor; using System.Collections; using System.Collections.Generic; [CustomEditor(typeof(MyClass))] public class MyClassEditor: Editor { public override void OnInspectorGUI() { GUI.color = Color.yellow; EditorGUILayout.HelpBox(target.GetType().ToString(), MessageType.Warning, true); GUI.color = Color.white; } }
edit
first you need a MonoBehaviour (AKA Component, Script, Class) for object. let's sat i have a Bullet Component that needs bullet prefab attached and on starts it instantiates one instance of it and gives it a speed 10 to forward. to give a speed we need first to check if Rigidbody component is attached to bullet.
Bullet.cs
using UnityEngine; using System.Collections; public class Bullet : MonoBehaviour { public GameObject bulletGameObject; void Start() { if (bulletGameObject.rigidbody) { GameObject newBullet = Instantiate(bulletGameObject) as GameObject; newBullet.rigidbody.velocity = newBullet.transform.forward * 10f; } } }
you already can attach this script to any GameObject in scene and use.
but now we need a custom editor with a warning message about rigidbody. let's do it.
this script first associated with Bullet Component - it means this is inspector for Bullet component only (line 5). now, in method OnInspectorGUI (that draws inspector) we write our own controls. first we take a 'target' (this is always a Component we edit with inspector) as draw a field to select GameObject (same as simple public reference in Component).
and here's the main part.
we check if selected gameObject has a rigidbody component, and draw info 'OK' or error 'Not OK' message
BulletInspector.cs
using UnityEngine; using UnityEditor; using System.Collections; using System.Collections.Generic; [CustomEditor(typeof(Bullet))] public class BulletInspector : Editor { public override void OnInspectorGUI() { Bullet b = (Bullet)target; b.bulletGameObject = EditorGUILayout.ObjectField("bullet GO", b.bulletGameObject, typeof(GameObject), true) as GameObject; if (b.bulletGameObject) { if (!b.bulletGameObject.rigidbody) { GUI.color = new Color(1f, 0.5f, 0.5f, 1f); EditorGUILayout.HelpBox("object must have a rigidbody component", MessageType.Error, true); } else { GUI.color = new Color(0.5f, 1f, 0.5f, 1f); EditorGUILayout.HelpBox("object have a rigidbody component", MessageType.Info, true); } GUI.color = Color.white; }
}
}
IMPORTANT
don't forget to place last script and all other editor scripts in 'Editor' folder. or else your project won't compile.Thanks you!!! it is possible for Javascript?
where I type the message?
yes, it is. use link above - there's a lot of examples. extending editor is at the end of page.
I have tried it but get this errors
.../$$anonymous$$yClassEditor.cs(8,17): warning CS0114: $$anonymous$$yClassEditor.OnInspectorGUI()' hides inherited member
UnityEditor.Editor.OnInspectorGUI()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword
.../$$anonymous$$yClassEditor.cs(5,22): error CS0246: The type or namespace name $$anonymous$$yClass ' could not be found. Are you missing a using directive or an assembly reference? .../$$anonymous$$yClassEditor.cs(5,2): error CS1502: The best overloaded method match for
UnityEditor.CustomEditor.CustomEditor(System.Type) ' has some invalid arguments
.../$$anonymous$$yClassEditor.cs(5,2): error CS1503: Argument #1' cannot convert
object' expression to type `System.Type'
and I'm not sure is what I need, please take look at pic
i'd fix one mistake in answer. and be sure you have a class that this inspector created for. in my example this should be $$anonymous$$onoBehaviour named $$anonymous$$yClass
I try this but say `$$anonymous$$yClass' could not be found
sorry but I'm anought noob, how I can create?
I also look the link to do this in JS but not understund how do it
and readig the it is very usefull but have som questions
where I write my comment? it have boolean for Warning 'true', like I write about a var i.e. 'rigidbody is needed' and the worning disappear when rigidbody is added, is correct?
how I say it's awfull and over my need, I only need somethink like this
var bullet : gameObject;
___________________
| the bullet |
| need a rigidbody |
| or not work |
|________________$$anonymous$$
and this appear in the insector after the script is added to object
thanks to you for all the help and $$anonymous$$ching