Search Unity

Accessing struct variables in inspector like enum dropdown

Discussion in 'Scripting' started by adityasreenivas, Nov 21, 2017.

  1. adityasreenivas

    adityasreenivas

    Joined:
    Sep 13, 2017
    Posts:
    6
    I wish to configure a particular set of gameobjects(vehicle-switches) to do particular animations depending upon structure variables , which i wish to configure through inspector.

    Below, in the snap attached, SwitchTypis is declared enum which tells the switch type. By which switch type will be configured for a gameobject to refer to corresponding animator controller property .

    Currently variables "Onswitch.... OfftoOnswitch" were declared public keycodes to make a setinteger trigger for calling animations of the respective animator.
    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:
    90.  
    Now i wish Onswitch...OfftoOnswitch to be public int, which has to get its values from a struct variable which should be configured.

    Can i list variables of a struct to display in the inspector like enum dropdown menu.
     

    Attached Files:

  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    If you put
    [System.Serializable]
    right before the struct definition, put it in a list, it will show editable fields. Not quite like an enum drop down, but it works pretty good.

    You can customize the editor also, I think, but haven't tried it.
     
  3. adityasreenivas

    adityasreenivas

    Joined:
    Sep 13, 2017
    Posts:
    6
    Code (CSharp):
    1. private struct sample
    2. {
    3. int setval1;
    4. int setval2;
    5. ....
    6. ....
    7. }
    8. public enum Onswitch
    9. {
    10. setval1,
    11. setval2
    12. ....
    13. ....
    14. }
    15. private int triggerval;
    16.  
    17. public Onswitch Onswitchvariable;
    1.Can the variables of a struct be listed as drop down just like enum drop down? Its like assigning public declared Onswitchval to any of struct variable in the inspector itself.
    2. There are 100 gameobjects in my scene which follows any of six animation controller. The animation controller what a single gameobject should refer, is configured through switchtypeis selection, an enum dropdown means of configuring. Refer previous snap.
    Having declared onswitch... as keycode, i can configure any of keycode to setinteger for state animations of particular animation controller.

    Now,since the state of each gameobject is sent through struct from a non-unity network host console application, i wish to wire up the variable responsible for setting animation with gameobject animation dependent variable in the struct sent, manually through inspector, eliminating 600 if combinations(6 animation states for each animator and 100 gameobjects.

    Currently, i declared onswitch to be enum with
    Enum members having same string name as the variable in the strut.

    Can i save the enum selection(say "setval1") as string which i shall use to find what value does sample.setval1 hold. Just like gameibject.find("name"), int triggerval = sample.setval1,

    Where triggerval can be used to call state animations.
     
    Last edited: Nov 21, 2017