Search Unity

How to display a struct field in inspector?

Discussion in 'Scripting' started by the_gnoblin, Oct 11, 2010.

  1. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    Hello!

    I have problems while trying to show

    Code (csharp):
    1. public Rect2 rect;
    in inspector,
    where Rect2 is a struct:

    Code (csharp):
    1. [System.Serializable]
    2. public struct Rect2
    3. {
    4.     public bool a;
    5.     public int b;
    6.     public int c;
    7.  
    8. }
    If I make it a class, then everything works ok:

    Code (csharp):
    1. [System.Serializable]
    2. public class Rect2
    3. {
    4.     public bool a;
    5.     public int b;
    6.     public int c;
    7. }
    Is it possible to show a struct in inspector? Vector3, Rect, Vector2 are all structs and we see them in the inspector, so I am a little bit confused.

    Thank you!
     
    Healky and SaSha_K like this.
  2. Prodigalpops

    Prodigalpops

    Joined:
    May 5, 2010
    Posts:
    86
  3. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    That other thread pretty much sums it up. In short, you're out of luck :(

    I seem to be one of the few who thinks being able to serialize custom structs in Unity would be useful. But, I would guess there's a valid technical reason it's not supported.

    As for Unity's own struct types, I imagine those are just handled as special cases.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    For the record, since this is the first Google result that comes up when you search for it: this works now.

    You do have to use System.Serializable and make sure your struct members are public, but assuming you do that, it works fine, both for single members and for arrays of them.
     
    Healky, kemijo, Melanzue and 26 others like this.
  5. browne11

    browne11

    Joined:
    Apr 30, 2015
    Posts:
    138
    Pretty old post but I can confirm the above is true. Thanks Joe!

    [System.Serializable] right above your declared struct.
     
    Healky, bebaoboy, Nuka_Coke and 10 others like this.
  6. aranthel09

    aranthel09

    Joined:
    Jul 17, 2015
    Posts:
    66
    This still doesn't work for me. I have this:
    Code (CSharp):
    1. [System.Serializable]
    2.     public struct EquippedItem
    3.     {
    4.         public long id;
    5.         public string type;
    6.         public string name;
    7.         public string equipmentSlot;
    8.     }
    however, nothing shows in the inspector anyway
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    It will work as long as you have a field of the type EquippedItem exposed to the inspector. Normally via a MonoBehaviour or a ScriptableObject.
     
    Bunny83 likes this.
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It should. Please show the MonoBehaviour where you're using this.

    (Doh! Sniped by @BoredMormon!)
     
    Kiwasi likes this.
  9. aranthel09

    aranthel09

    Joined:
    Jul 17, 2015
    Posts:
    66
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. using UnityEngine.UI;
    5. using System.Linq;
    6.  
    7. public class PartySystem : NetworkBehaviour {
    8.  
    9.  
    10.     [SyncVar]public string partyName = "";
    11.     [SyncVar]public int partyLevel = 0;
    12.     [SyncVar]public float partyEXP = 0f;
    13.     [SyncVar]public int maxMembers = 5;
    14.     [SyncVar]public string partyLeader = "";
    15.  
    16.     [System.Serializable]
    17.     public struct EquippedItem
    18.     {
    19.         public long id;
    20.         public string type;
    21.         public string name;
    22.         public string equipmentSlot;
    23.     }
    That's my script
     
    momichan and Hiddensquid_ like this.
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You need to add public EquippedItem equippedItem; to your variable declarations.
     
  11. jjhunt

    jjhunt

    Joined:
    Sep 26, 2014
    Posts:
    1
    thanks
     
  12. WilliamBerne

    WilliamBerne

    Joined:
    Jul 17, 2012
    Posts:
    2
    Thanks, it worked!
     
  13. adityasreenivas

    adityasreenivas

    Joined:
    Sep 13, 2017
    Posts:
    6
    Hello Kiwasi! Can we use [System.Serializable] to display variables kept inside a nested structure.....
     
  14. rajanerve

    rajanerve

    Joined:
    Nov 17, 2017
    Posts:
    17
    Hai! I had tried [System.Serializable] for simple structure with few variables. But that is not listing up in the Inspector. Could anyone please help!! The structure is kept within monobehaviour .
     
  15. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Please post the code (use the Insert Code button, located between the movie filmstrip and the floppy disk icon in the toolbar, to format it correctly). Show both where the struct is declared, and the MonoBehaviour where you are using it.
     
  16. rajanerve

    rajanerve

    Joined:
    Nov 17, 2017
    Posts:
    17
    Code (CSharp):
    1. using System;
    2. using System.Net;
    3. using System.Net.Sockets;
    4. using System.IO;
    5. using System.Runtime.Interopservices;
    6. using UnityEngine;
    7. using System.Threading;
    8. public class NetworkServer:MonoBehaviour
    9. {
    10. public int portAddress;
    11. public int AltHold,AP_Engage;
    12. SampleData sData = new SampleData();
    13. [System.Serializable]
    14. public struct tempControls
    15. {
    16. public float gama;
    17. public float track;
    18. }
    19. [System.Serializable]
    20. public struct Controls
    21. {
    22. public float gama_ref;
    23. public float track_error;
    24. public tempcontrols tc;
    25. }
    26.  
    That's my script
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You would have to have a variable of type 'tempControls' and 'Controls' to see them in the inspector. Maybe just 'Controls' in your case, and you'd see that plus the tempcontrols member. :)
     
    unity_Sh5lfZokN7wC_Q likes this.
  18. rajanerve

    rajanerve

    Joined:
    Nov 17, 2017
    Posts:
    17
    :) Thank u.Can you explain me with an code snipppet.
     
  19. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    a code snippet of making a variable?
    Code (csharp):
    1. public Controls myControls; // wow! :)
    try that.
     
  20. rajanerve

    rajanerve

    Joined:
    Nov 17, 2017
    Posts:
    17
    Thanks a lot :).Finally it worked:).
    still I have a doubt whether it is mandatory to make my class as Monobehaviour.
     
  21. adityasreenivas

    adityasreenivas

    Joined:
    Sep 13, 2017
    Posts:
    6
    Hai! I had tried listing up variables of a structure in an inspector.,I succeeded,Thanks a lot methos5k and kiwasi. But i wish to configure a particular set of gameobjects(switches) to do particular animations depending upon structure variables , which i wish to configure through inspector.

    Below SwitchTypeis is an enum which tells the switch type. Refer the snap attached, through which switch type will be configured. Similarly, I wish Onswitch,Offswitch,Ontoidleswitch,Offtoidleswitch,OntoOffswitch and OfftoOnswitch to be a structure variable which i wish to list up in the inspector just like enum-dropdown.

    Current These variables "Onswitch.... OfftoOnswitch" were declared public keycodes to make the action.

    Code (CSharp):
    1. switch (SwitchTypis)
    2.         {
    3.         /*******************************OnOffType****************************************************/
    4.         case SwitchType.OnOffType:
    5.             if (Input.GetKey (Onswitch))
    6.                 //Anim2.Play ("OnOffType");
    7.                 anim.SetBool ("OnOffStatus", true);
    8.  
    9.             if (Input.GetKey (Offswitch))
    10.                 //Anim2.Rewind ();
    11.                 anim.SetBool ("OnOffStatus", false);
    12.             break;
    13.             /*********************************PullOnOffType**************************************************/
    14.         case SwitchType.PullOnOffType:
    15.             if (Input.GetKey (Onswitch))
    16.                 //Anim2.Play ("OnOffType");
    17.                 anim.SetBool ("OnOffStatus", true);
    18.  
    19.             if (Input.GetKey (Offswitch))
    20.                 //Anim2.Rewind ();
    21.                 anim.SetBool ("OnOffStatus", false);
    22.             break;
    23.             /**********************************OnIdleOffType*************************************************/
    24.         case SwitchType.OnIdleOffType:
    25.             if (Input.GetKey (Onswitch))
    26.                 anim.SetInteger ("Switchpos", 1);
    27.  
    28.             if (Input.GetKey (Offswitch))
    29.                 anim.SetInteger ("Switchpos", 2);
    30.  
    31.             if (Input.GetKey (Offtoidleswitch))
    32.                 anim.SetInteger ("Switchpos", 3);
    33.  
    34.             if (Input.GetKey (Ontoidleswitch))
    35.                 anim.SetInteger ("Switchpos", 5);
    36.  
    37.             if (Input.GetKey (Ontooffswitch))
    38.                 anim.SetInteger ("Switchpos", 4);
    39.  
    40.             if (Input.GetKey (Offtoonswitch))
    41.                 anim.SetInteger ("Switchpos", 6);
    42.             break;
    43.             /***********************************PullOnIdleOffType************************************************/
    44.         case SwitchType.PullOnIdleOffType:
    45.             if (Input.GetKey (Onswitch))
    46.                 anim.SetInteger ("Switchpos", 1);
    47.  
    48.             if (Input.GetKey (Offswitch))
    49.                 anim.SetInteger ("Switchpos", 2);
    50.  
    51.             if (Input.GetKey (Offtoidleswitch))
    52.                 anim.SetInteger ("Switchpos", 3);
    53.  
    54.             if (Input.GetKey (Ontoidleswitch))
    55.                 anim.SetInteger ("Switchpos", 5);
    56.  
    57.             if (Input.GetKey (Ontooffswitch))
    58.                 anim.SetInteger ("Switchpos", 4);
    59.  
    60.             if (Input.GetKey (Offtoonswitch))
    61.                 anim.SetInteger ("Switchpos", 6);
    62.             break;
    63.             /***************************************SpringLoadedOnOff********************************************/
    64.         case SwitchType.SpringLoadedOnOff:
    65.             if (Input.GetKey (Onswitch))
    66.                 //Anim2.Play ("OnOffType");
    67.                 anim.SetBool ("Springloadon", true);
    68.  
    69.             if (Input.GetKey (Offswitch))
    70.                 //Anim2.Rewind ();
    71.                 anim.SetBool ("Springloadon", false);
    72.             break;
    73.             /***********************************SpringLoadedOnIdleOff************************************************/
    74.         case SwitchType.SpringLoadedOnIdleOff:
    75.             if (Input.GetKey (Onswitch))
    76.                 anim.SetInteger ("Springload", 1);
    77.  
    78.             if (Input.GetKey (Offswitch))
    79.                 anim.SetInteger ("Springload", 2);
    80.  
    81.             if (Input.GetKey (Offtoidleswitch))
    82.                 anim.SetInteger ("Springload", 3);
    83.  
    84.             if (Input.GetKey (Ontoidleswitch))
    85.                 anim.SetInteger ("Springload", 4);
    86.  
    87.             break;
    88.  
    89.         default:
     

    Attached Files:

  22. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    @adityasreenivas, that is getting well off the topic of this (old) thread. Please start a new thread with your question about animating based on a public enum or integer.
     
  23. SaSha_K

    SaSha_K

    Joined:
    Jan 4, 2017
    Posts:
    7
    Putting all together:
    * struct should be marked by [Serializable]
    * and it should be declared as public with public fields
     
    Genebris and ComradeVanti like this.
  24. PashDev

    PashDev

    Joined:
    Nov 6, 2021
    Posts:
    2
    for the record this will do fine:

    Code (CSharp):
    1. public class Exmple : MonoBehaviour
    2. {
    3.         public Data D = new Data{};
    4. }
    5.  
    6. [System.Serializable]
    7. public struct Data
    8. {
    9.     public uint Var;
    10. }
     
  25. HallelKadosh

    HallelKadosh

    Joined:
    Jan 14, 2024
    Posts:
    1
    So, does this not work any more? I Have the following:
    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     [System.Serializable]
    4.     public struct Location
    5.     {
    6.         public Sprite Image { get; set; }
    7.         public string Name { get; set; }
    8.         public string Description { get; set; }
    9.         public bool IsPlanet { get; set; }
    10.         public bool HasBar { get; set; }
    11.         public bool HasMarket { get; set; }
    12.         public bool HasBBS { get; set; }
    13.         public bool HasBooks { get; set; }
    14.         public bool HasOufitter { get; set; }
    15.         public bool HasShipyard { get; set; }
    16.         public bool HasFuel { get; set; }
    17.         public Color Color { get; set; }
    18.         public string System { get; set; }
    19.         public Vector3? Coords { get; set; }
    20.     }
    21.     public Location CurrentLocation;
    22.  
    I'm not seeing either the field or the struct subfields in the inspector.
    I also tried serializing each of the members of the struct and the instance as well per the other discussion and it didn't work.
    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     [System.Serializable]
    4.     public struct Location
    5.     {
    6.         [SerializeField] public Sprite Image { get; set; }
    7.         [SerializeField] public string Name { get; set; }
    8.         [SerializeField] public string Description { get; set; }
    9.         [SerializeField] public bool IsPlanet { get; set; }
    10.         [SerializeField] public bool HasBar { get; set; }
    11.         [SerializeField] public bool HasMarket { get; set; }
    12.         [SerializeField] public bool HasBBS { get; set; }
    13.         [SerializeField] public bool HasBooks { get; set; }
    14.         [SerializeField] public bool HasOufitter { get; set; }
    15.         [SerializeField] public bool HasShipyard { get; set; }
    16.         [SerializeField] public bool HasFuel { get; set; }
    17.         [SerializeField] public Color Color { get; set; }
    18.         [SerializeField] public string System { get; set; }
    19.         [SerializeField] public Vector3? Coords { get; set; }
    20.     }
    21.  
    22.     // Port Info
    23.     [SerializeField] public Location CurrentLocation;
    24.  
     
  26. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    This thread is fourteen (14) years old. Please don't necro-post. If you have a new question, make a new post. It's FREE!!



    When you post remember nobody can read your mind, and nobody can see your screen.



    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - Do NOT attach entire scripts to your post.
    - ONLY post the relevant code, and then refer to it in your discussion.
     
  27. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,175
    You're using properties not fields. A public field will automatically be serialized by Unity if you've marked the entire class or struct as serializable, but a property has to have the appropriate attribute and it has to be told to serialize the backing field of the property.

    Code (csharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     [System.Serializable]
    4.     public struct Location
    5.     {
    6.         [field: SerializeField] public Sprite Image { get; set; }
    7.         [field: SerializeField] public string Name { get; set; }
    8.         [field: SerializeField] public string Description { get; set; }
    9.         [field: SerializeField] public bool IsPlanet { get; set; }
    10.         [field: SerializeField] public bool HasBar { get; set; }
    11.         [field: SerializeField] public bool HasMarket { get; set; }
    12.         [field: SerializeField] public bool HasBBS { get; set; }
    13.         [field: SerializeField] public bool HasBooks { get; set; }
    14.         [field: SerializeField] public bool HasOufitter { get; set; }
    15.         [field: SerializeField] public bool HasShipyard { get; set; }
    16.         [field: SerializeField] public bool HasFuel { get; set; }
    17.         [field: SerializeField] public Color Color { get; set; }
    18.         [field: SerializeField] public string System { get; set; }
    19.         [field: SerializeField] public Vector3? Coords { get; set; }
    20.     }
    21.  
    22.     // Port Info
    23.     [SerializeField] public Location CurrentLocation;
     
    Last edited: Jan 14, 2024