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 LRG · Dec 19, 2014 at 07:09 PM · assetcustom editorassetdatabasecustom inspector

Object references in a custom editor

I am writing a custom editor for an object. That custom editor is for a class which should essentially be able to store a varying number of dynamic persistent references to assets and scene objects on it. Imagine a "PrefabsLinker" script, which can be attached to objects in the Editor and let users link prefabs to it.

Drawing the ObjectFields to display and let the user choose the assets is easy, and so is keeping them for the "current session". Basically, it is enough to, for each dynamic field:

 mField = EditorGUILayout.ObjectField(mField, typeof(UnityEngine.Object), true);

However, I am not sure what would be the best way to persist the choice. The problem is that though I can choose an object for the mField, when Unity is restarted the choice is forgotten. In fact, I guess that if I were to export the project, the specific asset that I referenced wouldn't necessarily be exported.

It is my (limited) understanding that through SerializedProperties I would be able to do this with relative ease, but I don't think I can rely on SerializedProperties at all, because I need to create those references dynamically (sometimes there will be 3 references in the object, sometimes 5, sometimes more...).

How could I do 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · Dec 19, 2014 at 07:53 PM

I use scriptable objects to store in a list on an empty gameobject that acts as the manager. You can store anything in there pretty much. Not what you asked for but it might help.

I can provide an example if you want but probably more honest to show where I got the idea from!

[SpellMaker on YouTube][1]

The unity package is downloadable on that page so you can see how it works. [1]: https://www.youtube.com/watch?v=kRO9uKjxeJc

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
avatar image
0

Answer by .Talon · Dec 19, 2014 at 08:31 PM

If Unity looses the changes upon restart, mField probably is not serialized. If mField is of type UnityEngine.Object make sure it is either public or has the [SerializeField] attribute.

Using SerializedProperties should work just fine. If you want to store multiple objects just use an array:

 //In your object
 [SerializeField]
 Object[] myObjects;
 
 
 //In editor with SerializedObject propObject
 public override void OnInspectorGUI(){
     propObject.Update();
     propObject.FindProperty("myObjects").arraySize = 5;
     for(int i = 0; i < propObject.FindProperty("myObjects").arraySize; i++)
     {
         SerializedProperty currentObject = propObject.FindProperty("myObjects").GetArrayElementAtIndex(i);
         currentObject.objectReferenceValue =  EditorGUILayout.ObjectField(currentObject.objectReferenceValue, typeof(UnityEngine.Object), true);
     }
     propObject.ApplModifiedProperties();

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to limit an ObjectField to assets with specific file extension 2 Answers

Load an asset right as it is imported? 0 Answers

Override asset on import 0 Answers

How to change an object's name inside an asset file? 0 Answers

Getting a target's gameobject in custom editor 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