Custom inspector to get asset path
Hi all,
I've done a fair bit of browsing and I've got a solution to this but it seems very very messy.
I have a list of strings in my scriptableobject.
I want to write an inspector that allows me to drag an asset into a field and save them asset path into this list. The list should be populated in OnEnable() with object references that match the current asset path strings in the list.
I've got it so I can create ObjectFields that populate temporary Objects and I can drop an asset into this field. I use GUI.Changed when this happens and I can save the asset path of the object into the ScriptableObject's serialisedproperty.
However I'm having real trouble recreating the list of asset references in the ObjectFields in the OnEnable of my custom editor. Also I'm not sure if this is the best approach for something like this.
The simple goal is to be able to drag an asset into a field, save the path of that asset and then load that asset during runtime via Resources.Load.
I just want to save designers having to type in the paths.
Code from my custom editor:
public override void OnInspectorGUI()
{
DrawDefaultInspector();
AssetStreamerData prefabStreamer = (AssetStreamerData)target;
SerializedProperty property = serializedObject.FindProperty("AssetResourcePaths");
if (property == null)
return;
if (GUILayout.Button(new GUIContent("+", "Create Set"), EditorStyles.miniButtonLeft, GUILayout.Width(20)))
{
AssetObjects.Add(null);
}
for (int index = 0; index < AssetObjects.Count; index++)
{
AssetObjects[index] = EditorGUILayout.ObjectField("Asset", AssetObjects[index], typeof(Object), false);
}
if(GUI.changed)
{
for (int index = 0; index < AssetObjects.Count; index++)
{
if (AssetObjects[index] == null)
continue;
if (index >= property.arraySize || property.arraySize == 0)
{
property.InsertArrayElementAtIndex(0);
SerializedProperty stringProperty = property.GetArrayElementAtIndex(0);
stringProperty.stringValue = AssetDatabase.GetAssetPath(AssetObjects[index]);
}
else
{
SerializedProperty stringProperty = property.GetArrayElementAtIndex(index);
stringProperty.stringValue = AssetDatabase.GetAssetPath(AssetObjects[index]);
}
}
}
serializedObject.ApplyModifiedProperties();
}
Thanks,
Mike.
Your answer
