Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Firedan1176 · Jun 26, 2015 at 02:39 PM · editorinspectorcustom-inspector

What should be handled in the custom inspector version of a script?

So if I have 2 scripts, Enemy and EnemyEditor, and "Enemy.cs" contains this (which is just an example):

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
 
 public float someFloat
 public GameObject someGameobject;
 
     void Update() {
     Debug.Log(someGameobject);
     }
 
 
 
 }


What should the editor version for the inspector contain?

This is what it currently looks like:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor(typeof(Enemy))]
 public class EnemyEditor : Editor {
 
     float editorFloat;
     gameObject editorGameobject;
     Enemy myEnemy;
 
     public override void OnInspectorGUI() {
     myEnemy = (Enemy)target;
     editorFloat = myEnemy.someFloat;
     editorGameobject = myEnemy.someGameobject;
 
 
 
     myEnemy.someGameobject = GameObjectField(myEnemy.someEnemy, true);
     myEnemy.someFloat = FloatField(myEnemy.someFloat);
 
     }
 
 }

My question is: Should the editor scripts only contain code for modifying the LOOK of the inspector, or can it contain all the functions in update, and what actually does stuff to the character?

And if the editor script is just for changing the way it looks, what should the code look like in both? I don't understand SerializedObjects, if that's important. If you could, please try to explain since I'm not familiar with this.

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
0

Answer by ModLunar · Jun 28, 2018 at 03:19 AM

Generally speaking, your editor scripts should be mostly to make life easier when trying to edit that object, or create/modify assets. As Unity's class you derived from, Editor, is part of the UnityEditor namespace, anything using that class, Editor, cannot be included in a standalone build. Meaning anyone you send your game to who doesn't use Unity like we do won't be able to see any of your editor script stuff, since your EnemyEditor (for this example) is only available in the full Unity editor.

So as with other UnityEditor classes and functions, be careful that your game doesn't rely on them to function normally if you want to build your project to play outside of Unity. Note that you can use "preprocessor directives" to tell the compiler to include/exclude certain parts of code in certain situations. In this case, you can wrap code with this: (literally any code, it can be outside functions, outside classes, etc.)

#if UNITY_EDITOR
...
#endif

So that will allow you to use editor code inside there, because any code inside of there will not be included in any builds -- it will only be available in the Unity editor. Sorry if that sounds like a broken record xD




Ah yes, Unity's serialization system (way of saving data) is complicated yet simple -- a long conversation from the beginning for sure though. It's based on SerializedObjects and SerializedProperties, and the editor knows how to draw all sorts of basic types using PropertyFields (Just about all classes inheriting from UnityEngine.Object (this includes MonoBehaviours, EventTriggers, ScriptableObjects, etc.) are able to have references to them saved. Others types like custom classes of our own, and structs -- these serialize inline, so you won't see a Vector3 reference field for example -- the values are serialized inline with your MonoBehaviour for example, where the inspector shows not a Vector3 reference field, but literally just x, y and z (float) fields. Serialization happens constantly in the inspector, as it typically serializes the data before displaying it. This is why the attribute [SerializeField] causes stuff to show in the inspector -- as it's being included in the editor's serialization of that object of yours, and then the inspector will show all the serialized (saved) data belonging to it.

There are quirks and limitations to Unity's serialization though. Also note that you can implement ISerializationCallbackReceiver (an interface in the UnityEngine) to slightly control how Unity serializes/deserializes (saves and loads) your classes and structs. You probably won't (I didn't) understand it all at first, and you may be able to get away with not really caring about Unity's serialization for your inspector. But it's definitely useful to know especially once you need to get around to advanced editor stuff -- you may eventually need it when you extend Unity to do really nice chores for you and your team :P. Here's a helpful blog post for more information where they describe serialization used by Unity.

I hope this somewhat helped! It's a big topic >.<. Also note! If you want to see the serialization first-hand, go to one of your Unity scene files or asset files (like a prefab) in your file explorer on your computer, and open it with a text editor like Notepad++ or Visual Studio or something. You'll be able to see all the serialized data, arrays, field names & values, and stuff in there!

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

22 People are following this question.

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

Related Questions

Drawing a custom variable in the inspector y position bugs selection 0 Answers

Inspector: custom property with custom type use default editor 3 Answers

Custom inspector, show string instead of component type 0 Answers

calculate things... and display in inspector 3 Answers

OnInspectorGUI changes reset when played in editor or building 2 Answers


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