unity3d - How to make class that can be edited in inspector? -


previous workflow had code this:

public class activityclass : monobehaviour  {     [system.serializable]     public struct activites     {         public string name;         public weaponclass weapon;         public bool hasownactivities;         [hideininspector] public int numb;         public conditionclass.conditions  why;         public actionclass.act[] what;         public int timetoreverse;         public actionclass.act[] whatif;         public animatclass.animat[] howlook;         public statsclass.stats sowhat;         public int actmode;         public int employ;         public bool change;         public bool chosen;         public int timeofaction;         public int timer;      }  } 

of course, looks bad practice, allowed setting these values in editor (there monobehavior descendant has list of activityclasses). how can code turned normal class still editable in editor? removing struct lines makes editor request reference, , not set values.

i not entirely sure think need this:

[system.serializable] public class activity {     public string name;     public weaponclass weapon;     public bool hasownactivities;     private int numb;     public conditionclass.conditions  why;     public actionclass.act[] what;     public int timetoreverse;     public actionclass.act[] whatif;     public animatclass.animat[] howlook;     public statsclass.stats sowhat;     public int actmode;     public int employ;     public bool change;     public bool chosen;     public int timeofaction;     public int timer;  }  public class myclass : monobehaviour {     public activity activity = new activity(); } 

the activity reference show in inspector serializable members. weaponclass instance needs tagged attribute show own members in inspector.

edit: if need have many instances, make array , unity take care of rest:

public activity [] activity; 

with this, unity ask size of array , corresponding amount of element show. each element can opened , display members.

edit 2 : can use list same array:

public list<activity> activities = new list<activity>(); 

this allows add/remove items , still see them or modify them @ runtime.

if class contained references objects, need add object creation in declaration or object ctor.

[system.serializable] public class topclass{     public int myvar;     public subclass subref = new subrclass(); } [system.serializable] public class subclass{     public string classname; }  public class mycomponent:monobehaviour{     public list<topclass>list = new list<topclass>();      void start(){         if(list.count > 2){            debug.log(list[1].subref.classname);         }     }     void update(){         if(input.getkeydown(keycode.space)){             topclass tp = new topclass();             tp.subref.classname = "newname";             list.insert(0, tp);             debug.log(list[0].subref.classname);         }     } } 

start adding value in inspector (at least 2 prints out). run game , @ list size increasing , new objects showing up.

you can see addition in inspector, can modify them manually , still use them in game.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -