Search Unity

Loading textures at runtime

Discussion in 'Scripting' started by Medusa-Zenovka, Nov 24, 2014.

  1. Medusa-Zenovka

    Medusa-Zenovka

    Joined:
    Oct 1, 2014
    Posts:
    29
    Hello,

    I am loading a texture by C# script:

    Code (CSharp):
    1.         string CommandBarTexturePath = TextureAssetsScript.assetPath[TextureAssetsScript.Id.IndexOf (faction.CommandBarTexture)] as string;
    2.  
    3.         CommandBarTexture = Resources.LoadAssetAtPath (CommandBarTexturePath, typeof(Texture2D)) as
    4.  
    5.         commandbarSkin.box.normal.background = CommandBarTexture;
    The info for importing the texture is stored in XML files which are correctly loaded into arrays and within the editor everything runs fine.

    The problem starts when I build and run my game. The texture files, which are .tga, are no displayed - but strangely in the editor everything works as intended.

    Do you have a clue?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There's no Resources folder in a build; everything is compiled into an asset file.

    --Eric
     
  3. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    If you would like to load a texture at runtime, I suggest the WWW class and an external folder.
    Code (CSharp):
    1. /*Call with a coroutine, or manually check isDone until it's ready*/
    2. IEnumerator loadImage()
    3.     {
    4.         file = new WWW("file://" + Application.dataPath + "/../OpenFiles/image.png");
    5.         yield return file;
    6.  
    7.         bait = file.texture;
    8.     }
    "./OpenFiles" is a folder in the same directory as the Assets folder. I've got a mac, so when I compile it I need to put that folder inside the package contents. I attached a screenshot of that, not sure where it would be in windows.
     

    Attached Files:

  4. Medusa-Zenovka

    Medusa-Zenovka

    Joined:
    Oct 1, 2014
    Posts:
    29

    Thank you very much. There is only one last issue: the texture is now loaded for sure, but not displayed. I dont know why, so here is the method thats called within the OnGUI void:

    Code (CSharp):
    1.     private void DrawCommandBar() {
    2.         GUI.skin = commandbarSkin;
    3.         GUI.BeginGroup(new Rect(CommandBarPos.x * Screen.width, CommandBarPos.y * Screen.height, (CommandBarWidth + CommandBarPos.x) * Screen.width, (CommandBarHeight + CommandBarPos.y) * Screen.height));
    4.         GUI.Box(new Rect(CommandBarPos.x * Screen.width, CommandBarPos.y * Screen.height, (CommandBarWidth + CommandBarPos.x) * Screen.width, (CommandBarHeight + CommandBarPos.y) * Screen.height), "");
    5.         GUI.EndGroup();
    6.     }
    It should draw a black bar on screen like in the screenshot I attached.


    ***** EDIT *****

    This code...
    Code (CSharp):
    1.     IEnumerator LoadFile(string path)
    2.     {
    3.         Debug.Log (Application.dataPath);
    4.  
    5.         WWW file = new WWW ("file://" + Application.dataPath + path);
    6.  
    7.         CommandBarTexture = file.texture;
    8.         yield return file;
    9.     }
    ...causes the strange question mark texture to appear ingame. :-/
     

    Attached Files:

    Last edited: Nov 24, 2014
  5. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    @Medusa Zenovka that's the "missing texture" in Unity. Was it working in the editor, then disappear in the standalone? ;)

    It took me a moment to figure it out, but if you print the Application.dataPath and draw it somewhere in the standalone, you'll see what went wrong :)

    For me on mac, I needed to put the external asset inside the package. For your windows standalone (assumed), you'll need to put the external file somewhere, probably either next to the executable or inside the Data folder. Move it around until the "?" becomes the image. I just struggled with this the other day haha, so the fix is fresh in my mind. Good luck.

    --edit

    you have 2 lines backwards. Put the yield return file before the CommandBarTexture = file.texture; and you should be set.