【发布时间】:2019-10-16 11:50:03
【问题描述】:
我有一个带有配置文件(用于动画名称)的统一项目
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Anims{
public const string anim1= "NameOf anim1";
public const string anim2= "NameOf anim2";
public const string anim3= "NameOf anim3";
...
}
public enum animNames { anim1, anim2,anim3...}
在另一个脚本中我想使用
来激活动画public animNames name_Of_The_Anim_I_Want_To_Activate;
我的问题是将name_Of_The_Anim_I_Want_To_Activate.ToString() 转换为实际的动画名称
我可以使用哪个工具将name_Of_The_Anim_I_Want_To_Activate = anim1 转换为“NameOf anim1”;
我想通过枚举,因为有几个按钮每个触发另一个动画 1.我不想使用继承或多个脚本 2.我想尝试一种比switch语句更优雅的方式。这就是为什么我不能枚举能够到达变量名的原因。
编辑: 最终,我为单一行为经理抛弃了“静态配置文件”的想法 那么我可以拥有(静态/常量与否) 公共字符串 anim1 = "name1"; 公共字符串 anim2 = "name2";
public enum CompAnim
{
Company1,
Company2,
}
并使用
public string CompanyAnim(CompAnim comp)
{
string toRun =
this.GetType().GetField(comp.ToString()).GetValue(this).ToString();
return toRun;
}
感谢 Camile 提供的优雅解决方案,这正是我所寻找的。p>
【问题讨论】: