简体   繁体   中英

How can i use variable value as variable name in other script?

I have a unity project with a config file (for anim names)

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...}

and in another script I want to activate animation using

public animNames name_Of_The_Anim_I_Want_To_Activate;

my issue is to convert name_Of_The_Anim_I_Want_To_Activate.ToString() into the actual animation name

which tool can i use to turn name_Of_The_Anim_I_Want_To_Activate = anim1 into "NameOf anim1";

I want to go through the enum, since there are several buttons each triggering another anim 1. I don't wanna use inheritance or multiple scripts 2. I wanna try a more elegant way than a switch statement. this is why I cant the enum to be able to reach the variable name.

Edit: Eventually i dumped the "Static config file" idea for a monobehaviour manager then i could have (static/const or not) public string anim1= "name1"; public string anim2 = "name2";

public enum CompAnim
{
   Company1,
   Company2,
}

and use

public string CompanyAnim(CompAnim comp)
{
    string toRun = 
    this.GetType().GetField(comp.ToString()).GetValue(this).ToString();
    return toRun;
}

thanks to Camile for the elegant solution, that's exactly what I was looking for.

Assuming that in Anims you have:

public string anim1 = "someAnimation";

In the script where you store the actual animation:

public string someAnimation = "theActualAnimationName";

...

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

This will assign "theActualAnimationName" to the variable toRun

If you store the animations as Animation objects rather than strings, you can use those directly with the above method too.

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

You could use, from the object that triggers the animation:

gameObject.GetComponent<Anims>().anim1

with gameObject being the object that holds Anim class. this will access variable anim1 from Anim class.

You have to just add reference of Anims class in another script and than access variables of Anims. If you have not Static class and static members.

In your case you just acess Anims Properties like:

var name_Of_The_Anim_I_Want_To_Activate = Anims.anim1;

This is why because you have static class and static member, so you have not need to create instance/object of a class to access variable of Anims class.

Try to Add an helper function similar to this:

    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!!"; 
        }  
    }

Update

In case you can use Dictionary<string,string> then you could do this:

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


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

register the entries like that

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

Create an helper method like that:

    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;
    }

so finally you can use it like this:

     name_Of_The_Anim_I_Want_To_Activate = animNames.anim2;

     var name = GetStringValue(name_Of_The_Anim_I_Want_To_Activate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM