0评论

Unity3D资源管理——Excel文件转化成ScriptableObject

文章来自https://blog.csdn.net/qq_28474981/article/details/82844856 2019-02-19 302浏览

想免费获取内部独家PPT资料库?观看行业大牛直播?点击加入腾讯游戏学院游戏策划行业精英群361498939

考虑到Unity中推荐的游戏配表数据的储存方式是ScriptableObject,所以这里就制作了一个工具,该工具可以自动将excel表自动转为ScriptableObject。

一、开发环境

Unity2017.2f

二、主要思路

通过之前文章实现的将Excel自动创建为对应的数据结构文件,从而读取Excel中存储的数据并转化为ScriptableObject。

三、主要代码

(1)数据文件
using System;
using UnityEngine;
using System.Collections.Generic;
using ResourceSystem;
public class ResourcesInfo:ScriptableObject,IDataCollectionTemplete
{
    public List<ResourcesData>  Data = new List<ResourcesData>();
    /// <summary>
    /// 获得内容Type
    /// </summary>
    public Type GetContenType()
    {
        return typeof(ResourcesData);
    }
    /// <summary>
    /// 转换成当前内容类型的列表
    /// </summary>
    public void ConvertToContentList(List<IDataTemplete> contents)
    {
       for(int i = 0; i < contents.Count; i++)
       {
           Data.Add((ResourcesData)contents[i]);
       }
    }
}
[Serializable]
public class ResourcesData:IDataTemplete
{
    /// <summary>
    /// 索引号
    /// </summary>
    public string ID ;
    /// <summary>
    /// 名称
    /// </summary>
    public string Name ;
    /// <summary>
    /// 路径
    /// </summary>
    public string Path ;
    /// <summary>
    /// 版本
    /// </summary>
    public string version ;
    /// <summary>
    /// 依赖
    /// </summary>
    public List<string> Dependencys ;
    /// <summary>
    /// 被依赖的数量
    /// </summary>
    public int DependeciedNum ;
    /// <summary>
    /// 构造方法
    /// </summary>
    public ResourcesData()
    {
        ID= default(string);
        Name= default(string);
        Path= default(string);
        version= default(string);
        Dependencys= new List<string>();
        DependeciedNum= default(int);
    }
    /// <summary>
    /// 反序列化
    /// </summary>
    public void DeSerialize(object[] content)
    {
        ID = content[0].ToString();
        Name = content[1].ToString();
        Path = content[2].ToString();
        version = content[3].ToString();
        Dependencys = ConvertUtils.GetStringListByString(content[4].ToString());
        DependeciedNum = Int32.Parse(content[5].ToString());
    }
}

(2)核心代码
    private static void GenerateExcelToScriptableObject(string typeName,string path, string targetPath)
    { 
        ScriptableObject instance = ScriptableObject.CreateInstance(typeName);
        if (instance == null)
        {
            return;
        }
        //读取Excel文件
        List<IDataTemplete> content = ResourceSystemFacade.Inst.ReadExcel(((IDataCollectionTemplete)instance).GetContenType(), path);
        ((IDataCollectionTemplete)instance).ConvertToContentList(content);
        string[] paths = targetPath.Split('/');
        string finalPath = "";
        bool isStart = false;
        for(int i = 0; i < paths.Length; i++)
        {
            if (paths[i] == "Assets")
            {
                isStart = true;
            }
            if (isStart)
            {
                finalPath += paths[i] + "/";
            }
        }
        DirectoryInfo dir = new DirectoryInfo(finalPath);
        if (!dir.Exists)
        {
            Directory.CreateDirectory(targetPath);
        }
        string initAssetPath = finalPath + typeName + ".asset";
        AssetDatabase.DeleteAsset(initAssetPath);
        AssetDatabase.CreateAsset(instance, initAssetPath);
    }
完整可运行工程可以看http://gad.qq.com/article/detail/289285里的Git库