什么是正确的Resource.Load路径? [英] What is the correct Resource.Load path?
本文介绍了什么是正确的Resource.Load路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I'm trying to load a Texture2D
(.png) resource using Resource.Load
. I've tried following path patterns:
Assets/CaseSensitivePath/TextureName
CaseSensitivePath/TextureName
Assets/CaseSensitivePath/TextureName.png
CaseSensitivePath/TextureName.png
每次Resource.Load(path, typeof(Texture2D))
都返回null.这是我的代码和错误处理:
Every time, Resource.Load(path, typeof(Texture2D))
returns null. This is my code and error handling:
public class LazyResource<T> where T : UnityEngine.Object
{
//Path is read-only
public string path { get { return _path; } }
private string _path = "";
//Whether NOT FOUND warning was thrown
//in that case, further load attemts are ommited and the resource returns always null...
public bool failed = false;
//Constructor uses the path as first parameter
public LazyResource(string path) {
_path = path;
}
//Cached resource
private T cache = null;
public T res
{
get
{
//Does not re-try if it failed before
if (cache == null && !failed)
{
//Load the proper type of resource
cache = (T)Resources.Load(_path, typeof(T));
//Throw warning (once)
if (cache == null)
{
Debug.LogWarning("Icon not found at '" + _path + "'!");
failed = true;
}
}
//Can return null
return cache;
}
}
}
错误:
Icon not found at 'Textures/GUI/Build/egg'!
UnityEngine.Debug:LogWarning(Object)
LazyResource`1:get_res() (at Assets/WorldObject/LazyResource.cs:28)
Actions.Action:GetMenuIcon() (at Assets/WorldObject/Action.cs:203)
HUD:DrawActions(Action[]) (at Assets/Player/HUD/HUD.cs:115)
HUD:DrawOrdersBar() (at Assets/Player/HUD/HUD.cs:85)
HUD:OnGUI() (at Assets/Player/HUD/HUD.cs:63)
在Unity3D项目中加载纹理的正确路径是什么?
What is the correct path to load a texture in the Unity3D project?
推荐答案
您不一定需要路径.您只需输入
You don't necessarily need a path. You can simply type
Resources.Load<Texture2D>("MyTexture");
如果TextureName
在文件夹中,则键入:
If TextureName
is in a folder then you type:
Resources.Load<Texture2D>("MyFolder/MyTexture");
如果要使用Resources.Load
,则需要将资源放入资源文件夹.这就是它在编辑器中的外观.
If you want to use Resources.Load
then you need to put the resource in the Resource folder. This is how it looks in the Editor.
这篇关于什么是正确的Resource.Load路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文