打包assetbundle程序
using UnityEngine;
using System.Collections;
using UnityEditor;
public class AssetBundleTest : Editor
{
[MenuItem("Custom Editor/WebPlayer")]
static void WebPlayer()
{
Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (Object obj in SelectedAsset)
{
string targetPath = Application.dataPath + "/Asset/" + obj.name + ".assetbundle";
if (BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.WebPlayer))
{
Debug.Log(obj.name + "资源打包成功");
}
else
{
Debug.Log(obj.name + "资源打包失败");
}
}
//刷新编辑器
AssetDatabase.Refresh();
}
[MenuItem("Custom Editor/Android")]
static void Android()
{
Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (Object obj in SelectedAsset)
{
string targetPath = Application.dataPath + "/Asset/" + obj.name + ".assetbundle";
if (BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android))
{
Debug.Log(obj.name + "资源打包成功");
}
else
{
Debug.Log(obj.name + "资源打包失败");
}
}
//刷新编辑器
AssetDatabase.Refresh();
}
}
加载程序
using UnityEngine;
using System.Collections;
public class loadModel : MonoBehaviour
{
public string RUL = null;
//"file://F:\\1.unity3d";
WWW www;
bool isDownLoad = false;
public GameObject fatherObject;
void Start()
{
StartCoroutine(LoadAssetbundle(RUL));
}
IEnumerator LoadAssetbundle(string url)
{
// Start a download of the given URL
// 开始从指定路径下载
www = WWW.LoadFromCacheOrDownload(url, 1);
// Wait for download to complete
// 等待下载完成
yield return www;
if(!www.isDone)
{
print("123");
}
if (www.error != null)
{
Debug.Log (www.error);
yield return null;
}
// Instantiate(www.assetBundle.mainAsset);
GameObject gameObj = GameObject.Instantiate(www.assetBundle.mainAsset, transform.position, Quaternion.identity) as GameObject;
gameObj.transform.parent = fatherObject.transform;
}
void Update()
{
if (!www.isDone)
{
isDownLoad = false;
}
else
{
isDownLoad = true;
}
}
void OnGUI()
{
if(!isDownLoad)
{
GUI.Label(new Rect(720f / 1627.0f * Screen.width, 300f / 915.0f * Screen.height, 200f / 1627.0f * Screen.width, 50f / 915.0f * Screen.height), "正在下载请等待);
}
}
}