• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ingen · Jul 28, 2012 at 08:40 PM · inspectorinfo

give info in inspector

hallo I wish have some info in the inspector about the script. like this

alt text

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 :)

info.png (10.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ingen · Jul 28, 2012 at 09:20 PM 0
Share

Thanks you!!! it is possible for Javascript?

where I type the message?

avatar image ScroodgeM · Jul 28, 2012 at 09:23 PM 0
Share

yes, it is. use link above - there's a lot of examples. extending editor is at the end of page.

avatar image Ingen · Jul 29, 2012 at 08:41 AM 0
Share

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

avatar image ScroodgeM · Jul 29, 2012 at 08:46 AM 0
Share

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

avatar image Ingen · Jul 31, 2012 at 04:31 AM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How does one inspect static vars? 3 Answers

Editor: multiple Inspectors 3 Answers

Getting the selected enum value set in the Inspector 2 Answers

WrapMode not visible in inspector 1 Answer

Singleton & Inspector 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges