【问题标题】:How can i use variable value as variable name in other script?如何在其他脚本中使用变量值作为变量名?
【发布时间】: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>

【问题讨论】:

标签: c# unity3d constants


【解决方案1】:

假设您在 Anims 中有:

public string anim1 = "someAnimation";

在您存储实际动画的脚本中:

public string someAnimation = "theActualAnimationName";

...

string toRun = this.GetType().GetField(someAnimNamesEnumValue.ToString()).GetValue(this);

这会将 "theActualAnimationName" 分配给变量 toRun

如果您将动画存储为 Animation 对象而不是字符串,您也可以通过上述方法直接使用它们。

编辑: Additionally you might want to look into what reflection is (this is what the code is doing)

【讨论】:

  • 我不确定我是否理解所以让我们改写一下:我想做的是相反的公共字符串 theActualAnimationName= "anim1"; pastebin.com/Skm7cW7w
  • 每个按钮运行动画的类是不同的还是相同的?我需要有关您想要实现的目标的更多信息,因为我目前不确定。这可能会有所帮助:pastebin.com/nJhh3eWA
  • 嗨,每个按钮都有不同的动画,这就是名称分离的原因,
  • 您好,我再次阅读了您所说的内容并重新测试,它现在可以正常工作,一切都很清楚并大大缩短了我的代码
  • 我注意到您希望它在静态类中运行。在这种情况下,您可以将“this”替换为静态类名,例如'动画'。另外,如果这回答了您的问题,请标记我的答案。
【解决方案2】:

您只需在另一个脚本中添加 Anims 类的引用,然后访问 Anims 的变量。如果你没有静态类和静态成员。

在您的情况下,您只需访问动画属性,例如:

var name_Of_The_Anim_I_Want_To_Activate = Anims.anim1;

这就是为什么因为你有静态类和静态成员,所以你不需要创建一个类的实例/对象来访问 Anims 类的变量。

【讨论】:

  • 谢谢,但这并不完全是我所要求的。 = Anims.anim1 部分适用于 1 个按钮,如果我有 8 个或 20 个按钮,我想让 name_Of_The_Anim_I_Want_To_Activate 可选择,然后将枚举结果转换为变量类型。这样,在没有辅助功能的情况下,我可以随意配置和缩放它。我的问题是它是否有可能做我要问的事情
【解决方案3】:

尝试添加类似这样的辅助函数:

    public static string GetStringValue(animNames enumTarget)
    { 
        switch (enumTarget)
        {
            case animNames.anim1:
                return Anims.anim1;
                break;
            case animNames.anim2:
                return Anims.anim2;
                break;
            case animNames.anim3:
                return Anims.anim3;
                break;
            default:
                return "Wrong Enum Value Sent!!"; 
        }  
    }

更新

如果您可以使用Dictionary<string,string>,那么您可以这样做:

using System.Collections.Generic;
using System.Linq;


public static Dictionary<string, string> _AnimNames = new Dictionary<string, string>();

像这样注册条目

    _AnimNames.Add("anim1", "NameOf anim1");
    _AnimNames.Add("anim2", "NameOf anim2");
    _AnimNames.Add("anim3", "NameOf anim3");

创建一个这样的辅助方法:

    public static string GetStringValue(animNames enumTarget)
    {
        var key = System.Enum.GetName(typeof(animNames), enumTarget);
        var nameEntry = _AnimNames.FirstOrDefault(x => x.Key == key);
        var nameValue = nameEntry.Value;

        return nameValue;
    }

所以你终于可以像这样使用它了:

     name_Of_The_Anim_I_Want_To_Activate = animNames.anim2;

     var name = GetStringValue(name_Of_The_Anim_I_Want_To_Activate);

【讨论】:

  • true,但我正在尝试研究一种比 switch 语句更优雅的方式。配置文件中辅助函数的想法还不错,但我更感兴趣的是是否有一种方法可以绕过枚举并通过变量名作为字符串来获取变量值。
  • 你可以使用 Dictionary 吗?如果是,那么您需要注册这些值并查询字典而不是使用 const 值
  • 谢谢,这很有帮助,是的,我最终使用了 dict
  • 我是被允许的,但我正在寻找一种不基于每次更改时都需要更新的数据结构的解决方案。你的解决方案很好,但我需要的更像是 Camile 提供的。唯一的缺点是它不能在静态对象中工作
猜你喜欢
  • 1970-01-01
  • 2015-11-27
  • 2021-12-01
  • 2011-01-19
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
相关资源
最近更新 更多