CSV文件处理

脚本一CreateCSV用于创建CSV文件,:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
public class CreatCsv 
{

    private string csvName;

    public string CsvName
    {
        get { return csvName; }
        set { csvName = value; }
    }
    private string csvContent;

    public string CsvContent
    {
        get { return csvContent; }
        set { csvContent = value; }
    }
    private string strContent;
    private int nRowCount;
    private int nColCount;

    public CreatCsv()
    {

    }
    public CreatCsv(CSVClassFiles placeName)
    {
        this.csvName = placeName.configName;
        this.csvContent = placeName.content;
        CSVManager.Init.AddCsv(this.csvName, this);
    }

    public virtual void Init()
    {

    }
    public virtual void Clear()
    {

    }

    public bool ChackCSV()
    {
        strContent = CsvContent;
        string[] strTitle = strContent.Split('\n');
        string[] strTemp = strTitle[0].Split(',');
        for(int i = 0; i < strTemp.Length ; i++)
        {
           for(int j = i+1 ; j < strTemp.Length ; j++)
           {   
               if(strTemp[i] == strTemp[j])
                {
                    Debug.LogWarning(csvName + "表头有重复");
                    return false;
                }
           }
        }
        nColCount = strTemp.Length;
        return true;
    }
    public bool AnalyzeCSV()
    {
        string[] strTitle = strContent.Split('\n');

        for (int i = 0; i < strTitle.Length - 1; i++)
        {
            List<string> list = new List<string>();
            string[] strTemp = strTitle[i].Split(',');
            for (int j = 0; j < strTemp.Length; j++)
            {
                string str = strTemp[j];
                if (str.Contains("\r"))
                {
                    str = str.Replace("\r", "");
                }
                list.Add(str);
            }
            if(i >= 3)
            {
                dic[i-3] = list;
            }

            csvList.Add(list);
        }
        return true;
    }
    public int GetIntData(int nRow , int nClo)
    {
        if ((nRow + 1) > csvList.Count || (nClo + 1) > csvList[nRow].Count)
        {
            return 0;
        }
        int value;
        if (int.TryParse(csvList[nRow][nClo] , out value))
        {
            return value;
        }
        return 0;
    }

    public int GetRowCount()
    {
        return csvList.Count;
    }
    public int GetColCount()
    {
        if (GetRowCount() > 0)
        {
            return csvList[0].Count;
        }
        return 0;
    }

    public string GetStringData(int nRow, int nClo)
    {
        if ((nRow + 1) > csvList.Count || (nClo + 1) > csvList[nRow].Count)
        {
            return null;
        }
        return csvList[nRow][nClo];
    }
    public string GetTitle(int nClo)
    {
        return GetStringData(0, nClo);
    }
    public List<string> GetList(int nRow)
    {
        if (dic.ContainsKey(nRow))
        {
            return dic[nRow];
        }
        return null;
    }
    public List<List<string>> csvList = new List<List<string>>();
    public Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>();
}

脚本二不必介绍:

using UnityEngine;
using System.Collections;

public class CSVClassFiles : ScriptableObject
{
    public string configName;
    public string content;
}

脚本三CsvManager用于管理csv文件:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
using DAS;
public class CSVManager : Singleton{

    public delegate void CreatCSVCallBack(int csvCount);
    public delegate void CreatCSVDone();
    public CreatCSVDone CreatCSVDoneCallBack;
    public static CSVManager Init;
    private Dictionary<string, CreatCsv> dic = new Dictionary<string, CreatCsv>();
    private int csvCount = 0;
    public CSVManager():base(false)
    {

    }
    public override void SingletonUpdate(float fTime, float fDtime)
    {

    }

    public void RegisterCreatCSVDone(CreatCSVDone callback)
    {
        CreatCSVDoneCallBack += callback;
    }
    public void RemoveCreatCSVDone(CreatCSVDone callback)
    {
        CreatCSVDoneCallBack -= callback;
    }
    public void AddCsv(string str , CreatCsv csv)
    {
        dic[str] = csv;
    }
    public T GetCSV<T>(string str)
    {
        if (dic.ContainsKey(str))
        {
            object obj = (object)dic[str];
            return (T)obj;
        }
        return default(T);
    }
    public void LoadCSV()
    {
        LoadManager.AddDownRequest(ResourceType.Type_CSV, GlobleConfig.strFlieSer + GlobleConfig.allCSVPath, CreadCSV, LoadProCallBack, GlobleConfig.allCSVPath);
        LoadManager.AddDownRequest(ResourceType.Type_CSV, GlobleConfig.strFlieSer + GlobleConfig.allScenesInfoPath, CreadCSV, LoadProCallBack, GlobleConfig.allScenesInfoPath);
    }
    private void LoadProCallBack(float value, object customParam)
    {
        Debug.LogWarning("LoadPro = " + value + " url = " + (string)customParam);
    }
    private void CreadCSV(AssetBundle bundle, object customParam)
    {
        if (bundle != null)
        {
            UnityEngine.Object[] objs = bundle.LoadAll(typeof(CSVClassFiles));
            for (int num = 0; num < objs.Length; num++)
            {
                UnityEngine.Object obj = objs[num];
                if (obj == null)
                {
                    continue;
                }
                CSVClassFiles csv = obj as CSVClassFiles;
                if (csv.configName.Contains("cfg_Scene_"))
                {
                    SceneMgr sceneMgr = (SceneMgr)SingletonMgr.Inst.GetSingleton(SingletonDefine.s_SceneMgr);
                    ScenesCSV scenesCSV = new ScenesCSV(csv);
                    sceneMgr.AddSceneCSV(csv.configName, scenesCSV);
                }
                if (csv.configName == GlobleConfig.csvEquipStone)
                {
                    new EquipStoneCSV(csv);
                }
                else if (csv.configName == GlobleConfig.csvPlayerConfig1)
                {

                }
                else if (csv.configName == GlobleConfig.csvConfig)
                {

                }
                else if (csv.configName == GlobleConfig.csvOpenFun)
                {
                    new OpenFunCSV(csv);
                }
                else if (csv.configName == GlobleConfig.csvDeviceInfo)
                {
                    new DeviceInfoCSV(csv);
                } 
            }
            bundle.Unload(false);
            if (customParam != null)
            {
                //Debug.LogWarning("customParam = " + customParam);
                if (customParam == "Config/AllCSVInfo.unity3d")
                {
                }
            }
            csvCount++;
            if (csvCount == 2)
            {
                CreatCSVDoneCallBack();
            }
        }
    }
}

