Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Working with Textures on Android

Discussion in 'Scripting' started by VladK02, Jun 2, 2021.

  1. VladK02

    VladK02

    Joined:
    May 11, 2021
    Posts:
    11
    Hello, I am trying to create 3d graphical redactor for Android, but there are some problems with the reading textures procedure. There are several predefined textures saved as png in the Resources folder. Users can set them to meshes of some objects. While testing the app in the editor the code loads them fine. Here is the code
    Code (CSharp):
    1. ...
    2.         ReadTexture("/Resources/Player/Texture1.png")
    3. ...
    4.  
    5. private Texture2D ReadTexture(string path)
    6.     {
    7.         var bytes = System.IO.File.ReadAllBytes(Application.dataPath + path);
    8.         Texture2D texture = new Texture2D(10, 10);
    9.         texture.LoadImage(bytes);
    10.         return texture;
    11.     }
    But on my Android device, it does not work. Using the adb I even did not see the png files in the application path. Why is it happen? I would be very glad for any help
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    VladK02 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    This will never work anywhere but in the Unity editor. Don't do this unless it's just an editor script.

    For pre-made stuff use direct drag-and-drop references, or Resources.Load<T>() (read all the requirements!)

    If you want to actually ship binary PNG files out, you can put them in StreamingAssets and read them. Each platform is a little different loading code.

    https://docs.unity3d.com/Manual/StreamingAssets.html
     
    VladK02 likes this.