• 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

  • 相关阅读:
    Android网络项目课程笔记-----系统设置_首选项框架&Holo风格的设置
    Android网络项目课程笔记-----加载数据
    Android网络项目课程笔记-----代码复用性
    Android网络项目课程笔记-----AdapterView嵌套
    Android网络项目课程笔记-----滑动Banner
    Android网络项目课程笔记-----滑动Tab&Banner
    Android网络项目课程笔记-----页面结构
    Android网络项目课程笔记-----欢迎页面新手引导
    C#调用SAP S4/HANA Gateway Service
    CefSharp试用
  • 原文地址:https://www.cnblogs.com/hisiqi/p/3202915.html
Copyright © 2020-2023  润新知