Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 GRaveChilD · Apr 17 at 10:54 AM · editoreditor-scriptingscriptableobjectscriptable objecteditor scripting

Scriptable Objects with editable functions

I have a simple ScriptableObject class like this one:

 [CreateAssetMenu(fileName = "Upgrade", menuName = "Upgrade")]
 public class Upgrade : ScriptableObject
 {
     public string Description;
     public void OnAdd() { }
 }

When I create a new SO from the editor menu I want to see an input field for the OnAdd function and be able to write different code for each instance IN THE INSPECTOR.

Is this (or something similar) posible to do with Unity and C# and how can I achieve it?

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 Captain_Pineapple · Apr 17 at 04:33 PM

In some way yes and no.

In a simple straight forward way as you want it no. But there are some workaround ways that could be used to generate a similar behaviour:


In general you'd need a text input field where you "Code". You can use runtime compilation methods to then take that string when the game is running and compile it into usable code that can be assigned to the OnAdd function. I must warn you here that using these methods is bad for performance as well as sercurity. See this reference on how to do this.


For now i will not go into further details about how else this could be solveable as this sounds a lot like a XY-Problem. Can you please read this reference and describe the problem that you are trying to solve here and why you want to be able to programm in the editor?

Comment
Add comment · Show 2 · 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 GRaveChilD · Apr 17 at 04:51 PM 0
Share

To elaborate, I want to create a "Designer Friendly" interface, as I call it, for implementing new upgrades for my game.

I image that you can click "two" buttons in the editor to create a new upgrade, write a short code snippet, attach it and you are good to go.

avatar image Captain_Pineapple GRaveChilD · Apr 17 at 07:40 PM 0
Share

Interesting. My suggestion for this would be to simply write a good documentation with template classes for Upgrade components.

Something like:

   public class MyNewFancyUpgrade : BaseUpgrade
   {
            public void override OnAdd()
            {
                    //put behaviour here
            }
   }

Which then just has to be copied, given a name. Then you can add behaviour to the OnAdd function and can add this component somewhere.

All complex stuff can be given to the function using the BaseUpgrade function.


I think i understand why you want to choose this approach of yours but think about this: Would it really be easier to write a programm in a tiny inspector text field without any code highlighting, autocompletion and syntax/error checks which visual studio for example will give you? Sure it would be somewhat less effort since you don't have to open VS but everything else might be more complex in return. And the time you'd have to spend to write your custom solution and make sure that it really works flawlessly as well as secure and performant...


In case you still want to stick to your idea the best way would imo be to either stick to my first answer or write something that basically creates Monobehaviour classes for you. E.g. you add this base component which then has a "Add new Upgrade" button. When you click it the name from a text input is taken to create new .cs file filled with a template as i posted it above. Then you can have a larger field where someone can enter some code which you then can save into the OnAdd function of your Upgrade component when some Save button is clicked.

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

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

How do I associate my custom object for the serializedProperty objectReferenceValue 1 Answer

Scriptable Objects, how to force Include in Compile / Build without referencing it in the scene? 1 Answer

SceneView.onSceneGUIDelegate GUI sorting problem 1 Answer

Vector2 Editor Visualization 2 Answers

How to work with custom class (ScriptableObject) SerializedProperty? 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