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 /
  • Help Room /
avatar image
0
Question by Exuro89 · Feb 03, 2017 at 06:16 AM · editor-scriptingscriptableobject

Editor script for scriptableobject not working.

Hello,

I wanted to add a way to select multiple enum values for an enum in my scriptable object and found out about enummaskfield, but edit scripts don't seem to work with scriptable objects? I tested it with a monobehavior and it works, it's just scriptable object not working. I'm not very familiar with editor scripting so let me know if there's something special I need to do for scriptable objects.

 using UnityEditor;
 
 [CustomEditor(typeof(Action))]
 public class EditorActionTarget : Editor {
     public override void OnInspectorGUI() {
         Action myTarget = (Action) target;
         myTarget.targetType = (TargetType) EditorGUILayout.EnumMaskField("Enums", myTarget.targetType);
     }
 }

Thanks

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
Best Answer

Answer by Adam-Mechtley · Feb 03, 2017 at 07:47 AM

The problem here is that you are writing directly to your target object's fields instead of to it serialized data stream, so the Editor does not know there are changes that are dirty and need to be flushed to disk. You may find e.g., my answer to this question helpful, but in short, you should instead use the Editor.serializedObject property and the SerializedObject.FindProperty() method to access the serialized data stream for the field in question. The result could look something like this:

 using UnityEditor;
  
 [CustomEditor(typeof(Action))]
 public class EditorActionTarget : Editor {
     static readonly GUIContent targetTypeLabel = new GUIContent("Enums");
     SerializedProperty targetType;
     void OnEnable {
         // note that the string passed here needs to match the name of the actual serialized field, in case it is a backing field for an accessor; e.g., it might be m_TargetType in your case
         targetType = this.serializedObject.FindProperty("targetType");
     }
     public override void OnInspectorGUI() {
         this.serializedObject.Update();
         EditorGUILayout.PropertyField(targetType, targetTypeLabel);
         this.serializedObject.ApplyModifiedProperties();
     }
 }

If you need or want to draw that property using a special type of control, the best way to do so is to use a custom PropertyAttribute and PropertyDrawer. Done properly, this can often obviate the need to make a custom Editor at all.

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 Exuro89 · Feb 03, 2017 at 08:00 PM 0
Share

Hi,

Thanks for the response. So maybe I'm missing something from what you said. I checked out your other question and it looks like his issue was just dealing with the data not saving when it resets. I'm not having that Issue. I just want to be able to multiselect enums using enummaskfield. You answer to me suggests I would have to write this from scratch using PropertyAttributes/Drawers with the above method. Is that right or can I use that method here?

Thanks.

avatar image Adam-Mechtley Exuro89 · Feb 03, 2017 at 09:08 PM 0
Share

Sorry for the lack of clarity: Yes that is probably what I would do. That said, there's really no reason Unity shouldn't be smart enough to simply use an enum mask drawer if the field type has System.FlagsAttribute, so I will probably add that to my to-do list.

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

99 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

Related Questions

Changes to asset data persist - unable to discard changes 1 Answer

How to edit Properties of a ScriptableObject asset, specificly arrays/lists 0 Answers

Add ScriptableObject to collection upon creation 0 Answers

ScriptableObject alternative, editable at runtime 0 Answers

ScriptableObject not Serializing? 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