博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
unity3D中动态加载物体的常用的方法
阅读量:5076 次
发布时间:2019-06-12

本文共 3083 字,大约阅读时间需要 10 分钟。

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;
}
}

 

转载于:https://www.cnblogs.com/Wj8177/p/6087042.html

你可能感兴趣的文章
判断请求是否为ajax请求
查看>>
【POJ2699】The Maximum Number of Strong Kings(网络流)
查看>>
spring boot配置跨域
查看>>
BZOJ 1996 合唱队(DP)
查看>>
进击吧!阶乘——大数乘法
查看>>
安卓学习资料推荐-25
查看>>
linux设置静态ip
查看>>
C++ 学习笔记 变量和基本类型(一)
查看>>
python-输出颜色显示
查看>>
HDU - 5744 Keep On Movin
查看>>
[MySQL Reference Manual] 20 分区
查看>>
OO第三单元总结
查看>>
Linux编译提速
查看>>
.net下Global.asax使用
查看>>
在传统以太网中,为什么要有最小帧长度和最大帧长度的限制?
查看>>
泛微云桥e-birdge之金蝶云之家集成配置手册
查看>>
oracle plsql 统计
查看>>
Nginx + uWSGI 部署Django 项目,并实现负载均衡
查看>>
迭代器和生成器
查看>>
GestureDetector
查看>>