关于unity3d ugui图集使用中Resources.Load的使用

扫一扫,访问微社区
后使用快捷导航没有帐号?
签到成功!您今天第{todayrank}个签到,签到排名竞争激烈,记得每天都来签到哦!已连续签到:{constant}天,累计签到:{days}天
当前位置: &
查看: 2008|回复: 2
如何使用unity中Resources文件夹
7排名<font color="#FF昨日变化1主题帖子积分
在线时间665 小时
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
才可以下载或查看,没有帐号?
本帖最后由 caedmom 于
10:25 编辑
如何使用unity中Resources文件
unity有几种特殊文件夹。其中一个文件是Resources文件夹。存储资源的简单概念已经在官方文档中被很好的解释:
通常,我们在场景中创建资源的实例以便在游戏设置中使用它们。不过unity还允许从脚本即时加载资源。通过将资源放在一个叫Resources 的文件夹或它的子文件夹(实际上可以有任意数量的Resources文件夹并可以放置在工程中的任意位置)中完成这步。用Resources.Loadfunction就可以加载这些资源。
对于为什么使用Resources文件件的原因或许还有些不清楚。首先要明白unity构建过程是如何工作的,还有就是unity怎样访问游戏资源。
Unity build processunity构建过程构建游戏之前,需要声明游戏中包括哪些场景。这些都可以在Build Settings窗口来完成。
01.png (60.04 KB, 下载次数: 1)
09:46 上传
至少有两个unity为什么会要求这样做原因:
它需要知道哪个场景被首先加载(最上边的场景)
它需要知道在构建的项目中包括哪些资源(相关性)
什么是场景相关性?他们是以任何方式连接到场景层级的资源,通常作为组件。
02.png (73.01 KB, 下载次数: 1)
09:46 上传
Unity Logo对象包括引用自Unity Logo资源的Sprite Renderer对象
依赖关系图表如下:
03.png (28.43 KB, 下载次数: 2)
09:46 上传
这种情况下有两个场景。Scene 1使用Asset 1 和Asset 2。Scene 2使用Asset 2 和 Asset 3。如果决定不构建Scene 2会发生什么?
04.png (33.67 KB, 下载次数: 1)
09:46 上传
由于Asset 3只是被Scene 2引用了,所以在构建时只包含了Asset 1 和 Asset 2,Asset3就不再被包括到构建中了。幸亏有了这种以来跟踪,unity会在构建中包括实际用到的资源。更不用说不需要担心存储暂时用不到的资源了。它不会影响构建项目的大小。
Override!重写!有一种方法可以绕过这个过程。如果把资源放入了Resources文件夹,它们就会被包括到你的构建中。不过要小心!这需要很好的理由才能做!
正如之前所说,大多数情况下当需要使用资源时,会在场景中做一个引用。这种方法可以很容易的使用任何附加资源。所以为什么要使用一个没有保持引用的资源?或许有一些原因并且每个原因都取决于特殊的需要,不过让我们看一个多数游戏都常见的案例。
当资源从场景中被直接引用时,在场景启动之前就会被加载到内存中。庆幸的是,玩家不会经历由于场景加载(伴随一些小的异常)导致的帧率下降。代价当然是需要时间来加载这些资源。有时也许是不可接受的。
Example – loading screen withdifferent backgrounds示例——加载不同背景的场景
05.jpg (66.08 KB, 下载次数: 1)
09:46 上传
很多游戏加载场景时播放随机图片来显得不是很无聊
加载场景有时候也是一个场景。让我们想象一种情况,真正游戏加载的时候当你想在背景显示随机的图片时。收集了15张图并把它们添加到了加载场景图片旋转的脚本。它们工作的很棒,不过当玩游戏的时候会意识到加载场景需要的时间比通过实际游戏关卡的时间要长!这是由资源预加载机制引起的,并且可以通过使用Resources文件夹很容易修复这个。首先从场景中移除所有纹理的引用。然后把图片放到Resources/LoadingImages目录,如下:
06.png (4.01 KB, 下载次数: 2)
09:46 上传
然后在代码中可以使用如下代码:[C#] 纯文本查看 复制代码int texturesCount = 3;
int textureId = Random.Range(1, texturesCount + 1);
Texture2D texture = Resources.Load&Texture2D&(&LoadingImages/& + textureId);
注意Random.Range()返回了一个介于包含第一个参数和不包含第二个参数之间的随机数,这就是为什么会有+1。如果需要给这个纹理附加一个Image组件,你可以这样做:[C#] 纯文本查看 复制代码Sprite sprite = Sprite.Create(
new Rect(0, 0, texture.width, texture.height),
new Vector2(0.5f, 0.5f));
Image image = GetComponent&Image&();
image.sprite =
A word of caution一个字的警告Resources 文件夹只有在真正需要的时候才去用。即时加载资源会使FPS 下降,间接依赖关系会使工作更加困难。值得再次提到的是这些资源会一直存在于构建当中,即时没有使用它们。已经警告你啦!
原文作者:PIOTR KORZUSZEK原文链接:
扫描下方二维码关注官方微信~每日都有精选干货与你分享呦~
蛮牛微信.jpg (20.98 KB, 下载次数: 1)
10:13 上传
本文由蛮牛译馆倾情奉献,翻译:xiaojiejie,审校:caedmom,如有请及时联系,除 合作社区 及 合作媒体 外,禁止转载。
unity 动态加载序列图; 图表插件;unity logo炫特效;unity 跟踪;unity 打开文件夹;unity resources.unity文件夹;unity文件夹说明;unity特殊文件夹;unity resources load sprite
每日推荐:
71804/5000排名<font color="#FF昨日变化46主题帖子积分
日久生情, 积分 1804, 距离下一级还需 3196 积分
日久生情, 积分 1804, 距离下一级还需 3196 积分
蛮牛币2754
在线时间627 小时
谢谢分享& && && && && && && && && && &&&
每日推荐:
142/50排名<font color="#FF昨日变化9主题帖子积分
注册看看, 积分 42, 距离下一级还需 8 积分
注册看看, 积分 42, 距离下一级还需 8 积分
在线时间14 小时
3qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
每日推荐:
经过游戏蛮牛认证的蛮牛小翻译温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
static def Load(path as string) as
static function Load(path: string, systemTypeInstance: Type): ;
static Load(string path, Type systemTypeInstance);
static def Load(path as string, systemTypeInstance as Type) as
Parameters
Pathname of the target folder.
systemTypeInstance
Type filter for objects returned.
Description
Loads an asset stored at path in a Resources folder.
Returns the asset at path if it can be found otherwise returns null. Only objects of type will be returned if this parameter is supplied. The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted.
Note:All asset names & paths in Unity use forward slashes, paths using backslashes will not work.
// Assigns a texture named "glass" to a
function Start () {
var go = new ();
go.renderer.material.mainTexture = ("glass", );
using UnityE
using System.C
public class Example :
void Start() {
go.renderer.material.mainTexture = ("glass", typeof());
import UnityEngine
import System.Collections
public class Example():
def Start() as void:
go.renderer.material.mainTexture = ('glass', typeof())
// Instantiates a prefab named "enemy" located in any
// folder in your project's Assets folder. function Start () {
var instance :
= Instantiate(("enemy", ));
using UnityE
using System.C
public class Example :
void Start() {
instance = Instantiate(("enemy", typeof()));
import UnityEngine
import System.Collections
public class Example():
def Start() as void:
instance as
= Instantiate(('enemy', typeof()))
static function Load(path: string): T;
static T Load(string path);
static def Load(path as string) as T
Parameters
Pathname of the target folder.
Description
Loads an asset stored at path in a Resources folder.
Returns the asset at path if it can be found otherwise returns null. Only objects of type T will be returned. The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted.
Note:All asset names & paths in Unity use forward slashes, paths using backslashes will not work.
// Assigns a texture named "glass" to a
function Start () {
var go = new ();
go.renderer.material.mainTexture = Resources.Load.&Texture2D&("glass");
using UnityE
using System.C
public class Example :
void Start() {
go.renderer.material.mainTexture = Resources.Load&Texture2D&("glass");
import UnityEngine
import System.Collections
public class Example():
def Start() as void:
go.renderer.material.mainTexture = [of ]('glass')
// Instantiates a prefab named "enemy" located in any
// folder in your project's Assets folder. function Start () {
var instance = Instantiate(Resources.Load.&GameObject&("enemy"));
using UnityE
using System.C
public class Example :
void Start() {
instance = Instantiate(Resources.Load&GameObject&("enemy"));
阅读(4584)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'Resources.Load',
blogAbstract:'from:/Documentation/ScriptReference/Resources.Load.html\r\nstatic '
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}鏌ョ湅: 3309|鍥炲?: 2
Unity鍔犺浇澶栭儴璧勬簮
褰撳墠绂荤嚎
涓婚?甯栧瓙璐$尞
鐢垫?鐩磋揪
鏈?笘鏈

我要回帖

更多关于 unity3d使用什么语言 的文章

 

随机推荐