,我找到了一些老师让玩家提高技能。目前,有12个人,所以我为他们写了一个基础课。在我的游戏中优化Unity CharacterSelection
该类从它自己的“teacherDataClass”中选择正确的数据。数据由编辑器的索引设置。
我的代码:
[SerializeField]
int teacherIndex; // set an index in the editor -> selection of teacher
NpcTeacherData teacherData; // the data class
private void Start()
{
NpcTeacherData[] teachers = // Collection of all 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]; // Get the right teacher by the index
}
所以这看起来很好,并能正常工作。如果你不希望使用编辑器,你可以比较teacherObject的标签像这样
NpcTeacherData teacherData; // the data class
private void Start()
{
switch (gameObject.tag) // compare the tag of the teacher and set its class
{
case "TeacherAlchemist":
teacherData = new TeacherAlchemist();
break;
case "TeacherBlacksmith":
teacherData = new TeacherBlacksmith();
break;
//...
}
}
但我不喜欢这些方式那么多:/有没有更好的机会?像编辑器中的DropDownSelection可能?
我试图
[SerializeField]
object script; // Place the right data class here
[SerializeField]
object script as NpcTeacherData; // Place the right data class here
但这并没有工作。
我只想优化它:)
对不起,我不希望使用OnGUI :)我要选择的数据类1次。 OnGUI方法将被称为所有游戏。 – peterHasemann