Search Unity

Load texture on the fly

Discussion in 'Editor & General Support' started by newtext, Aug 31, 2010.

  1. newtext

    newtext

    Joined:
    Dec 14, 2009
    Posts:
    18
    hi there,
    Is it possible to load a texture at run time? That means to new the texture and load its content all manually and skip those import settings?

    something like :


    Code (csharp):
    1.  
    2. Texture2D t = new Texture2D(width, height)
    3. t.LoadImage(File.ReadAllBytes(texPath);
    4.  
    Note : for now Texture2D.LoadImage support only imageTextAsset, which means we cant create the texture on the fly, since they are packaged in a asset.

    Thank you.

    NT.
     
  2. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
  3. newtext

    newtext

    Joined:
    Dec 14, 2009
    Posts:
    18
    Thanks Vimalakirti,

    Resource.Load is already used in my project in a lot of places, but it's not the answer to my question.

    in my sample code:
    Code (csharp):
    1.  
    2. Texture2D t = new Texture2D(width, height)
    3. t.LoadImage(File.ReadAllBytes(texPath);
    4.  
    Where, File.ReadAllBytes(texPath), the image referenced by texPath can be anywhere on your disk,
    and while using Resource.Load the texture must be already packed in .asset files when released. In other words, by using Resource.load, the texture must already exist and be placed somewhere in your Resource folder which is not what I want. What if I want to load a texture that the UnityEditor isn't aware of?

    Thanks again for the quick reply, and I hope this clears up my question ;)


    NT.
     
  4. hai_ok

    hai_ok

    Joined:
    Jun 20, 2007
    Posts:
    193
    The following works for me:
    Code (csharp):
    1.  
    2. var imageVariable : Texture2D; //
    3. function loadImage(){
    4.     www3 = new WWW("file://" + Application.dataPath + "/image.jpg");
    5.         yield www3;
    6.         imageVariable = www3.texture;
    7. }
    8.  
    Where image variable can be assigned to nearly anything that can use a Texture2D.

    It took a while before I decided that this was what worked best for me. I'm still looking and I may find something better. But it's what I use for now.