您的位置:首页 > 移动开发 > Unity3D

【转】unity3d动态加载及生成配置文件

2012-07-11 22:36 489 查看
本文大部分转载,作者做了关于配置文件生成工作,但是很遗憾,关于position和rotation信息目前尚未自动生成,运行本例的朋友,需要自己手动添加位置和角度信息,否则程序会报错。
标准的json数据:

{

"AssetList" : [{

"Name" : "Chair 1",

"Source" : "Prefabs/Chair001.unity3d",

"Position" : [2,0,-5],

"Rotation" : [0.0,60.0,0.0]

},

{

"Name" : "Chair 2",

"Source" : "Prefabs/Chair001.unity3d",

"Position" : [1,0,-5],

"Rotation" : [0.0,0.0,0.0]

},

{

"Name" : "Vanity",

"Source" : "Prefabs/vanity001.unity3d",

"Position" : [0,0,-4],

"Rotation" : [0.0,0.0,0.0]

}

}]

}

用Unity3D制作基于web的网络游戏,不可避免的会用到一个技术-资源动态加载。比如想加载一个大场景的资源,不应该在游戏的开始让用户长时间等待全部资源的加载完毕。应该优先加载用户附近的场景资源,在游戏的过程中,不影响操作的情况下,后台加载剩余的资源,直到所有加载完毕。

在讲述代码之前,先想象这样一个网络游戏的开发流程。首先美工制作场景资源的3D建模,游戏设计人员把3D建模导进Unity3D,托托拽拽编辑场景,完成后把每个gameobject导出成XXX.unity3d格式的资源文件(参看BuildPipeline),并且把整个场景的信息生成一个配置文件,xml或者Json格式(本文使用Json)。最后还要把资源文件和场景配置文件上传到服务器,最好使用CMS管理。客户端运行游戏时,先读取服务器的场景配置文件,再根据玩家的位置从服务器下载相应的资源文件并加载,然后开始游戏,注意这里并不是下载所有的场景资源。在游戏的过程中,后台继续加载资源直到所有加载完毕。
json.txt:(注:当生成的json无法读取时,记得改一下编码格式 改成 ANSI)
{"AssetList":[{"Name":"Sphere","Source":"Prefabs/Sphere.unity3d"},{"Name":"cube","Source":"Prefabs/cube.unity3d"},{"Name":"Sphere","Source":"Prefabs/Sphere.unity3d"},{"Name":"cube","Source":"Prefabs/cube.unity3d"}]}

主程序:

using UnityEngine;
using System.Collections;

public class MainMonoBehavior : MonoBehaviour {

public delegate void MainEventHandler(GameObject dispatcher);
public event MainEventHandler StartEvent;
public event MainEventHandler UpdateEvent;
public void Start()
{
ResourceManager.getInstance().LoadSence("Scenes/json.txt");//json配置文件
if(StartEvent != null)
{
StartEvent(this.gameObject);
}
}
public void Update()
{
if (UpdateEvent != null)
{
UpdateEvent(this.gameObject);
}
}
}


这里面用到了C#的事件机制,大家可以看看我以前翻译过的国外一个牛人的文章。C# 事件和Unity3D
在 start方法里调用ResourceManager,先加载配置文件。每一次调用update方法,MainMonoBehavior会把update 事件分发给ResourceManager,因为ResourceManager注册了MainMonoBehavior的update事件。

辅助类:
View Code

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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: