总体思路就是 想办法先把配置表内容搞成.asset配置文件再打包

直接给个示例吧,感觉更方便看懂

比如工程目录下有个Excel表,路径如图所示(演示用,你也可以自己设置要放在哪,详见代码注释)

Android手机的内置存储根目录unity怎么读取_System

表的内容如下图,注意表的名称和Sheet名称,一会用得到

Android手机的内置存储根目录unity怎么读取_unity_02

那就写代码呗,需要用到三个dll,分别是

EPPlus.dll

Android手机的内置存储根目录unity怎么读取_github_03

https://github.com/mltqiao/UnityAssetsAndPackages/blob/main/Assets/Plugins/Excel%20Import%20Dlls/EPPlus.dll

Excel.dll

Android手机的内置存储根目录unity怎么读取_github_03

https://github.com/mltqiao/UnityAssetsAndPackages/blob/main/Assets/Plugins/Excel%20Import%20Dlls/Excel.dll

ICSharpCode.SharpZipLib.dll

Android手机的内置存储根目录unity怎么读取_github_03

https://github.com/mltqiao/UnityAssetsAndPackages/blob/main/Assets/Plugins/Excel%20Import%20Dlls/ICSharpCode.SharpZipLib.dll

想自己下就搜搜,懒得搜我也给你整了个github仓库,可以直接下

下载完放到Assets/Plugins路径里就行,如图

Android手机的内置存储根目录unity怎么读取_System_06


然后搞个生成.asset配置文件功能的DataSamples.cs,放到Assets/Editor路径里

上代码吧

using System.Collections.Generic;
using System.Data;
using System.IO;
using Excel;
using UnityEngine;
using UnityEditor;

/// <summary>
/// 用来存放数据的结构体
/// </summary>
[System.Serializable]
public struct Sample
{
    public int ID;
    public string Name;
    public float Age;
}

/// <summary>
/// 用来存放多个结构体的List,并继承自ScriptableObject类
/// </summary>
public class DataSamples: ScriptableObject
{
    public List<Sample> samples;
}

public class SampleExcelAccess
{
    /// <summary>
    /// 读取Excel;需要添加Excel.dll、System.Data.dll;
    /// </summary>
    /// <param name="excelName">excel文件名</param>
    /// <param name="sheetName">sheet名称</param>
    /// <returns>DataRow的集合</returns>
    static DataRowCollection ReadExcel(string excelName, string sheetName)
    {
        // 设置需要读取的Excel的路径 我为了省事直接扔到工程目录/Assets里 你可以自己选择要读取的路径
        string path = Application.dataPath + "/" + excelName;
        FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();
        
        // 设置需要读取的Sheet
        // tables可以按照sheet名获取,也可以按照sheet索引获取
        // return result.Tables[0].Rows;
        return result.Tables[sheetName].Rows;
    }
    
    // 需要读取的Excel表的名字
    public static string Excel = "sample";
    
    public static List<Sample> ReadSampleExcel()
    {
        // 补全Excel表的名字
        string excelName = Excel + ".xlsx";
        // 选择想要读取的Excel表的Sheet名
        string sheetName = "Sample1";
        // 读取目标Excel表
        DataRowCollection collect = ReadExcel(excelName, sheetName);
        // 用下面创建的Sample结构体组成的List来存读到的内容
        List<Sample> collectedSampleArray = new List<Sample>();
        // 跳过第0行,从表的第1行开始,遍历每一行
        for (int i = 1; i < collect.Count; i++)
        {
            // 当本行的第0列没有内容时,跳过这一行(或中断break,根据自己表的内容的需要,也可以不写)
            if (collect[i][0].ToString() == null) continue;

            // 第0列为ID 第1列为姓名 第2列为年龄
            Sample sample = new Sample
            {
                ID = int.Parse(collect[i][0].ToString()),
                Name = collect[i][1].ToString(),
                Age = float.Parse(collect[i][2].ToString())
            };
            // 将这个结构体添加进List中
            collectedSampleArray.Add(sample);
        }
        return collectedSampleArray;
    }
}

// 继承自Editor类,需要UnityEditor引用
public class CreatSampleExcelAsset : Editor
{
    // 在Editor界面添加一个MenuItem来执行上面的代码
    [MenuItem("BuildAsset/Build ExcelSample Asset")]
    public static void BuildExcelSampleAsset()
    {
        DataSamples dataSamples = CreateInstance<DataSamples>();
        // 执行刚才写的那堆代码
        dataSamples.samples = SampleExcelAccess.ReadSampleExcel();
        // 输出的.asset路径和名称
        string path = "Assets/Resources/Data/SampleData.asset";
        // 创建.Asset
        AssetDatabase.CreateAsset(dataSamples, path);
        // 刷新Assets
        AssetDatabase.Refresh();
    }
}

找到刚才添加的按钮,点一下

Android手机的内置存储根目录unity怎么读取_github_07


然后就好了,在你设置的路径下面多了一个.asset配置文件,里面就是读到的Excel内容

Android手机的内置存储根目录unity怎么读取_List_08