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
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
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; } }
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.
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
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; } } }