脚本4下载:

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using DAS;
public class LoadManager : Singleton
{
    public static LoadManager Init = null;
    public delegate void DownFinishDelegate(AssetBundle wwwObj, object customParam);
    public delegate void DownLoadProgressDelegate(float value, object customParam);
    private static Dictionary<string, AssetBundle> dic = new Dictionary<string, AssetBundle>();
    private static Dictionary<string, WWWRequest> m_WWWMap = new Dictionary<string, WWWRequest>();
    public LoadManager():base(false)
    {

    }
    public class WWWRequest
    {
        public ResourceType type;
        public string requestURl;
        public DownFinishDelegate calbackFun;
        public DownLoadProgressDelegate LoadProCallBack;
        public WWW wwwObject = null;
        public bool bHasDeal = false;
        public int loadSize;
        public List<object> customParams = new List<object>(); 
        public WWWRequest() { }
        public WWWRequest(ResourceType resType ,string url, DownFinishDelegate cbFun,DownLoadProgressDelegate LoadPro, object customParam = null)
        {
            type = resType;
            requestURl = url;
            calbackFun = new DownFinishDelegate(cbFun);
            LoadProCallBack = new DownLoadProgressDelegate(LoadPro);

            try
            {
                wwwObject = new WWW(url);
            }
            catch(Exception ex)
            {
                Debug.LogWarning(ex.Message);
            }
            customParams.Add(customParam);
        }

    }
    //WWW Request List

    public static void AddDownRequest(ResourceType type , string url, DownFinishDelegate callBackFun, DownLoadProgressDelegate LoadPro = null, object customParam = null)
    {
        if (url != "")
        {
            //增加新的资源下载需求
            if (!m_WWWMap.ContainsKey(url))
            {
                m_WWWMap.Add(url, new WWWRequest(type ,url, callBackFun, LoadPro, customParam));
            }
            else
            {
                //已经提交相同请求,但是没有下载完成
                if (!m_WWWMap[url].wwwObject.isDone)
                {
                    m_WWWMap[url].calbackFun += callBackFun;
                    m_WWWMap[url].customParams.Add(customParam);
                }
                //已下载资源,直接调用回调函数
                else
                {
                    callBackFun(m_WWWMap[url].wwwObject.assetBundle, customParam);
                }
            }
        }
    }


    public static void UnloadAllRes()
    {
        foreach (KeyValuePair<string, WWWRequest> wwwPair in m_WWWMap)
        {
            wwwPair.Value.wwwObject.assetBundle.Unload(true);
        }
        m_WWWMap.Clear();
        Resources.UnloadUnusedAssets();
        System.GC.Collect();
    }
    public static void UnloadSingleRes(string resName)
    {
        foreach (KeyValuePair<string, WWWRequest> wwwPair in m_WWWMap)
        {
            if (wwwPair.Key.Contains(resName))
            {
                wwwPair.Value.wwwObject.assetBundle.Unload(true);
                m_WWWMap.Remove(resName);
                Resources.UnloadUnusedAssets();
                break;
            }
        }
    }
    // Update is called once per frame
    public override void SingletonUpdate(float fTime, float fDtime)
    {

        foreach (KeyValuePair<string, WWWRequest> wwwPair in m_WWWMap)
        {
            WWWRequest wwwReq = wwwPair.Value;
            //try
            //{
            //    if (wwwReq.wwwObject == null || wwwReq.wwwObject.assetBundle == null)
            //    {
            //        continue;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Debug.LogWarning(ex.Message);
            //}

            //如果尚未调用回调,并且下载完成,则调用
            if ((!wwwPair.Value.bHasDeal && wwwReq.wwwObject.isDone && wwwReq.wwwObject.assetBundle != null))
            {
                for (int i = 0; i < wwwReq.calbackFun.GetInvocationList().GetLength(0); i++)
                {

                    ((DownFinishDelegate)wwwReq.calbackFun.GetInvocationList()[i]).Invoke(wwwReq.wwwObject.assetBundle, wwwReq.customParams[i]);
                    wwwReq.wwwObject.assetBundle.Unload(false);
                    //wwwReq.wwwObject.Dispose();

                }
                wwwPair.Value.bHasDeal = true;
            }
            //if (!wwwReq.wwwObject.isDone)
            //{
            //    for (int i = 0; i < wwwReq.calbackFun.GetInvocationList().GetLength(0); i++)
            //    {
            //        ((DownLoadProgressDelegate)wwwReq.LoadProCallBack.GetInvocationList()[i]).Invoke(wwwReq.wwwObject.progress, wwwReq.requestURl);
            //    }
            //}
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瓜皮肖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值