1、打包需要用Android的路徑打包
using UnityEngine;
using System.Collections;
using UnityEditor;
public class Test : Editor
{
[MenuItem("Custom Editor/Create AssetBunldes Main")]
static void CreateAssetBunldesMain()
{
//獲取在Project視圖中選擇的所有游戲對象
Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//遍歷所有的游戲對象
foreach (Object obj in SelectedAsset)
{
//本地測試:建議最后將Assetbundle放在StreamingAssets文件夾下,如果沒有就創建一個,因為移動平台下只能讀取這個路徑
//StreamingAssets是只讀路徑,不能寫入
//服務器下載:就不需要放在這里,服務器上客戶端用www類進行下載。
string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";
//BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies
if (BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android))
{
Debug.Log(obj.name + "資源打包成功");
}
else
{
Debug.Log(obj.name + "資源打包失敗");
}
}
//刷新編輯器
AssetDatabase.Refresh();
}
[MenuItem("Custom Editor/Create AssetBunldes ALL")]
static void CreateAssetBunldesALL()
{
Caching.CleanCache();
string Path = Application.dataPath + "/StreamingAssets/ALL.assetbundle";
Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (Object obj in SelectedAsset)
{
Debug.Log("Create AssetBunldes name :" + obj);
}
//這里注意第二個參數就行
if (BuildPipeline.BuildAssetBundle(null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies))
{
AssetDatabase.Refresh();
}
else
{
}
}
[MenuItem("Custom Editor/Create Scene")]
static void CreateSceneALL()
{
//清空一下緩存
Caching.CleanCache();
string Path = Application.dataPath + "/MyScene.unity3d";
string[] levels = { "Assets/Level.unity" };
//打包場景
BuildPipeline.BuildPlayer(levels, Path, BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
AssetDatabase.Refresh();
}
}
2、加載也需要用Android的路徑加載
using UnityEngine;
using System.Collections;
using System.IO;
public class RunScript : MonoBehaviour
{
string DownPath = "";
////不同平台下StreamingAssets的路徑是不同的,這里需要注意一下。
//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
void OnGUI()
{
if (GUI.Button(new Rect(50, 100, 100, 100), "Main Assetbundle"))
{
StartCoroutine(LoadMainGameObject("http://10.131.229.166/MyItems/Cube.assetbundle"));
StartCoroutine(LoadMainGameObject("jar:file://" + Application.dataPath + "!/assets/" + "Cube.assetbundle"));
}
}
//讀取一個資源
private IEnumerator LoadMainGameObject(string path)
{
WWW bundle = new WWW(path);
yield return bundle;
//加載到游戲中
yield return Instantiate(bundle.assetBundle.mainAsset);
Debug.Log("+++++++++++++++++++++++++++Load successful!!");
bundle.assetBundle.Unload(false);
}
//從服務器下載資源
private IEnumerator DownLoadToLocal(string url)
{
WWW.EscapeURL(url); //url編碼
WWW www = new WWW(url);//訪問url
WWW.UnEscapeURL(url); //url解碼
string filename = url.Substring(url.LastIndexOf('/') + 1); //根據URL獲取文件的名字。
yield return www; //等待下載
if (www.error == null)
{
FileStream fs = File.Create(Application.persistentDataPath + "/" + filename); //path為你想保存文件的路徑。
DownPath = Application.persistentDataPath + "/" + filename; //把下載的數據寫到文件
fs.Write(www.bytes, 0, www.bytes.Length);
fs.Close();
Debug.Log(filename + "下載完成");
}
else
{
Debug.Log(www.error);
}
}
}
文件夾的名字也不要弄錯了。