编辑代码

using System;

[CreateAssetMenu(fileName="PlayerData", menuName="Data/PlayerData")]
public class PlayerData : ScriptableObject
{
    //public int Id{get{return GetInstanceID();}}

    public float jumpForce;
    public float moveSpeed;
    public float rotateSpeed;
}

public class CSVExporter:ScriptableWizard
{
    public string _ScriptableObjectClassName = "";

    private string _SaveTo = "";
    
    public sring SaveTo
    {
        get
        {
            return _SaveTo;
        }
        private set
        {
            _SaveTo = value;
        }
    }

    private Type type = null;

    [MenuItem("Tools/Excel Config/Export Excel Template")]
    static void ExportXml()
    {
        ScritableWizard.DisplayWizard<CSVExporter>("Export Excel Template", "Export", "SelectPath");
    }

    void OnWizardUpdate()
    {
        isValue = false;

        if(_ScriptableObjectClassName == "")
        {
            errorString = "Enter ScriptableObjectClassName";
            return;
        }

        type = Type.GetType(_ScriptableObjectClassName);
        if(type == null)
        {
            errorString = "Invalid class name!";
            return;
        }

        isValue = true;
        if(SaveTo == "")
        {
            errorString = "Select save path";
            return;
        }

        errorString = " ";

    }

    void OnWizardCreate()
    {
        ExportCSVMethod(_ScriptableObjectClassName, SaveTo, _ScriptableObjectClassName)
    }

    void OnWizardOtherButton()
    {
        SaveTo = EditorUtility.OpenFolderPanel("SaveTo", "", "") + "/";
        if(SaveTo != "")
        {
            helpString = "SavePath:\"" + SaveTo + "\"";
            return;
        }
    }

    // 将 指定类里面的 字段 导出为 文本格式
    public void ExportCSVMethod(string typeName, string path, string fileName)
    {
        Type type = Type.GetType(typeName);
        string filePath = path + fileName + ".csv";
        string content = string.Empty;
        using(StreamWriter sw = new StreamWrite(filePath))
        {
            FieldInfo[] fieldInfos = type.GetFields();
            int count = 0;
            foreach(FieldInfo fieldInfo in fieldInfos)
            {
                if(!fieldInfo.IsNotSerialized)
                {
                    if(count > 0)
                    {
                        sw.Write(',');
                    }
                    sw.Write(fieldInfo.Name);
                    count++;
                }
            }
        }
    }
}

public class CSVImporter
{
    [MenuItem("Tools/Excel Config/Import Excel Data")]
    public static void ImportXmlTemplateMethod()
    {
        DirectoryInfo info = new DirectoryInfo("Assets");
        if(!info.Exists) info.Create();

        string path = EditorUtility.OpenFilePanel("Import Excel Data", "", "csv");
        if(string.IsNullOrEmpty(path)) return;

        // 要把要导入的文件 复制到 Unity 的 Asset 目录下
        string[] split = path.Split(',');
        string fileName = split[split.Length - 1].Split('.')[0];
        string copyPath = "Assets/" + fileName + ".csv";

        TextAsset text = AssetDatabase.LoadMainAssetAtPath(copyPath) as TextAsset;
        if(text != null)
        {
            AssetDatabase.DeleteAsset(copyPath);
        }

        // 导入到unity
        FileUtil.CopyFileOrDirecotry(path, Application.dataPath + "/" + fileName + ".csv");
        AssetDatabase.ImportAsset(copyPath);

        //读取文件
        text = AssetDatabase.LoadMainAssetAtPath(copyPath) as TextAsset;
        if(text == null)
        {
            return;
        }

        // get scriptable object
        Type dataType = Type.GetType(fileName);
        if(dataType == null)
        {
            AssetDatabase.DeleteAsset(copyPath);
            Debug.Log("Don't have this name of ScriptableObject: " + fileName);
            return;
        }

        string importPath = EditorUtility.SaveFilePanel("Save Data Path", Application.dataPath, "data", ".asset");
        if(string.IsNullOrEmpty(importPath)) return;

        string relativePath = importPath.Split(new string[]{"Assets"}, StringSplitOptions.None)[1];
        string[] divides = relativePath.Split('/');
        string saveFolder = "Assets";

        for(int i=0; i<divides.Length-1; i++)
        {
            saveFolder += divides[i] + "/";
        }

        // get field
        string[] rows = text.text.Split(new string[]{"\n"}, System.StringSplitOptions.RemoveEmptyEntries);
        for(int i=0; i<rows.Length; i++)
        {
            ros[i] = rows[i].TrimEnd('\r');
        }
        string[] columns = rows[0].Split(',');
        List<FieldInfo> fieldInfos = new List<FieldInfo>();
        List<bool> multis = new List<bool>();
        foreach(string column in columns)
        {
            string col = column.TrimEnd();
            string fieldName = col.Split('+')[0];
            FieldInfo field = dataType.GetField(fieldName);
            if(field != null)
            {
                fieldInfos.Add(field);
                bool multi = col.Contains("+");
                multis.Add(multi);
            }
            else
            {
                Debug.Log(fieldName);
            }
        }

        // create data
        for(int i=1; i<rows.Length; i++)
        {
            if(rows[i] == null) continue;

            columns = rows[i].Split(',');
            string assetPath = saveFolder + columns[0] + ".asset";
            ScritableObject asset = ScritableObject.CreateInstance(fileName);//T data = new T()

            for(int j=0; j<fieldInfos.Count; j++)
            {
                object value = StringConvert.ToValue(fieldInfos[j].FieldType, column[j], multis[j]);
                fieldInfos[j].SetValue(asset, value);
            }
            AssetDatabase.CreateAsset(asset, assetPath);
            AssetDatabase.ImportAsset(assetPath);
            AssetDatabase.Refresh();
        }
        AssetDatabase.DeleteAsset(copyPath);
    }
}