- Home /
Custom editor array without serialized object
Hi
I have an array of audio clips that needs to be assigned in the editor and the script is using a custom editor script. But I can't find a way to make that array of audio clips work. I tried this but it didn't work.
script.audioClips.walkFootstepSounds = EditorGUILayout.ObjectField(script.audioClips.walkFootstepSounds, typeof(AudioClip[]), true);
I have been Googling for a while and all I can find is to use seralizedObject but I can't use it in the way my game is set up. Is there any other way I can make an array of audio clips without using serializedObject?
Thanks
Answer by Polymo · Sep 12, 2015 at 02:19 PM
I think you can use EditorGUILayout.PropertyField(serializedObject.FindProperty("YourArrayName"),y), if that field is marked as [System.serializable]
I did this...
script.audioClips.walkFootstepSounds = EditorGUILayout.PropertyField(serializedObject.FindProperty("walkFootstepSounds"));
and that did not work. "Cannon implicitly convert type 'bool' to 'UnityEngine.AudioClip[]'. But my array is also stored inside a class that is inside the target class if that helps any. That is why I don't want to use these serialized things.
you dont have to assign the result back to the variable. But what you should do ins$$anonymous$$d is Apply$$anonymous$$odifiedProperties() at the end of the OnGUI block. Edit: as Bunny83 below said.
After countless tries at workarounds I finally went with this. It somehow felt "dirty" but it works. Thanks.
Unity also needs to add a easier way of adding arrays...
Answer by Bunny83 · Sep 12, 2015 at 03:19 PM
You only have two options, either:
use EditorGUILayout.PropertyField like ValooFX said
Implement the array mechanics yourself using multiple editor controls.
For the first solution you just have to use something like:
EditorGUILayout.PropertyField(serializedObject.FindProperty("walkFootstepSounds"), true);
You don't need to pass any return values since the scriptable object handles that itself internally when ApplyModifiedProperties is called.
For the second solution you have to handle everything yourself:
Display a foldout for the array and only display the following when it's expanded.
Display an IntField for the item count of the array. When the count is changed you have to recreate the array and copy over the old elements.
Display an ObjectField for each element in a loop.