• 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 lPVDl · Nov 27, 2017 at 02:26 PM · referenceassetdatabasemonoscript

Keep mono reference in the asset of the private class

I started playing with custom editors and got an idea to have something like config for the editors. So I've created simple method for that purpose:

 public static T LoadOrCreateConfig<T>(this Editor editor) where T : ScriptableObject
     {
         var path = GetRequiredConfigName<T>(editor);
         var config = AssetDatabase.LoadAssetAtPath<T>(path);
         if (config == null)
         {
             AssetDatabase.DeleteAsset(path);
             config = ScriptableObject.CreateInstance<T>();
             AssetDatabase.CreateAsset(config, path);
         }
         return config;
     }
 
     private static string GetRequiredConfigName<T>(Editor editor)
     {
         var mono = MonoScript.FromScriptableObject(editor);
         var path = AssetDatabase.GetAssetPath(mono);
         return PathUtil.ExcludeFileExtension(path) + "_config.asset";
     }

So predicted common usage may look like:

 public class MyCustomEditor : Editor
 {
     private class MyCustomEditorConfig : ScriptableObject { }
 
     private MyCustomEditorConfig _config;
 
     private void OnEnable()
     {
         _config = this.LoadOrCreateConfig<MyCustomEditorConfig>();
     }
 }

The problem is that, if I declare config class that way (inside editor class) then it breaks each time unity restart, it just says me that reference to the mono script is lost. alt text I want to use private class just in order to be sure that all config is encapsulated and no one can access it (and hide it in the IntelliSense as well). I'm trying to use ScriptableObject instead of JSON, XML, INI etc., because I can create custom drawers for that config and it is easy to setup reference to the complicated objects, those like textures, gameobjects, mono etc. So the question, is there a way to keep reference to the class declared inside the other class?

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
1

Answer by Bunny83 · Nov 27, 2017 at 11:10 PM

ScriptableOBjects have the same requirement as MonoBehaviours. They need to be located in a file that has the same name as the class in order to be serialized. That means you can't make it private nor a nested class.


If you want to store config data you may just serialize them as JSON. Of course you can't store / serialize references that way but you haven't specified what the actual use would be.

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 lPVDl · Nov 28, 2017 at 09:05 AM 0
Share

The usage is to move all vital constants to the external file, so actual script user can tune behavior without digging throw code. For example this can be setup of icons for the editor, float variables which corresponds to the updating of the colliders in some custom system, etc. By the way, is that a good idea or not?

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

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

Related Questions

Scriptable Object references if not saved in AssetDatabase? 1 Answer

Why AssetDatabase.LoadAssetAtPath does not trows exception? 0 Answers

Where is Mono 1.2.5 assembly reference as used by Unity? 1 Answer

How do i convert a String into a Class reference in C# 1 Answer

How do I call a function in another gameObject's script? 5 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