c# - Optimizing a Unity CharacterSelection -
in game, got teachers make player increase skills. @ moment, there 12 guys, wrote base class them.
this class selects correct data own "teacherdataclass". data set index editor.
my code:
[serializefield] int teacherindex; // set index in editor -> selection of teacher npcteacherdata teacherdata; // data class private void start() { npcteacherdata[] teachers = // collection of teachers { new teacheralchemist(), new teacherblacksmith(), new teacherbowyer(), new teacherbutcher(), new teacherhunter(), new teacherinnkeeper(), new teacherjuggler(), new teachermessenger(), new teacherpriest(), new teachertamer(), new teacherthief(), new teachertownguard() }; teacherdata = teachers[teacherindex]; // right teacher index }
so looks fine , works fine. if not want use editor, compare tag of teacherobject way
npcteacherdata teacherdata; // data class private void start() { switch (gameobject.tag) // compare tag of teacher , set class { case "teacheralchemist": teacherdata = new teacheralchemist(); break; case "teacherblacksmith": teacherdata = new teacherblacksmith(); break; //... } }
but not these ways :/ there better possibilities? dropdownselection in editor maybe?
i tried
[serializefield] object script; // place right data class here [serializefield] object script npcteacherdata; // place right data class here
but did not work.
i want optimize :)
maybe can use enum achieve this?
it this:
public enum teachertype { teacheralchemist, teacherblacksmith, teacherbowyer, teacherbutcher, teacherhunter, teacherinnkeeper, teacherjuggler, teachermessenger, teacherpriest, teachertamer, teacherthief, teachertownguard } public teachertype type; private npcteacherdata teacherdata; private void start() { switch (type) { case teachertype.teacheralchemist: teacherdata = new teacheralchemist(); break; //... } }
however doesn't differ lot first solution (only choice bit easier since type shown instead of index).
hope helps,
Comments
Post a Comment