最近在学习Unity基础,学习到Input的虚拟轴时发现虚拟轴必须要填写轴的名称,这需要到ProjectSetting里面去复制出来,再粘贴,非常的蛋疼。
于是我就想能否自己封一个类来通过枚举名把名字点出来呢?
于是就完成了这个工具类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;
using System.Text;
using System;
public enum E_Axis_Name
{
Horizontal,
Vertical,
Fire1,
Fire2,
Fire3,
Jump,
MouseX,
MouseY,
MouseScrollWheel
}
/// <summary>
/// 通过枚举名来获取虚拟轴的名字
/// </summary>
public static class GetAxisNameByEnum
{
public static List<string> strList = new List<string>();
//获取字串开始下标
public static int subStartIndex = 8;
/// <summary>
/// 改变InputMannger中的值时同时要更新List
/// </summary>
public static void AxisUpdate()
{
string path = @"D:\unity\UnityTeach1\ProjectSettings\InputManager.asset";
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs, Encoding.Default);
string content = string.Empty;
strList.Clear();
while ((content = sr.ReadLine()) != null)
{
content = content.Trim().ToString();
if (content.StartsWith("m_Name"))
{
strList.Add(content.Substring(8));
}
}
}
/// <summary>
/// 获取轴的名字
/// </summary>
/// <param name="name">枚举名</param>
/// <returns></returns>
public static string GetName(E_Axis_Name name)
{
return strList[Convert.ToInt32(name)];
}
}
思路就是去读取InputMannger这个文件,获得所有的轴名称,存入List中,然后再把枚举转换为下标去读取。
本人还在入门阶段,写的不好的希望大家能多指正。