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

1.开发环境

Unity2017.2f

2.主要思路

        通过之前文章实现的将Excel自动创建为对应的数据结构文件(https://blog.csdn.net/qq_28474981/article/details/82529529),从而读取Excel中存储的数据并转化为ScriptableObject。

 

3.主要代码

(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);

    }

完整可运行工程可以看https://blog.csdn.net/qq_28474981/article/details/82749021里的Git库

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值