unity3dResources.Load动态加载资源
  初步整理并且学习unity3d资源加载⽅法,预计⽤时两天完成⼊门学习Unity3d常⽤两种加载资源⽅案:Resources.Load和AssetBundle
Resources.Load就是从⼀个缺省打进程序包⾥的AssetBundle⾥加载资源⽽⼀般AssetBundle⽂件需要你⾃⼰创建,运⾏时动态加载,
可以指定路径和来源的。其实场景⾥所有静态的对象也有这么⼀个加载过程,只是Unity后台替你⾃动完成
⼀:Resources.Load:使⽤这种⽅式加载资源,⾸先需要下Asset⽬录下创建⼀个名为Resources的⽂件夹,这个命名是U3D规定的⽅式,然后把资源⽂件放进去,
当然也可以在Resources中再创建⼦⽂件夹,当然在代码加载时需要添加相应的资源路径,下⾯是⼀个简demo,两个预
设,Cube和Sphere,
其中Cube放在Resource中的Prebs中,⽽Sphere放在Resources跟⽬录下,下⾯分别实现Resources.Load资源的加载
using UnityEngine;
using System.Collections;
public class LoadResDemo : MonoBehaviour {
private string cubePath = "Prebs/MyCubePreb";
private string spherePath = "MySpherePreb";
void Start () {
//把资源加载到内存中
Object  cubePreb = Resources.Load(cubePath, typeof(GameObject));
//⽤加载得到的资源对象,实例化游戏对象,实现游戏物体的动态加载
GameObject cube = Instantiate(cubePreb) as GameObject;
//以下同理实现Sphere的动态实例化
/
/把资源加载到内存中unity3d入门
Object spherePreb = Resources.Load(spherePath, typeof(GameObject));
//⽤加载得到的资源对象,实例化游戏对象,实现游戏物体的动态加载
GameObject sphere = Instantiate(spherePreb) as GameObject;
}
void Update () {
}
}
  将上⾯的脚本附加到某个游戏对象上,在运⾏游戏时就可以看到场景中动态创建的上⾯的游戏对象了
上⾯是第⼀种使⽤Resources.Load()的⽅式动态加载游戏对象的,然⽽在项⽬中更长⽤的却是第⼆种使⽤AssetBundle的⽅式动态加载游戏对象。
使⽤AssetBundle打包预设或者场景可以将与其相关的所有资源打包,这样很好地解决资源的依赖问题,使得我们可以⽅便的加载GameObject
⾸先需要打包资源:
using UnityEngine;
using System.Collections;
using UnityEditor;
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 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 return1;
}
private IEnumerator loadBundleAll(string path)
{
WWW bundle = new WWW(path);
yield return bundle;
Instantiate(bundle.assetBundle.Load("MyCubePreb"));
Instantiate(bundle.assetBundle.Load("MySpherePreb"));
yield return1;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。