Unity中安卓和IOS读取Excel表

1 篇文章 0 订阅
1 篇文章 0 订阅
**#Unity快速读取Excel表**

项目开发过程中,策划们可能会配置很多的数据放在不同的Excel当中,如商城物品等,那么我们程序就需要通过读取Excel的内容,已供程序使用。
下面是读取的Excel表内容:
Excel表
首先在Unity中导入以下dll文件:
dll文件
dll下载地址:
链接:https://pan.baidu.com/s/1iCmAn1sHpdzeaZY1u1x4Sw
提取码:njvm

加载Excel的代码方法:
代码

加载Excel中的行列中的数据代码:
代码
注意:红圈中的字符名要和Excel表的Sheet名一致!(如下图)
Excel
创建一个字典用来存储Excel的数据!
字典

Ok,Excel表的数据已经全部存储到了字典中!当然,这个方法只适用于Unity编辑器模式,如果需要在IOS或者安卓中读取表请参照以下方法:
把Excel中的内容编辑成.asset文件:
读取:

public class ExcelAccess 
{
    public static string Excel = "Excel/config";
    //查询eyes表
    public static List<Eyes> SelectEyesTable()
    {
        string excelName = Excel + ".xlsx";
        string sheetName = "eyes";
        DataRowCollection collect = ExcelAccess.ReadExcel(excelName, sheetName);

        List<Eyes> eyesArray = new List<Eyes>();
        for (int i = 1; i < collect.Count; i++)
        {
            if (collect[i][1].ToString() == "") continue;

            Eyes menu = new Eyes
            {
               ID = int.Parse(collect[i][0].ToString()),
               realName = collect[i][1].ToString(),
               unlock_Gold = float.Parse(collect[i][2].ToString()),
               unlock_Stage = float.Parse(collect[i][3].ToString()),
               up_Gold = float.Parse(collect[i][4].ToString()),
               up_glod_Growth = float.Parse(collect[i][5].ToString()),
               basic_add_Score = float.Parse(collect[i][6].ToString()),
               up_add_Score = float.Parse(collect[i][7].ToString()),
            };
            eyesArray.Add(menu);
        }
        return eyesArray;
    }

    public static List<Animal> SelectAnimalTable()
    {
        string excelName = Excel + ".xlsx";
        string sheetName = "animal";
        DataRowCollection collect = ExcelAccess.ReadExcel(excelName, sheetName);
        List<Animal> animalArray = new List<Animal>();
        for (int i = 1; i < collect.Count; i++)
        {
            if (collect[i][1].ToString() == "") continue;

            Animal animal = new Animal
            {
               ID = int.Parse(collect[i][0].ToString()),
               realName = collect[i][1].ToString(),
               hp = int.Parse(collect[i][2].ToString()),
               dropGold = int.Parse(collect[i][3].ToString()),
            };
            animalArray.Add(animal);
        }
        return animalArray;
    }

    /// <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)
    {
        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();
        //int columns = result.Tables[0].Columns.Count;
        //int rows = result.Tables[0].Rows.Count;

        //tables可以按照sheet名获取,也可以按照sheet索引获取
        //return result.Tables[0].Rows;
        return result.Tables[sheetName].Rows;
    }
}

/// <summary>
/// 基于ScriptObject的BookHolder类
/// </summary>
public class BookHolderAnimal: ScriptableObject
{
    public List<Animal> animal;
}
public class BookHolderEyes:ScriptableObject
{
    public List<Eyes> eyes;
}


[System.Serializable]
public struct Eyes
{
    public float ID;//id
    public string realName;//读取的名字
    public float unlock_Gold;//金币资源
    public float unlock_Stage;//第几关卡
    //升级金币=基础金币+2的(升级等级-1)平方*金币系数
    public float up_Gold;//基础金币  
    public float up_glod_Growth;//金币系数
    //升级后的附加属性=基础附加属性+(升级等级-1)*升级附加属性系数
    public float basic_add_Score;//基础附加属性
    public float up_add_Score;//升级附加属性系数
}
[System.Serializable]
public class Animal
{
    public int ID;//id
    public string realName;//读取的名字
    public int hp;//血量
    public int dropGold;//过关的金币
}

打包成.asset文件:

public class CreatAessets : Editor
{
    [MenuItem("BuildAsset/Build ExcelEyes Asset")]
    public static void ExcuteEyesBuild()
    {
        BookHolderEyes holder = ScriptableObject.CreateInstance<BookHolderEyes>();
        //查询excel表中数据,赋值给asset文件
        holder.eyes= ExcelAccess.SelectEyesTable();
        string path = "Assets/Resources/Excel/ExcelEyes.asset";
        AssetDatabase.CreateAsset(holder, path);
        AssetDatabase.Refresh();
    }

    [MenuItem("BuildAsset/Build ExcelAnimal Asset")]
    public static void ExcuteAnimalBuild()
    {
        BookHolderAnimal holder = ScriptableObject.CreateInstance<BookHolderAnimal>();
        //查询excel表中数据,赋值给asset文件
        holder.animal = ExcelAccess.SelectAnimalTable();
        string path = "Assets/Resources/Excel/ExcelAnimal.asset";
        AssetDatabase.CreateAsset(holder, path);
        AssetDatabase.Refresh();
    }
}

打包出来的文件:
1
读取:

string eyesName = "Excel/ExcelEyes";
        string animalName = "Excel/ExcelAnimal";
        BookHolderEyes asset = Resources.Load<BookHolderEyes>(eyesName);
        foreach (Eyes e in asset.eyes)
        {
            Debug.Log("eyesID:" + e.ID);
            Debug.Log("eyesName:" + e.realName);
            Debug.Log("eyesUnlockGlod:" + e.unlock_Gold);
            Debug.Log("eyesStage:" + e.unlock_Stage);
            Debug.Log("eyesUpGold:" + e.up_Gold);
            Debug.Log("eyesGlodGrowth:" + e.up_glod_Growth);
            Debug.Log("eyesBasicScore:" + e.basic_add_Score);
            Debug.Log("eyesUpaddScore:" + e.up_add_Score);
        }
        BookHolderAnimal holder = Resources.Load<BookHolderAnimal>(animalName);
        foreach (Animal item in holder.animal)
        {
            Debug.Log("animId:" + item.ID);
            Debug.Log("animName:" + item.realName);
            Debug.Log("animHp:" + item.hp);
            Debug.Log("animDropGold:" + item.dropGold);
        }

记得把这两个类单独分离出来!记得把这两个类单独分离出来!记得把这两个类单独分离出来!
分离
Ok 就介绍到这里了!

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值