- Home /
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?
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?
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();
}
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.
Your answer
Follow this Question
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