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 Oamm · May 13, 2016 at 10:01 AM · c#editoreditor-scriptingscriptableobjectserializedproperty

How do I associate my custom object for the serializedProperty objectReferenceValue

I'm attempting to write a custom class object that will allow for storing particular data for my editor script functionality.

I have a custom class that I'm using that looks like this:

 [System.Serializable]
 public class ColorList : ScriptableObject
 {
     public List<Color> colors;
     public List<string> labels;
 
     //public Color curColor;
     //public string curLabel;
 
     public ColorList()
     {
       colors = new List<Color>();
       labels = new List<string>();
     }
 
     public void AddColor(Color c)
     {
       colors.Add(c);
     }
 
     public void AddLabel(string s)
     {
       labels.Add(s);
     }
 
     public void At(ref object obj, int index, int list)
     {
       int count = 0;
       // 0 : represents the List<Color>
       // 1 : represents the List<string>
       if (list == 0)
       {
         foreach (Color c in colors)
         {
           if (count == index)
           {
             obj = c;
             return;
           }
 
           ++count;
         }
       }
       else if (list == 1)
       {
         foreach (string s in labels)
         {
           if (count == index)
           {
             obj = s;
             return;
           }
 
           ++count;
         }
       }
     }
 }

Within my Editor functionality, I'm trying to create my reference during the creation of one of the colorAssociations:

 ++colorAssociations.arraySize;
 colorAssociations.GetArrayElementAtIndex(colorAssociations.arraySize - 1).objectReferenceValue = ScriptableObject.CreateInstance<GeometryImageGenerator.ColorList>();
 colorAssociations.serializedObject.ApplyModifiedProperties();

For some reason creating it like this doesn't seem to be setting the objectReferenceValue correctly, it remains as null.

Later on I'm attempt to utilize my object for manipulation, currently this is just test code to get things working:

 GeometryImageGenerator.ColorList colorList = colorAssociations.GetArrayElementAtIndex(i).objectReferenceValue as GeometryImageGenerator.ColorList;
 for (int j = 0; j < colorList.colors.Count; ++j)
 {
    object c = null;
    colorList.At(ref c, j, 0);
    c = EditorGUILayout.ColorField((Color)c);
 }

However, colorList remains to be null. What am I doing wrong and how should I be approaching this?

Comment
Add comment · Show 2
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 SteveFSP · May 13, 2016 at 04:36 PM 0
Share

A couple quick questions first: What object type is colorAssociations associated with?

Is it a project asset or a scene object?
If it is a scene object, is the assignment happening while in play mode or in editor mode?

avatar image Oamm SteveFSP · May 14, 2016 at 03:28 AM 0
Share

colorAssociations is part of my scene object that is utilized within editor mode. It is a SerializedProperty that I retrieve from my GeometryImageGenerator script that is associated within the GeometryImageGeneratorEditor script.

This is the ColorList[] from my base GeometryImageGenerator : $$anonymous$$onoBehavior script:

 [SerializeField]
 public ColorList[] ColorAssociations;

I am attempting to assign the objectReferenceValue when I press my "Add Geometry" button, which executes this code, from above, within the OnInspectorGUI function:

 if (GUILayout.Button("Add Geometry"))
 {
   ++geometryImages.arraySize;
   ++toLoadImages.arraySize;
   ++colorAssociations.arraySize;
 
   colorAssociations.GetArrayElementAtIndex(colorAssociations.arraySize - 1).objectReferenceValue = ScriptableObject.CreateInstance<GeometryImageGenerator.ColorList>();
       colorAssociations.serializedObject.Apply$$anonymous$$odifiedProperties();
       
   serializedObject.Apply$$anonymous$$odifiedProperties();
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Oamm · May 14, 2016 at 09:59 AM

One of my problems seems to have been stemming from not separating my ColorList class into it's own file. Once I did that I was able to create the ScriptableObject reference for my colorAssociations:

 colorAssociations.GetArrayElementAtIndex(colorAssociations.arraySize - 1).objectReferenceValue = ScriptableObject.CreateInstance<ColorList>();

Still some things I have to figure out, but at least now I'm able to associate the desired object for referencing.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to work with custom class (ScriptableObject) SerializedProperty? 1 Answer

Select custom object from a list via PopUp in custom editor for ScriptableObject 0 Answers

Confused about custom GameObjects,Custom GameObject confusion 0 Answers

Stop a MonoBehavior from being addable as a script? 0 Answers

Get list of all "Action" classes 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