• 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
1
Question by Setzer22 · Jan 16, 2013 at 12:34 PM · editoreditor-scripting

Editor scripting: Accessing the script's instance

Hello everyone.

I've started learning a little bit of editor scripting, as I've seen it's an easy en elegant way of doing things for my game. I've followed some tutorials and learned how to do a simple editor script which changes the inspector window for a script or component. I'm extending the editor class, and using [CustomEditor( typeof(MyScript) )], to specify that this will be what's shown at this script's inspector instead of the default script inspector.

The whole point of doing this (although is not very important to what I'm asking) is to make an NPC "pseudo-editor", so the script MyScript just contains a couple of generic lists (or List) of a class "NPC". My goal is being able to add more NPCs (instances of the class NPC) to any of the lists from within the editor (so selecting an npc type, and some of the stats in something like a form), and then being able to select previous elements for the list and modify them.

As far as I know, it shouldn't be difficult with the GUILayout controls to easily build a nice GUI for this, and the functionality I'm trying to implement is not that hard, but I'm having a hard time trying to figure out how can I access the script which I'm creating the inspector for, from my editor script.

So, to explain myself, my script is called NPC_Array, and my editor script NPC_Adder. From NPC_Adder, I want to access the instance of NPC_Array this editor script is currently working on. Just like when I use the "this" keyword for a class, but it has to be something different as this would refference the editor script, not the NPC_Array class (which, if it's of any help, inherits from MonoBehaviour, although otherwise it couldn't have an inspector window wouldn't it?)

Any help on this will be much appreciated, as well as some comments about faster or easier (or fancier) ways to accomplish what I'm trying to do through editor scrpting, which seems to be a whole new world to me.

Thank you very much

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

2 Replies

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

Answer by Bunny83 · Jan 16, 2013 at 12:57 PM

You want the target? ;)

Keep in mind that yourNPC_Array script have to contain serializable constructs and the variables need to be public. The serialization doesn't support inheritance. I guess the NPC class is a simple class with an Serializable attribute? In this case it should work, good luck.

Comment
Add comment · Show 4 · 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 Setzer22 · Jan 16, 2013 at 01:31 PM 0
Share

As far as I know, generic lists (List) are serializable in Unity, am I right? (They show up by the default inspector). Should I do anything else than declaring them as public on Unit_array in order to being able of modifying them?

Other than that, thank you very much, target whas the property I was missing. I should've looked the documentation ^^' my bad

avatar image Bunny83 · Jan 16, 2013 at 01:59 PM 0
Share

Exactly, you should test your class without your custom editor if it serializes properly. If it works as expected, you can use your custom inspector.

avatar image Setzer22 · Jan 16, 2013 at 04:28 PM 0
Share

I have one more doubt. The class is serialized correctly, as it consists of a list of the class NPC, but I don't think the class NPC itself would serialize correctly (I use structs and structs don't serialize with unity, and the class NPC doesn't even inherit from monobehaviour). Would that be a problem in order to save the data I add to the arrays between sessions?

avatar image yoyo · Jan 20, 2013 at 09:59 AM 0
Share

I like adding my own typesafe Target property to my editors, like so: NPC_Array Target { get { return (NPC_Array)this.target; } }

avatar image
0

Answer by CodeMasterMike · Jan 16, 2013 at 01:00 PM

Normally you can do something like this:

 [CustomEditor (typeof(NPC_Array))]
 public class NPC_Adder : Editor 
 {
   // Class here
 }

That means that the NPC_Adder, which is a editor script, is connected to your MonoBehaviour script NPC_Array. Now you can use SerializedProperty to set and retrieve data to and from the NPC_Array script in your custom inspector.

Good luck!

Comment
Add comment · Show 1 · 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 Setzer22 · Jan 16, 2013 at 01:28 PM 0
Share

Thank you, but I already had figured out that. What I was missing was the "target" property to refference the target script. Anyway, good to know that I am doing this right

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Access Monobehavior Instance from Static Function of Editor Script 1 Answer

Is it possible to have editor-only native plugins? 0 Answers

Stop a MonoBehavior from being addable as a script? 0 Answers

How To Best Match Editor GUI Content Heights 0 Answers

runInBackground in Edit Mode 0 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