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
1
Question by JulesPomme · May 26, 2021 at 04:32 PM · scriptableobjectpropertydrawer

Access dynamic values from the custom PropertyDrawer of a ScriptableObject

Hi there,

I have a ScriptableObject A that intends to capture dynamically a list of parameter values from the inspector. Here is an illustration of what I whant to do : I have one instance of this ScriptableObject A, that represents a set of three parameters : a float (speed), a boolean (isActive) and a Vector3 (initPosition). Each element of the array is an instance of a Parameter, which consists in a variable Name (string) and a variable Type (another ScriptableObject).

alt text

Then, in another class B, I declare a public variable "customParams" of type A, and drag and drop this ScriptableObject to obtain the following in the inspector :

alt text

To do that, I made a custom PropertDrawer on my class A, that loops over the parameters, and displays the appropriate field to fill. Here is some code :

 public class ADrawer : PropertyDrawer
 {
    private Parameter[] parameters = null;
    private object[] values;
 
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
 
         float height = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
         A myParams = property.objectReferenceValue as System.Object as A;
         if (myParams != null) {
             parameters = myParams.parameters;
             height += parameters.Length * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing);
         }
         return height;
     }
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
 
         EditorGUI.BeginProperty(position, label, property);
 
         EditorGUI.PropertyField(position, property, label, true);
 
         if (parameters != null) {
             int prevIndent = EditorGUI.indentLevel;
             Rect indentedPosition = EditorGUI.IndentedRect(position);
             EditorGUI.indentLevel += 1;
             values = new object[parameters.Length];
             int i = 0;
             foreach (Parameter p in parameters) {
                 //Parameter label
                 Rect paramLabelPosition = new Rect(indentedPosition.x, position.y + ((i + 1) * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing)), EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight);
                 EditorGUI.LabelField(paramLabelPosition, p.GetInspectorName());
 
                 //Parameter value
                 Rect valueRect = new Rect(paramLabelPosition.x + paramLabelPosition.width, paramLabelPosition.y, indentedPosition.width - paramLabelPosition.width, EditorGUIUtility.singleLineHeight);
                 Type type = p.type.FindType();
                 values[i] = p.type.DrawPropertyField(valueRect);
                 i++;
             }
             behaviour.SetParameters(parameters, values);
             EditorGUI.indentLevel = prevIndent;
         }
         EditorGUI.EndProperty();
     }

My question is simple : how can I access all the array values from, let's say, a custom PropertyDrawer of B, or even any other class?

Thanks!

2.png (9.3 kB)
1.png (24.7 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

122 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

Related Questions

EditorGUI.PrefixLabel returns a rect that doesn't line up with other content when a custom property drawer is used in an array. 2 Answers

Can you use Editor.DrawDefaultInspector() inside a property drawer? 1 Answer

ScriptableObject drawing error 0 Answers

Display Custom Inspectors for each class in a List<> 1 Answer

PropertyDrawers not working with ScriptableObjects 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