Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Assigning properties (via enums) to an array value

Discussion in 'Scripting' started by Aurecon_Unity, Apr 19, 2014.

  1. Aurecon_Unity

    Aurecon_Unity

    Joined:
    Jul 6, 2011
    Posts:
    241
    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
     
  2. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    Try to use an array of class.
     
  3. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    It sounds like you are after something like this:
    Code (csharp):
    1.  
    2. // Assets/CameraEntry.cs
    3. using UnityEngine;
    4.  
    5. public enum YourEnum {
    6.     A,
    7.     B,
    8.     C
    9. }
    10.  
    11. [System.Serializable]
    12. public sealed class CameraEntry {
    13.     public Camera camera;
    14.     public YourEnum yourEnum;
    15. }
    16.  
    17. // Assets/YourBehaviour.cs
    18. using UnityEngine;
    19. using System.Collections.Generic;
    20.  
    21. public class YourBehaviour : MonoBehaviour {
    22.     public List<CameraEntry> entries = new List<CameraEntry>();
    23. }
    24.  
    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:
    Code (csharp):
    1.  
    2. // Assets/Editor/CameraEntryPropertyDrawer.cs
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomPropertyDrawer(typeof(CameraEntry))]
    7. public class CameraEntryPropertyDrawer : PropertyDrawer {
    8.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
    9.         var propCamera = property.FindProperty("camera");
    10.         var propYourEnum = property.FindProperty("yourEnum");
    11.  
    12.         // Draw camera field.
    13.         position.width /= 2;
    14.         EditorGUI.PropertyField(position, propCamera, GUIContent.none);
    15.  
    16.         // Draw enum field.
    17.         position.x = position.xMax;
    18.         EditorGUI.PropertyField(position, propYourEnum, GUIContent.none);
    19.     }
    20. }
    21.  
    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 :)
     
  4. Aurecon_Unity

    Aurecon_Unity

    Joined:
    Jul 6, 2011
    Posts:
    241
    Thanks, I'll check that out.
     
  5. Aurecon_Unity

    Aurecon_Unity

    Joined:
    Jul 6, 2011
    Posts:
    241
    Thanks a lot numberkruncher, you've given me a lot to digest and learn! Much appreciated.