【问题标题】:What is the correct Resource.Load path?什么是正确的 Resource.Load 路径?
【发布时间】:2015-01-10 07:12:46
【问题描述】:

我正在尝试使用Resource.Load 加载Texture2D (.png) 资源。我尝试了以下路径模式:

Assets/CaseSensitivePath/TextureName
CaseSensitivePath/TextureName
Assets/CaseSensitivePath/TextureName.png
CaseSensitivePath/TextureName.png

每次,Resource.Load(path, typeof(Texture2D)) 返回 null。这是我的代码和错误处理:

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 项目中加载纹理的正确路径是什么?

【问题讨论】:

    标签: c# unity3d texture2d


    【解决方案1】:

    您不一定需要路径。你可以简单地输入

    Resources.Load<Texture2D>("MyTexture");
    

    如果TextureName 在文件夹中,则键入:

    Resources.Load<Texture2D>("MyFolder/MyTexture");
    

    如果您想使用Resources.Load,则需要将资源放入资源文件夹。这就是它在编辑器中的外观。

    【讨论】:

    • 它在Assets/GUI/Build/egg.png。问题是我不知道你必须使用the Resources folder... 这太糟糕了。我想按我的意愿组织我的文件。
    • 如果你想使用Resources.Load那么你需要把资源放在Resource文件夹中。
    • 我会使用不同的东西。
    • 对我来说@apxcode 解决了它......我真的只是觉得 Assets 文件夹是为此而生的,我的直觉是 Resources.Load 就是从资产加载。但是不行:你必须手动创建一个资源文件夹。奇怪的教训。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 2016-04-25
    • 1970-01-01
    • 2013-10-12
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多