Resources文件夹

 

In the editor, Resources.FindObjectsOfTypeAll can be used to locate assets and scene objects.

1.

Assets文件夹下面可以存在多个"Resources"文件夹,每个"Resources"文件夹下的资源都可以通过Resources.Load访问:

static Object  Load(string path);
static Object  Load(string path, TypesystemTypeInstance);
返回类型为Object,通常需要强制转换为所需的类型
 
static TLoad(stringpath);
泛型函数,不需要强制转换
如:go.renderer.material.mainTexture = Resources.Load<Texture2D>("glass");Load方法返回的类型就是<>中的Texture2D
path是相对于Resources文件夹的路径名,包含要加载的资源文件名,不包含扩展名.
另外,Load一个GameObject(prefab)后返回的只是资源,而不是场景中的物体。
比如
    GameObject go= Resources.Load<GameObject>("scene1/Road_1"+"_"+i));
   GameObject h=Instantiate(go)as GameObject;
go是Resources文件下面的prefab,h是这个prefab实例化后在场景中的物体,需要区分开来
 
2.
Resources.LoadAll
static functionLoadAll(path: string): Object[];
static Object[]LoadAll(stringpath);
static defLoadAll(path as string) as Object[]
static functionLoadAll(path: string,systemTypeInstance:  Type): Object[];
static Object[]LoadAll(stringpath, TypesystemTypeInstance);
static defLoadAll(path as string,systemTypeInstance as Type) as Object[]
 
如果path指向文件夹名,将返回该文件夹下的所有资源,如果path指向文件名,将只返回这个资源。

在Unity中,路径名使用正斜杠,反斜杠无效。

在Windows及Unity中路径名都会不区分大小写的

 LoadAll也有泛型版本。

 

3.

Resources.LoadAssetAtPath

static Object LoadAssetAtPath(string assetPath, Type type);

同样有一个泛型函数

由于一些资源可能包含多个物体,如FBX文件可能包含mesh和很多object,avatar等,这时加载这个FBX文件,返回的就是类型为type的第一个物体

能够加载Assets文件夹下的所有资源,而不仅仅是Resources文件夹下的资源

assetPath为项目文件夹下自Assets开始的路径名,要包含文件的扩展名,如

"Assets/Artwork/mymodel.fbx"只在Unity编辑器中才有作用,在standalone player or web player中总是返回null,所以发布的版本中应该使用Resources.Load或Resources.LoadAll
编辑器类AssetDatabase有一个同名方法,功能和用法基本一致。一个(AssetDatabase)是编辑器类,只能在编辑器中使用,并且一般要放在Editor文件夹下;另一个(Resources)一个只在编辑器中起作用(不在编辑器中能使用但是没有作用)
4.This radically minimizes the size of your players to the assets that you actually use in the built game. When you place assets in "Resources" folders this can not be done, thus all assets in the "Resources" folders will be included in a build.

在Unity中你通常并不使用路径名来访问资源,取而代之的是通过声明一个成员变量来暴露一个资源的引用,然后在inspector中给它赋值。当使用这个技术时,Unity在bulid a player时自动计算哪些资源会被使用.这就使你的player最小化了:只使用你实际上使用的资源。当你把资源放在"Resources"文件夹中时,”Resources“文件中中所有的资源都将会包含在

也就是说,虽然我们在Project View中放置了很多资源,但是只有在游戏中被使用的资源(如场景中的模型要用到的贴图,材质球等;脚本暴露资源引用,通过inspector进行赋值定的资源等)才会被打包进APK(对于发布到安卓平台来说)中,其余的不会被打包进去,也不会影响最终的包大小。但是如果我们创建了Resources文件夹

Another disadvantage of using path names is that it leads to less reusable code since scripts will have specific hard coded requirements on where the used assets are placed. On the other hand using references that are exposed in the inspector are self-documenting and immediately obvious to the user of your script.

However there are situations where it is more convenient to fetch an asset by its name instead of linking to it in the inspector. Essentially whenever it is inconvenient to assign the reference to the object in the inspector. For example you might want to create a game object procedurally from a script and for example assign a texture to a procedurally generated mesh.

Some loaded assets, most notably textures, can use up memory even when no instance exists in the scene. To reclaim this memory when the asset is no longer needed, you can use Resources.UnloadUnusedAssets.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值