先上几种打包代码,记住 BuildTarget.iPhone
BuildTarget.Android 别混用了。
using UnityEngine;
using UnityEditor;
public class ExportAssetBundles : MonoBehaviour {
[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
static void ExportResource () {
// Bring up save panel
string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
if (path.Length != 0) {
// Build the resource file from the active selection.
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies, BuildTarget.iPhone);
Selection.objects = selection;
}
}
[MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
static void ExportResourceNoTrack () {
// Bring up save panel
string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
if (path.Length != 0) {
// Build the resource file from the active selection.
BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
}
}
[MenuItem ("Assets/Build AssetBundle From Selection") ]
static void ExportBundle()
{
string str = EditorUtility.SaveFilePanel("Save Bundle...", Application.dataPath, Selection.activeObject.name, "unity3d");
if (str.Length != 0){
BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, str, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android);
}
}
}
场景打包
@MenuItem ("Build/BuildWebplayerStreamed")
static function MyBuild(){
var path = EditorUtility.SaveFilePanel("Build Bundle","","*","unity3d");
var levels : String[] = ["Assets/E.unity"];
BuildPipeline.BuildStreamedSceneAssetBundle( levels, path, BuildTarget.iPhone);
//BuildPipeline.BuildPlayer( levels, "StreamedWebplayer.unity3d",BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
}
下面是www加载场景资源包
Application.LoadLevelAdditive(scenename); 参数是打包的场景名称,不是包名称,别搞错了
IEnumerator loadNetScene(string url,string scenename)
{
// Wait for the Caching system to be ready
while (!Caching.ready)
yield return null;
// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
using(WWW www = WWW.LoadFromCacheOrDownload (url,1))
{
yield return www;
if (www.error != null)
{
throw new Exception("WWW download had an error:" + www.error);
//Debug.LogError(www.error);
//yield return false;
}
bundle = www.assetBundle;
Application.LoadLevelAdditive(scenename);
yield return new WaitForSeconds(0.5f);
www.assetBundle.Unload(false);
Resources.UnloadUnusedAssets();
}
}
下面是www加载prefab资源包
GameObject obj = bundle.Load("prefabname",typeof(GameObject))asGameObject; "prefabname"是包内要加载的物体名字,不是包名,别搞错了
IEnumerator loadNetPrefab(string url)
{
// Wait for the Caching system to be ready
while (!Caching.ready)
yield return null;
// Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
using(WWW www = WWW.LoadFromCacheOrDownload (url,1))
{
yield return www;
if (www.error != null)
{
throw new Exception("WWW download had an error:" + www.error);
//Debug.LogError(www.error);
//yield return false;
}
bundle = www.assetBundle;
GameObject obj = bundle.Load("prefabname", typeof(GameObject))as GameObject;
objPrefab = Instantiate(obj)as GameObject;
}
yield return new WaitForSeconds(0.5f);
bundle.Unload(false);
Resources.UnloadUnusedAssets();
}