初步整理并且学习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的动态实例化
//把资源加载到内存中
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;
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 == )
{
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 ;
} 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 ;
}
}

unity3d Resources.Load动态加载资源的更多相关文章

  1. 【Unity3D】Unity3D之 Resources.Load 动态加载资源

    [Unity3D]Unity3D之 Resources.Load 动态加载资源 1.Resources.Load:使用这种方式加载资源,首先需要下Asset目录下创建一个名为Resources的文件夹 ...

  2. unity3d动态加载资源

    在Unity3D的网络游戏中实现资源动态加载 分类: 最新学习2012-06-14 13:35 1127人阅读 评论(0) 收藏 举报 网络游戏nullvectorjson游戏string 用Unit ...

  3. 从高德 SDK 学习 Android 动态加载资源

    前不久跑去折腾高德 SDK 中的 HUD 功能,相信用过该功能的用户都知道 HUD 界面上的导航转向图标是动态变化的.从高德官方导航 API 文档中 AMapNaviGuide 类的描述可知,导航转向 ...

  4. 动态加载资源文件(ResourceDictionary)

    原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<App ...

  5. Style样式的四种使用(包括用C#代码动态加载资源文件并设置样式)

    Posted on 2012-03-23 11:21 祥叔 阅读(2886) 评论(6) 编辑 收藏 在Web开发中,我们通过CSS来控制页面元素的样式,一般常用三种方式: 1.       内联样式 ...

  6. [UE4]一个好用的虚幻4插件,根据资源名称动态加载资源,GetCurrentLeveName(获得当前地图名称)

    下载地址 一.下载与UE4相对应的版本 二.在工程根目录新建Plugins目录,解压插件. 三.如果工程已经打开,则需要重新打开   四.重新打开工程后,右下角会有提示有新插件可用. 五.这个插件提供 ...

  7. JavaScript动态加载资源【js|css】示例代码

    在开发过程中会用到各种第三方的插件,或者自己写在单独文件中的js方法库或者css样式,在html头部总是需要写一大堆的script和link标签,如果想要自己实现动态的引入资源文件,可以使用开源的re ...

  8. WPF 界面实现多语言支持 中英文切换 动态加载资源字典

    1.使用资源字典,首先新建两个字典文件en-us.xaml.zh-cn.xaml.定义中英文的字符串在这里面[注意:添加xmlns:s="clr-namespace:System;assem ...

  9. JavaScript动态加载资源

    //动态加载样式 function dynamicLoadingCss(path){ if(!path || path.length === 0){ return false; } var head ...

随机推荐

  1. golang http 中间件

    golang http 中间件 源码链接 golang的http中间件的实现 首先实现一个http的handler接口 type Handler interface { ServeHTTP(Respo ...

  2. Altium Designer入门学习笔记2:使用原创客3D元件库

    请自行淘宝购买: 元件库列表(2018年11月27日): 问题一:在项目库或已安装的库中找不到? 将"原创客"提供的文件全部添加到libraries中!"原创客" ...

  3. Git for Windows 工具的使用(二)

    Git分支  当一个人开发功能A而另一个人开发功能B,之后代码进行整合的时候,使代码既有功能A也有功能B.在Git中,Git给了我们分支的概念. 分支可以使用我们快速的开发协作,并且快速的合并. 分支 ...

  4. Linux学习-检验软件正确性

    md5sum / sha1sum / sha256sum 目前有多种机制可以计算文件的指纹码,我们选择使用较为广泛的 MD5, SHA1 或 SHA256 加密机 制来处理,我们拿NTP 软件来检查看 ...

  5. Notepad++ WebEdit插件

    虽然PHP的IDE有很多,但是,我还是比较喜欢用Notepad++这款编辑器,因为比较轻量级,而且用起来比较顺手. 但是最近在改别人写的代码的时候,经常要在选定的php前后插入<?php  ?& ...

  6. [POJ 1008] Maya Calendar C++解题

        Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 62297   Accepted: 192 ...

  7. Python学习——第一天

    https://www.runoob.com/python/python-chinese-encoding.html 第一个python程序 [root@mini1 ~]# vi python01.p ...

  8. bat 中的特殊符号输出问题

    系统关键字(感叹号!)冲突 由于是自动化部署,因此需要使用到循环,这里就不可避免的用到了延迟变量(setlocal enabledelayedexpansion) 有关延迟变量的知识,大家可以通过这篇 ...

  9. linux基础(Vi编辑器)

    整理的linux vi编辑器命令 Vi编辑器,进入方式,输入vi file即可进入编辑模式 1.vi模式(Linux严格区分大小写) Vi所学到的几种模式 模式 主要用途 相应操作 对应命令 普通模式 ...

  10. javascript学习笔记 - 引用类型 Array

    二 Array 1.可以通过length属性删除或创建新的数组项 arr = [1,2,3]; arr.length = 4;//增加 [1,2,3,undefined] arr.length = 2 ...