此文简单的说明下,在unity编辑器中导出材质(Material),并在运行时使用WWW加载到游戏中.
1.从editor中先导出(材质)资源文件.
#pragma strict
#pragma implicit
#pragma downcast
import System.Collections.Generic;
//////////////////////////////////////////////////////
///场景,组件,导出工具.
//////////////////////////////////////////////////////
/** 上次场景保存的名字 */
private static var lastSceneName:String = "";
/** 常量 */
private static var strSaveScene:String = "指定保存场景的位置";
private static var lastDirPath:String = "";
/** 导出的场景后缀 */
private static var strUnity3d:String = "unity3d";
/** 场景后缀 */
private static var strSceneExtendName:String = "unity";
private static var strSceneExportError:String = "no scene file!";
private static var strExport:String = "导出";
/**
导出场景
*/
@MenuItem("Level4/Export/ExportScene")
static function BuildStreaedScene(){
var pathList:List.<String> = new List.<String>();
var objs:Object[] = Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);
for(var obj:UnityEngine.Object in objs){
var path:String = AssetDatabase.GetAssetPath(obj);
if(path.Contains(strSceneExtendName)){
pathList.Add(path);
}
}
if(pathList.Count > 0){
var locationPath:String = EditorUtility.SaveFilePanel(strSaveScene,"",lastSceneName,strUnity3d);
if(!String.IsNullOrEmpty(locationPath)){
BuildPipeline.BuildStreamedSceneAssetBundle(pathList.ToArray(),locationPath,BuildTarget.WebPlayer);
OpenDir(locationPath);
}
}else{
Debug.Log(strSceneExportError);
}
pathList.Clear();
pathList = null;
}
/**
导出素材
*/
@MenuItem("Level4/Export/ExportResource")
static function ExportResource(){
if(Selection.activeObject){
var srcName:String = Selection.activeObject.name;
var path:String = EditorUtility.SaveFilePanel(strExport,"",srcName,strUnity3d);
if(path.Length != 0){
BuildPipeline.BuildAssetBundle(Selection.activeObject,Selection.objects,path);
}
}else{
Debug.Log("Project中选中要导出的物体.");
}
}
/**
导出素材和素材依赖
*/
@MenuItem("Level4/Export/ExportResource Track Dependence(multi2one)")
static function ExportResourceTrack(){
if(Selection.activeObject){
var srcName:String = Selection.activeObject.name;
lastDirPath = PlayerPrefs.GetString("lastDirPath","");
var path:String = EditorUtility.SaveFilePanel(strExport,lastDirPath,srcName,strUnity3d);
if(path){
PlayerPrefs.SetString("lastDirPath",System.IO.Directory.GetParent(path).ToString());
var objs:Object[] = Selection.GetFiltered(Object,SelectionMode.DeepAssets);
BuildPipeline.BuildAssetBundle(Selection.activeObject,objs,path,BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
Selection.objects = objs;
//打开导出的目录
OpenDir(path);
}
}else{
Debug.Log("Project中选中要导出的物体.");
}
}
@MenuItem("Level4/Export/ExportResources Track Dependence(one by one)")
static function ExportResourcesTrack(){
var objs:UnityEngine.Object[] = Selection.objects;
var path:String = null;
for(var obj:UnityEngine.Object in objs){
var srcName:String = obj.name;
lastDirPath = PlayerPrefs.GetString("lastDirPath","");
path = EditorUtility.SaveFilePanel(strExport,lastDirPath,srcName,strUnity3d);
if(path){
PlayerPrefs.SetString("lastDirPath",System.IO.Directory.GetParent(path).ToString());
BuildPipeline.BuildAssetBundle(obj,null,path,BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.CompleteAssets);
Selection.activeObject = obj;
}else{
//全部取消.
break;
}
}
if(path){
OpenDir(path);
}
}
/**
打开目录
*/
static function OpenDir(path:String){
if(EditorUtility.DisplayDialog("注意","打开刚才导出文件的目录?","是","否")){
path = System.IO.Directory.GetParent(path).ToString();
System.Diagnostics.Process.Start(path);
}
}
2.使用WWW类,在运行期加载导出的材质文件.
function LoadMat(url:String){
Debug.Log(url);
var www:WWW = WWW(url);
yield www;
// Debug.Log(typeof www.assetBundle.mainAsset);
var mat:Material = www.assetBundle.mainAsset; //这里获取下载的材质.可直接应用到模型.
}
其余的素材资源,如果模型fbx,声音等.导出后在运行时加载到游戏中的方式类似哦.
导出场景(关卡)的方式则不同: