1.用Resources.Load();參數為路徑,需要在Assets文件夾中創建Resources文件夾,通過路徑去查找,實例化並加入到內存中去,通過Instantiate動態加載的方法來實現物體場景的加載;
2.使用AssetBundle打包預設或者場景可以將與其相關的所有資源打包,這樣很好地解決資源的依賴問題
要先打包資源:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class AesstBundleTest : MonoBehaviour {
[MenuItem("Custom Bundle/Create Bundel Main")]
public static void creatBundleMain()
{
//獲取選擇的對象的路徑
Object[] os = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
bool isExist = Directory.Exists(Application.dataPath + "/StreamingAssets");
if (!isExist)
{
Directory.CreateDirectory(Application.dataPath + "/StreamingAssets");
}
foreach (Object o in os)
{
string sourcePath = AssetDatabase.GetAssetPath(o);
string targetPath = Application.dataPath + "/StreamingAssets/" + o.name + ".assetbundle";
if (BuildPipeline.BuildAssetBundle(o, null, targetPath, BuildAssetBundleOptions.CollectDependencies))
{
print("create bundle cuccess!");
}
else
{
print("failure happen");
}
AssetDatabase.Refresh();
}
}
[MenuItem("Custom Bundle/Create Bundle All")]
public static void CreateBundleAll()
{
bool isExist = Directory.Exists(Application.dataPath + "/StreamingAssets");
if (!isExist)
{
Directory.CreateDirectory(Application.dataPath + "/StreamingAssets");
}
Object[] os = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
if (os == null || os.Length == 0)
{
return;
}
string targetPath = Application.dataPath + "/StreamingAssets/" + "All.assetbundle";
if (BuildPipeline.BuildAssetBundle(null, os, targetPath, BuildAssetBundleOptions.CollectDependencies))
{
print("create bundle all cuccess");
}
else
{
print("failure happen");
}
AssetDatabase.Refresh();
}
}
把上面的代碼放在Editor中,在菜單欄中就可以看見自定的菜單項,選中需要打包的預設,就可以把對應的預設打包並輸出到StreamAssets中了
下面就是加載了:
using UnityEngine;
using System.Collections;
public class LoadBundleTest : MonoBehaviour {
//不同平台下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
// Update is called once per frame
void Update () {
}
void OnGUI()
{
if (GUILayout.Button("Load Bundle Main"))
{
string path_shpere = PathURL + "MySpherePreb.assetbundle";
StartCoroutine(loadBundleMain(path_shpere));
string path_cube = PathURL + "MyCubePreb.assetbundle";
StartCoroutine(loadBundleMain(path_cube));
print(path_cube);
}
if (GUILayout.Button("Load Bundle All"))
{
StartCoroutine(loadBundleAll(PathURL + "All.assetbundle"));
}
}
private IEnumerator loadBundleMain(string path)
{
WWW bundle = new WWW(path);
// yield return bundle;
Instantiate(bundle.assetBundle.mainAsset);
bundle.assetBundle.Unload(false);
yield return 1;
}
private IEnumerator loadBundleAll(string path)
{
WWW bundle = new WWW(path);
yield return bundle;
Instantiate(bundle.assetBundle.Load("MyCubePreb"));
Instantiate(bundle.assetBundle.Load("MySpherePreb"));
yield return 1;
}
}