Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Zubli · Jul 22, 2017 at 05:02 AM · unity 5script.variableinspectormonobehavior

How to assign script to inspector variable

I am trying to create an extensible utility that generates objects based on some data. I'd like to be able to generate these objects based on a selected script that can be assigned to a field in the inspector (on a gameobject or in an editor window).

I might for example have the classes "Generator", "ObjectGenerator : Generator", "ImageGenerator : Generator", "VectorGenerator : Generator". Any developer can add whatever generator classes they like, they just need to be able to assign them to a field, which will be read and used by some executing code.

So far I haven't seen any way to do this, other than doing annoying things like adding these scripts as components on to otherwise useless objects in the hierarchy. I'd really rather not hard code the available options and display them all as a list. I also need to be able to assign this field from code.

I don't need the above system exactly, just some way that works similarly without a difficult process for the end user. Ideally there would also be a way for them to control the parameters and variables on their selected script, like a regular inspector.

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
1

Answer by dfego · Aug 27, 2017 at 04:13 AM

I think the use of ScriptableObject might be handy to you here. It's a bit long, but if you look at the tutorial series "Pluggable AI With Scriptable Objects", it might help. The thing I was Googling to figure out when I found this thread was "why do I need to make classes and then make assets and then assign those assets?" I think the answer is that you simply can't assign classes that way, but using ScriptableObject classes, generating a single asset for that SO, and then assigning it in the inspector works. I hope this at least helps!

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 hexagonius · Aug 27, 2017 at 06:36 AM 0
Share

It's actually the only way to do this

avatar image
0

Answer by taylank · Mar 03, 2018 at 11:47 PM

There is a way to skip the whole "assign the SO manually" step. The variable type you need is Monoscript.


         [OnValueChanged("ReadScript")]
         public MonoScript script;
         [InlineEditor]
         public List<ScriptableObject> testList = new List<ScriptableObject>();


The attribute you see above is an Odin Inspector attribute, which is a plugin available in the asset store, but you could also have your own mechanism for doing the same thing, which is calling a function whenever the value of our script variable changes. I'll get to the list part in a second. Let's look at the ReadScript function first.


The function looks like this:

 private void ReadScript ()
     {
         var assetType = script.GetClass();
         if (typeof(ScriptableObject).IsAssignableFrom(assetType))
         {
             var instance = ScriptableObject.CreateInstance(assetType);
             AssetDatabase.CreateAsset(instance, "Assets/Resources/AutoGenerated/NewInstance.asset");
             testList.Add(instance);
         }
     }



In a nutshell what we're doing here is getting the type defined in the script file each time the user assigns a new script to the script variable, and creating a new instance of that type. I am also checking to make sure the type inherits from ScriptableObject, which has a couple advantages, like being able to save the newly created instance as an asset and being able to modify it with an inline editor (again, courtesy of Odin). After creating the new instance I'm saving it with the AssetDatabase (which makes the data on it persist) and I add it to a list which has the [InlineEditor] attribute, which is the thing that allows you to edit its fields in the editor.


The flaw in this example is that each time you create a new instance of something, it will overwrite the NewInstance.asset file, since we're not checking to see if the file name exists in the first place. You can add your own mechanism of making sure file names stay unique.

Hope this helps.

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

138 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 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

Is it possible to show Static Variables in the Inspector? 10 Answers

Accessing color presets in c# script. 1 Answer

Javascript and C#, different behaviour in inspector ? 1 Answer

How to create gameobject with different setting 1 Answer

How to validate camera when collides with a wall - ThirdPerson 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