Topic Author
Posts: 125
Joined: 30 Jul 2018, 05:03

Problem loading texture in Unity.

13 Aug 2018, 13:10

Hello. I'm having a problem loading textures in Unity. The textures show when ran in WPF, and they show in the inspector, but I get a, "Cannot load texture" error in Unity.

The path is obviously correct, so I wasn't sure what the problem is. I moved them in a resource folder (is that required?), but it didn't make a difference.

A bonus question: How do you do absolute paths in Unity for resources? I've read the documentation on Urls, but the examples did not work for me. Do you start the path at Assets?

Thank you for your help, and sorry if this is not enough information. I wasn't sure what else to add.
User avatar
Site Admin
Posts: 2534
Joined: 22 Dec 2011, 19:20

Re: Problem loading texture in Unity.

22 Aug 2018, 01:22

It is strange because if we found the xaml dependencies it should work (there is no need to place the images inside a Resources folder).
I just tried a xaml with an Image element and it worked fine.
What kind of image resource are you using? an Image, an ImageBrush, an ImageSource in a dicitionary?

We consider absolute paths those starting with '/'. In Unity an absolute path means it is relative to the project folder, so your absolute paths should start with "/Assets/".
Testing this in Unity I found there are a couple of problems that can be avoided if you change NoesisGUI/Plugins/Editor/NoesisPostprocessor.cs functions NormalizePath and AbsolutePath to this:
    /// <summary>
    /// Unity always expects paths with forward slashes (/) and rooted at "Assets/"
    /// </summary>
    private static string NormalizePath(string uri)
        string full = Path.GetFullPath(uri).Replace('\\', '/');
        int n = full.IndexOf("Assets/");
        return n >= 0 ? full.Substring(n) : "";

    /// <summary>
    /// In XAML, URIs are relative by default. Absolute URIs use the following syntaxes:
    ///     "pack://application:,,,/path1/path2/resource.ext"
    ///     "/ReferencedAssembly;component/path1/path2/resource.ext"
    ///     "/path1/path2/resource.ext"
    /// </summary>
    private static string AbsolutePath(string parent, string uri)
        const string PackUri = "pack://application:,,,";
        int n = uri.IndexOf(PackUri);
        if (n != -1)
            uri = uri.Substring(n + PackUri.Length);

        const string ComponentUri = ";component";
        n = uri.IndexOf(ComponentUri);
        if (n != -1)
            uri = uri.Substring(n + ComponentUri.Length);

        if (uri.StartsWith("/"))
            return NormalizePath(uri.Substring(1));
            return NormalizePath(parent + "/" + uri);

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest