Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to load a texture2d from path, without resources.load?

Discussion in 'Scripting' started by raarc, Jun 18, 2020.

  1. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    I want to load a texture2d, from any path on my computer, not only from inside the assets folder.

    I found some results on google but its all outdated and obsolete methods, hope someone can point me in the right direction.

    Im hoping for something that does the same as resources.load<texture2d> but from any path, even outside of the project folder. It is not a problem if it only works from the editor, as long as it does not need to be in the project folder. Something like

    Texture2d tex = File.Open(filepath.png)

    Thanks!
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
  3. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    thanks so do I just write the path as "file://C:/Users/etc.png" ?

    Also is there alternate ways to do it or is this the only one?
     
  4. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
  5. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    Thanks!

    I was asking about alternatives because I needed a method that would return the texture without yielding,
    so when I have tex=resources.Load<texture2D> the rest of the code won't execute until the resource is loaded, since the unitywebrequest implements a coroutine it will yield and keep executing the rest of the code before allocating the texture.

    Or is there a way to code a method that returns a texture2d without yielding using the unitywebrequest?

    Thanks!
     
    SedemQuame likes this.
  6. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    185
    Here's a blocking way to do it:

    Code (CSharp):
    1.  
    2.         string filename = "Assets/Resources/Heightmaps/filename.png";
    3.         var rawData = System.IO.File.ReadAllBytes(filename);
    4.         Texture2D tex = new Texture2D(2, 2); // Create an empty Texture; size doesn't matter (she said)
    5.         tex.LoadImage(rawData);
    6.  
     
    JohngUK, coidevoid, Snubber and 7 others like this.
  7. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    thanks!
     
  8. SedemQuame

    SedemQuame

    Joined:
    May 7, 2020
    Posts:
    2
    Vineet112233 likes this.
  9. anderbggo

    anderbggo

    Joined:
    Jan 17, 2022
    Posts:
    1
    Thanks!

    PD: She said... xDD
     
  10. welliAR

    welliAR

    Joined:
    Jun 25, 2018
    Posts:
    31
    Hey Folks. I know im late to the party. But anyone knows why LoadImage is not available on tex? There is no such method?!

    1. string filename = "Assets/Resources/Heightmaps/filename.png";
    2. var rawData = System.IO.File.ReadAllBytes(filename);
    3. Texture2D tex = new Texture2D(2, 2); // Create an empty Texture; size doesn't matter (she said)
    4. tex.LoadImage(rawData);
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
  12. welliAR

    welliAR

    Joined:
    Jun 25, 2018
    Posts:
    31
    Special thanks for your fast reply. but i cannot find imageconversion also. Im on Unity 2018 btw. There is always extension method mention, and i cannot call them or find them. Anyone has a way to load a simple jpg via ByteArray into a texture2d without dealing with all the conversation?
     
  13. welliAR

    welliAR

    Joined:
    Jun 25, 2018
    Posts:
    31
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Sounds like Visual Studio being Visual Studio. It's still on Texture2D in Unity2018.

    From Unity 2018.4.19.f1:

    Screen Shot 2022-09-09 at 11.27.16 AM.png


    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

    Barring all that, move on to other ideas:

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    Also, try update the VSCode package inside of Unity: Window -> Package Manager -> Search for Visual Studio Code Editor -> Press the Update button

    Also, this: https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874
     
    PanicEnsues and welliAR like this.
  15. welliAR

    welliAR

    Joined:
    Jun 25, 2018
    Posts:
    31
    Yea! You're right. The code works in runtime!!! Only VS is having a problem seeing this method. But its there! I will check that. Thank you very, very much for this tip!
     
    Kurt-Dekker likes this.