unity3d 场景配置文件生成代码

using UnityEngine;
using UnityEditor;
using System.IO;
using System;
using System.Text;
using System.Collections.Generic;
using LitJson;
public class BuildAssetBundlesFromDirectory
{
      static List<JsonResource> config=new List<JsonResource>();
      static Dictionary<string, List<JsonResource>> assetList=new Dictionary<string, List<JsonResource>>();
        [@MenuItem("Asset/Build AssetBundles From Directory of Files")]//这里不知道为什么用"@",添加菜单
        static void ExportAssetBundles ()
        {//该函数表示通过上面的点击响应的函数
                   assetList.Clear();
                   string path = AssetDatabase.GetAssetPath(Selection.activeObject);//Selection表示你鼠标选择激活的对象
                  Debug.Log("Selected Folder: " + path);
       
       
                  if (path.Length != 0)
                  {
                           path = path.Replace("Assets/", "");//因为AssetDatabase.GetAssetPath得到的是型如Assets/文件夹名称,且看下面一句,所以才有这一句。
                            Debug.Log("Selected Folder: " + path);
                           string [] fileEntries = Directory.GetFiles(Application.dataPath+"/"+path);//因为Application.dataPath得到的是型如 "工程名称/Assets"
                           
                           string[] div_line = new string[] { "Assets/" };
                           foreach(string fileName in fileEntries)
                           {
                                    j++;
                                    Debug.Log("fileName="+fileName);
                                    string[] sTemp = fileName.Split(div_line, StringSplitOptions.RemoveEmptyEntries);
                                    string filePath = sTemp[1];
                                     Debug.Log(filePath);
                                    filePath = "Assets/" + filePath;
                                    Debug.Log(filePath);
                                    string localPath = filePath;
                                    UnityEngine.Object t = AssetDatabase.LoadMainAssetAtPath(localPath);
                                   
                                     //Debug.Log(t.name);
                                    if (t != null)
                                    {
                                            Debug.Log(t.name);
                                            JsonResource jr=new JsonResource();
                                            jr.Name=t.name;
                                            jr.Source=path+"/"+t.name+".unity3d";
                                              Debug.Log( t.name);
                                            config.Add(jr);//实例化json对象
                                              string bundlePath = Application.dataPath+"/../"+path;
                                               if(!File.Exists(bundlePath))
                                                {
                                                   Directory.CreateDirectory(bundlePath);//在Asset同级目录下相应文件夹
                                                }
                                              bundlePath+="/"  + t.name + ".unity3d";
                                             Debug.Log("Building bundle at: " + bundlePath);
                                             BuildPipeline.BuildAssetBundle(t, null, bundlePath, BuildAssetBundleOptions.CompleteAssets);//在对应的文件夹下生成.unity3d文件
                                    } 
                           }
                            assetList.Add("AssetList",config);
                            for(int i=0;i<config.Count;i++)
                            {
                                    Debug.Log(config[i].Source);
                            }
                  }
                string data=JsonMapper.ToJson(assetList);//序列化数据
                Debug.Log(data);
        string jsonInfoFold=Application.dataPath+"/../Scenes";
        if(!Directory.Exists(jsonInfoFold))
        {
            Directory.CreateDirectory(jsonInfoFold);//创建Scenes文件夹
        }
                string fileName1=jsonInfoFold+"/json.txt";
                if(File.Exists(fileName1))
                {
                    Debug.Log(fileName1 +"already exists");
                    return;
                }
                UnicodeEncoding uni=new UnicodeEncoding();
                  
                using(  FileStream  fs=File.Create(fileName1))//向创建的文件写入数据
                {
                        fs.Write(uni.GetBytes(data),0,uni.GetByteCount(data));
                        fs.Close();
                }
      }
}

 http://www.cnblogs.com/U-tansuo/archive/2012/07/11/2587173.html

posted @ 2013-07-20 19:10  斯玛特琦  阅读(1534)  评论(0编辑  收藏  举报