分包加载
AssetbundleLoad.js脚本
var house : GameObject;
var carpet : Object;
var mirror : GameObject;
var textures : GameObject;
var mainobject : GameObject;
var names : String;
function Start()
{
var www=new WWW("http://www.xxx.com/test/AssetBundle.unity3d"); //从服务器加载
//var www=new WWW("file:///C:/1/AssetBundle.unity3d"); //从本地加载,绝对路径
yield www;
//一起选择Room,Carpet和Mirror的Prefab后导出,那么Room会成为mainAsset ,其他的用AssetBundle.Load("name")来加载
//mainobject=Instantiate(www.assetBundle.mainAsset);
house=Instantiate(www.assetBundle.Load("Room"));
carpet=Instantiate(www.assetBundle.Load("asd")); //奇怪的是,就是它的名字不能写成carpet或者Carpet
mirror=Instantiate(www.assetBundle.Load("Mirror"));
textures=Instantiate(www.assetBundle.Load("Textures"));
names=www.assetBundle.mainAsset.name;
//www.assetBundle.Unload(false);
renderer.material.mainTexture=textures.GetComponent("abc").A;
}
function Update ()
{
}
abc.js
var A: Texture2D;
var B: Texture2D;
var C: Texture2D;
function Update ()
{
}
AssetbundleBuild.cs
/// <summary>
/// Exports a player compatible AssetBundle containing the selected objects, including dependencies.
/// </summary>
/// <remarks>Editor scripts are removed from the exported Assets.</remarks>
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
class AssetbundleEditor
{
[MenuItem("Assets/Build AssetBundle")]
public static void ExportPlayerAssetBundle()
{
string tempRelativePath = "Assets/Temp/";
string tempAbsolutePath = Application.dataPath + "/../" + tempRelativePath;
// Bring up save dialog.
string path = EditorUtility.SaveFilePanel("Save AssetBundle", "", "AssetBundle", "unity3d");
if (path.Length > 0)
{
// Get all selected objects.
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//Object[] processedSelection = new Object[selection.Length];
Object[] processedSelection=selection;
for (int i = 0; i < selection.Length; i++)
{
// Clone the original object.
Object currentObject = selection[i];
bool isPrefab = currentObject != null && currentObject.GetType() == typeof(GameObject);
if (isPrefab)
{
if (!Directory.Exists(tempAbsolutePath))
Directory.CreateDirectory(tempAbsolutePath);
// Remove unneeded scripts from the prefab.
/*
Object clonedPrefab = EditorUtility.CreateEmptyPrefab(string.Format("{0}{1}.prefab", tempRelativePath, currentObject.name));
if (clonedPrefab != null)
{
clonedPrefab = EditorUtility.ReplacePrefab((GameObject)currentObject, clonedPrefab);
//EditorBase component = ((GameObject)clonedPrefab).GetComponent(typeof(EditorBase)) as EditorBase;
UnityEditor component = ((GameObject)clonedPrefab).GetComponent(typeof(UnityEditor)) as UnityEditor;
if (component != null)
GameObject.DestroyImmediate(component, true);
EditorUtility.SetDirty(clonedPrefab);
processedSelection[i] = clonedPrefab;
}
*/
}
}
// Save changes to AssetDatabase and import processed prefabs.
EditorApplication.SaveAssets();
AssetDatabase.Refresh();
// Export the processed AssetBundle.
BuildPipeline.BuildAssetBundle(Selection.activeObject, processedSelection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
Selection.objects = selection;
// Remove all cloned objects from the project.
for (int i = 0; i < processedSelection.Length; i++)
{
if (processedSelection[i] != null)
AssetDatabase.DeleteAsset(string.Format("{0}{1}.prefab", tempRelativePath, processedSelection[i].name));
}
if (Directory.Exists(tempAbsolutePath))
Directory.Delete(tempAbsolutePath, false);
}
}
}
本条目发布于2010年3月8日。属于Unity脚本分类。