如何将字符串转换为枚举数组(keycode [])

时间:2019-06-05 08:39:51

标签: c# arrays string unity3d

我正在尝试转换一个字符串,该字符串包含用逗号','(F1,F2,A,B ...)分隔的键控代码。

但是当我在文本文件中写入此值时,我得到的是UnityEngine.KeyCode []而不是实际值。

    private static ConfigSimulation UpdateConfigSimulation(string path, ConfigSimulation config)
{
    #region ReadConfig
    Dictionary<string, string> content = new Dictionary<string, string>();
    content = ReadConfigFile(path);
    #endregion
    foreach (KeyValuePair<string, string> cont in content)
    {
        if (config.Keys.ContainsKey(cont.Key))
        {
            KeyCode[] Tab = new KeyCode[cont.Value.Split(',').Length];
            if (!cont.Value.Contains(',') || (cont.Value.Length == 0))
            {
                KeyCode MyStatus = (KeyCode)Enum.Parse(typeof(KeyCode), cont.Value, true);
                Tab[0] = MyStatus;
            }
            else if (cont.Value.Contains(','))
            {
                string[] Splits = cont.Value.Split(',');
                for (int i = 0; i < Splits.Length; i++)
                {
                    KeyCode MyStatus = (KeyCode)Enum.Parse(typeof(KeyCode), Splits[i], true);
                    Tab[i] = MyStatus;
                }
            }
            config.Keys[cont.Key] = Tab;
        }
    }
    return (config);
}

对于编写代码:

        File.WriteAllText(path, string.Empty);
    using (StreamWriter file = new StreamWriter(path))
    {
        file.WriteLine("###keys");
        foreach (var kvp in config.Keys)
        {
            file.WriteLine(kvp.Key + '=' + kvp.Value);
        }
    }

1 个答案:

答案 0 :(得分:1)

export class CubeViewWebGlComponent implements OnInit { public angle = { x: 0, y: 0, z: 0 }; /////Some code//////////////// } 的类型为import { Component, ViewChild, AfterViewInit, Input} from '@angular/core'; import {CubeViewWebGlComponent} from './cube-view-web-gl/cube-view-web-gl.component'; export class AppComponent implements AfterViewInit { @ViewChild(CubeViewWebGlComponent) CubeView: CubeViewWebGlComponent; ngAfterViewInit() { setInterval(this.checkCubeAngle, 1000); } checkCubeAngle() { console.log(this.angleCubeComponent.angle); } /////Some code//////////////// } ,所以当您这么做

config.Keys

Dictionary<string, KeyCode[]>的类型为foreach (var kvp in config.Keys) { file.WriteLine(kvp.Key + '=' + kvp.Value); } ,因此其隐式称为kvp.Value返回KeyCode[]。没有隐式方法可以打印出所有逗号分隔的值。


使用string.Join将数组转换为逗号分隔的字符串

ToString()