Search Unity

Display Dictionary with a class in the inspector?

Discussion in 'Editor & General Support' started by Zymes, Nov 15, 2017.

  1. Zymes

    Zymes

    Joined:
    Feb 8, 2017
    Posts:
    118
    I have a Dictionary with waypoints for my nav mesh agent, and I want him to do multiple operations on the waypoints and the ability to set this in the inspector.

    I know how to get a dictionary to show up in the inspector, but not with its own class and properties.

    The data looks like this:

    Code (csharp):
    1.  
    2. public class Operations
    3. {
    4.     public LoadOperation loadOperation;
    5.     public UnloadOperation unloadOperation;
    6.  
    7.     public enum LoadOperation
    8.     {
    9.         NoLoading,
    10.         Load
    11.     }
    12.  
    13.     public enum UnloadOperation
    14.     {
    15.         NoUnloading,
    16.         Unload
    17.     }
    18. }
    19.  
    20. [Serializable]
    21.     public class WaypointList
    22.     {
    23.         public Transform waypoint;
    24.         public Operations operation;
    25.     }
    26.  
    27. // Holds the list of waypoints and their operation
    28.     [SerializeField]
    29.     private List<WaypointList> waypointList;
    30.  
    31. // The list of operations for each waypoint
    32.     private Dictionary<Transform, Operations> operationsList;
    33.  
    34. private void Awake()
    35.     {
    36.         // Create an in editor friendly array with waypoints and the operations for each
    37.         operationsList = new Dictionary<Transform, Operations>();
    38.         foreach (WaypointList entry in waypointList) {
    39.             operationsList.Add(entry.waypoint, entry.operation);
    40.         }
    41.     }
    42.  
    43.  
    Right now it only shows me the waypoints in the array because editor can't read the class...

    So in the inspector I want to set the waypoints in an array, drag the transform on to the waypoint, then set the one or multiple operations to execute on this waypoint.
     
  2. sergienko88

    sergienko88

    Joined:
    Jul 22, 2013
    Posts:
    22
    In https://docs.unity3d.com/ScriptReference/SerializeField.html
    Serializable types are:

    - All classes inheriting from UnityEngine.Object, for example GameObject, Component, MonoBehaviour, Texture2D, AnimationClip.
    - All basic data types like int, string, float, bool.
    - Some built-in types like Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, LayerMask.
    - Arrays of a serializable type
    - List of a serializable type)
    - Enums
    - Structs

    Try:
    http://wiki.unity3d.com/index.php/SerializableDictionary
     
  3. Zymes

    Zymes

    Joined:
    Feb 8, 2017
    Posts:
    118
    Oh boy that is a lot of code. However the screenshot only shows Key, Value pairs. I want it to look something like this:

    Code (CSharp):
    1.  
    2. +------------+------------------------------------+-------------------+
    3. | Waypoint 1 | Transform                          |                   |
    4. +------------+------------------------------------+-------------------+
    5. |            | LoadOperation (Operations class)   | Load (enum)       |
    6. +------------+------------------------------------+-------------------+
    7. |            | UnloadOperation (Operations class) | Unload (enum)     |
    8. +------------+------------------------------------+-------------------+
    9. | Waypoint 2 | Transform                          |                   |
    10. +------------+------------------------------------+-------------------+
    11. |            | LoadOperation (Operations class)   | No loading (enum) |
    12. +------------+------------------------------------+-------------------+
    13. |            | UnloadOperation (Operations class) | Unload (enum)     |
    14. +------------+------------------------------------+-------------------+
    15. | Waypoint 3 | Transform                          |                   |
    16. +------------+------------------------------------+-------------------+
    17. |            | LoadOperation (Operations class)   | Load (enum)       |
    18. +------------+------------------------------------+-------------------+
    19. |            | UnloadOperation (Operations class) | No unload (enum)  |
    20. +------------+------------------------------------+-------------------+
    21.  
     
    Last edited: Nov 15, 2017