using UnityEngine;
using UnityEditor;
public class AssetControl
{
[MenuItem("Assets/Bundle/Assetbundle")]
static void BuildBundle()
{
//根据不同平台打不同的包
BuildTarget target =
#if UNITY_ANDROID
BuildTarget.Android;
#elif UNITY_IPHONE
BuildTarget.iOS;
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
BuildTarget.StandaloneWindows;
#else
BuildTarget.StandaloneWindows;
#endif
Debug.Log(target);
string dir = Application.dataPath + "StreamingAssets";
string path = EditorUtility.SaveFilePanel("save file", dir, "New Resoure", "unity3d");
if (path.Length!=0)
{
Object[] selects = Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);
BuildPipeline.BuildAssetBundle(Selection.activeObject,selects,path,BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.CompleteAssets,target);
}
AssetDatabase.Refresh();
}
}
//www加载资源
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public static readonly string pathurl=
#if UNITY_ANDROID
"jar:file://"+Application.dataPath+"!/assets/";
#elif UNITY_IPHONE
Application.dataPath+"/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
"file://" + Application.dataPath + "/StreamingAssets/";
#else
string.Empty;
#endif
// Use this for initialization
void Start () {
StartCoroutine("LoadAsset");
}
IEnumerator LoadAsset()
{
string path = pathurl + "New Resoure2.unity3d";
Debug.Log(path);
WWW www = new WWW(path);
yield return www;
AssetBundle bundle = www.assetBundle;
Object obj0 = bundle.LoadAsset("Cube");
Object obj1 = bundle.LoadAsset("Sphere");
Object obj2 = bundle.LoadAsset("Sphere 1");
//加载到游戏中
yield return Instantiate(obj0);
yield return Instantiate(obj1);
yield return Instantiate(obj2);
bundle.Unload(false);
}
}
当在编译器里debug时,应该选择平台为pc端,因为你打包好的东西是在项目的streamingassets文件夹中,所以在编译器debug时,说明assets是在pc端的文件夹中,选择了其他的平台编译,将会读取不到资源。