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

Load image from file and apply to material.

Discussion in 'Scripting' started by ElPiwai, Mar 9, 2020.

  1. ElPiwai

    ElPiwai

    Joined:
    Jan 30, 2020
    Posts:
    4
    With a beginner C# level, I'm developping an application for Oculus Go. the goal here is to externalize the big 360 images and video from the apk, and to manually place them on the Headset Storage. it works for the videos, and I'm now trying to do it with the images. the image will be applied as a texture to a skybox material set as environnement. Here's the Script I came up with. Note that Unity doesn't detect any errors, but the image doesn't show up when I run the app on the Oculus Go.
    Code (CSharp):
    1. public class ImageLoadFromStorage : MonoBehaviour
    2. {
    3.     public Material BackgroundMaterial; // material set as environnement
    4.     private string rootPath;
    5.     private string path;
    6.     public string fileName; //name of the image on the headset
    7.  
    8. #if UNITY_ANDROID
    9.     void Awake()
    10.     {
    11.         if (Application.platform == RuntimePlatform.Android)
    12.         {
    13.             rootPath = Application.persistentDataPath;
    14.             path = Path.Combine(rootPath,"Medias/Images/" + fileName);
    15.         }
    16.  
    17.         var bytes = System.IO.File.ReadAllBytes(path);
    18.         var myTexture = new Texture2D(1, 1);
    19.  
    20.         myTexture.LoadImage(bytes);
    21.  
    22.         BackgroundMaterial.mainTexture = myTexture;
    23.  
    24.     }
    25.  
    26.  #endif
    27. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Are there any exceptions being thrown in the
    adb logcat
    ? That is always going to be the first place to look whenever you are debugging on device.
     
  3. ElPiwai

    ElPiwai

    Joined:
    Jan 30, 2020
    Posts:
    4
    Nope. No exceptions.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Next step is to display onscreen (in the VR unit obviously) the number of bytes read on line 17.

    This should also give you insight as to whether this code is even being executed at all in the first place! :)
     
  5. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    I haven't tried it myself, but if this is being used as the texture for a skybox, I think you need to create a Cubemap, not a Texture2D. Does this work in editor with just a texture2d?
     
  6. ElPiwai

    ElPiwai

    Joined:
    Jan 30, 2020
    Posts:
    4
    The texture is a spherical 360 photo, and the material I'm trying to assign to is set as Skybox/Panoramic.