////从文件初始化关卡信息(Level)
    //private void InitLevelModel(ref Dictionary<string, LevelModel> _levelModel, string _path)
    //{
    //    if (_levelModel == null)
    //    {
    //        _levelModel = new Dictionary<int, LevelModel>();
    //    }
    //    RDFileStream.ReadLevelTable(ref _levelModel, _path);
    //}

    //从CSV表初始化Dictionary
    private static Dictionary <string, T> LoadCsvData <T>(string _fileName)
    {
        Dictionary <string, T> dic = new Dictionary <string, T>();

        /* 从CSV文件读取数据 */
        Dictionary <string, Dictionary <string, string> > result = RDFileStream.ReadCsvFile(_fileName);

        /* 遍历每一行数据 */
        foreach (string name in result.Keys)
        {
            /* CSV的一行数据 */
            Dictionary <string, string> datas = result[name];

            /* 读取Csv数据对象的属性 */
            PropertyInfo[] props = typeof(T).GetProperties();
            /* 使用反射,将CSV文件的数据赋值给CSV数据对象的相应字段,要求CSV文件的字段名和CSV数据对象的字段名完全相同 */
            T obj = Activator.CreateInstance <T>();
            foreach (PropertyInfo p in props)
            {
                ReflectUtil.PiSetValue <T>(datas[p.Name], p, obj);
            }

            /* 按name-数据的形式存储 */
            dic[name] = obj;
        }

        return(dic);
    }
Example #2
0
    // Use this for initialization
    static void CreatPrefabAndBuildAssetBundles()
    {
        List <AssetBundleBuild> assestBuildMap     = new List <AssetBundleBuild>(); //AssetBundel Map
        List <string>           assestPathList     = new List <string>();           //Assest路径+文件名
        List <SceneTileModel>   sceneTileModelList = new List <SceneTileModel>();
        int        lastId = RDFileStream.ReadCsvFile("SceneTile.csv").Count;
        GameObject root   = GameObject.Find("Root");

        foreach (Transform levelTheme in root.transform)
        {
            //清空上一场景AssestPath
            assestPathList.Clear();
            AssetBundleBuild assetBundle = new AssetBundleBuild();
            //以关卡类型命名
            assetBundle.assetBundleName = levelTheme.name + ".package";

            if (levelTheme.gameObject.activeSelf)
            {
                //路径:Assets/Prefabs/Scene/关卡类型名/prefab名.prefab
                string prefabPath = "Assets/Prefabs/Scene/" + levelTheme.name + "/";
                if (!Directory.Exists(prefabPath))
                {
                    Directory.CreateDirectory(prefabPath);
                }
                foreach (Transform tile in levelTheme)
                {
                    if (tile.gameObject.activeSelf)
                    {
                        string prefabFullPath = prefabPath + tile.name + ".prefab";
                        //创建prefab
                        Object prefab = PrefabUtility.CreatePrefab(prefabFullPath, tile.gameObject);
                        PrefabUtility.ReplacePrefab(tile.gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
                        //增加到assestPath
                        assestPathList.Add(prefabFullPath);
                        //创建CSV表-行
                        SceneTileModel row = new SceneTileModel();
                        row.name      = tile.name;
                        row.type      = tile.gameObject.isStatic ? 1 : 2;
                        row.levelType = levelTheme.name;
                        sceneTileModelList.Add(row);
                    }
                }

                assetBundle.assetNames = assestPathList.ToArray();
                if (assetBundle.assetNames.Length > 0)
                {
                    assestBuildMap.Add(assetBundle);
                }
            }
        }
        if (sceneTileModelList.Count > 0)
        {
            RDFileStream.WriteCsvFile("SceneTile.csv", sceneTileModelList.ToArray());
            sceneTileModelList.Clear();
        }
        if (assestBuildMap.Count > 0)
        {
            //AssestBundle路径:StreamingAssets/AssestBundles
            BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath + "/AssestBundles", assestBuildMap.ToArray(), BuildAssetBundleOptions.None, RDPlatform.isOSXEditor ? BuildTarget.StandaloneOSX : BuildTarget.StandaloneWindows);
            Debug.Log("Success");
        }
    }