Assigning properties (via enums) to an array value

Hi All

I have a camera manager script that is just a simple public array that functions as per normal - I type in the size of the array in the inspector, then I can drag and drop the camera objects to the list.

However, is it possible to somehow introduce an enum dropdown to each of these cameras?

So, the workflow would be I type in how many cameras (the size of the array) into the inspector, then I drag and drop the cameras, and then each camera in the inspector also has a dropdown where I can specify an additional property per camera.

My simple knowledge of scripting tells me this isn’t really possible in a simple way like I envisage, however i’d like to hear some thoughts about possible ways this could be implemented - I’m not asking for code, just general guidance so I can figure it out for myself.

Thanks

Try to use an array of class.

It sounds like you are after something like this:

// Assets/CameraEntry.cs
using UnityEngine;

public enum YourEnum {
    A,
    B,
    C
}

[System.Serializable]
public sealed class CameraEntry {
    public Camera camera;
    public YourEnum yourEnum;
}

// Assets/YourBehaviour.cs
using UnityEngine;
using System.Collections.Generic;

public class YourBehaviour : MonoBehaviour {
    public List<CameraEntry> entries = new List<CameraEntry>();
}

The default inspector requires that you expand items in order to specify camera and your enum value, but you could create a custom property drawer for a more appropriate inspector interface:

// Assets/Editor/CameraEntryPropertyDrawer.cs
using UnityEngine;
using UnityEditor;

[CustomPropertyDrawer(typeof(CameraEntry))]
public class CameraEntryPropertyDrawer : PropertyDrawer {
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
        var propCamera = property.FindProperty("camera");
        var propYourEnum = property.FindProperty("yourEnum");

        // Draw camera field.
        position.width /= 2;
        EditorGUI.PropertyField(position, propCamera, GUIContent.none);

        // Draw enum field.
        position.x = position.xMax;
        EditorGUI.PropertyField(position, propYourEnum, GUIContent.none);
    }
}

If you want your list to be reorderable then you might want to consider using our open source control:
https://bitbucket.org/rotorz/reorderable-list-editor-field-for-unity

Disclaimer: None of the above has been tested, but hopefully it will help. Let me know if you have any trouble with these snippets and I will take a closer look for you :slight_smile:

Thanks, I’ll check that out.

Thanks a lot numberkruncher, you’ve given me a lot to digest and learn! Much appreciated.