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

Simple 2D streaming assets image

Discussion in '2D' started by CESIL, Jun 27, 2019.

  1. CESIL

    CESIL

    Joined:
    Jun 25, 2019
    Posts:
    5
    Is there not a script for loading an image from the Streaming Assets folder that is as simple as loading the video player? I've looked all over, and none of the tutorials or assets from the asset store are working for me. They either use www (which Unity is telling me is obsolete) or they don't apply at all to what I want to do (I need 2D only right now) or they were created in older versions of Unity with errors popping up. I just want one image from the Streaming Assets folder for a super simple program. It should be easy (even for a noob like me), but I'm soooo stuck and frustrated.
     
  2. CESIL

    CESIL

    Joined:
    Jun 25, 2019
    Posts:
    5
    using System.Collections;
    using System.IO;
    using UnityEngine;
    using UnityEngine.Networking;

    [RequireComponent(typeof(Mesh))]

    public class LocalImageGet : MonoBehaviour
    {
    private UnityEngine.Texture2D texture;

    void Start()
    {
    StartCoroutine(LoadTexture());
    }

    private IEnumerator LoadTexture()
    {
    GameObject img = GameObject.Find("RawImage");

    // Creates a 2D texture with width and height scale.
    Texture2D texture = new Texture2D(1, 1);

    string uri = System.IO.Path.Combine(Application.streamingAssetsPath, "image1");

    using (var uwr = UnityWebRequestTexture.GetTexture(uri))
    {
    uwr.downloadHandler = new DownloadHandlerTexture();
    yield return uwr.SendWebRequest();

    GetComponent<Renderer>().material.mainTexture = DownloadHandlerTexture.GetContent(uwr);
    }
    }


    }
     
  3. CESIL

    CESIL

    Joined:
    Jun 25, 2019
    Posts:
    5
    In the above example, I basically threw everything at it. The image still does not show up, and I get an error that the image hasn't finished loading (which doesn't make sense), so the last line of code can't execute.
     
  4. TigerDawn

    TigerDawn

    Joined:
    Aug 4, 2018
    Posts:
    21
    What is the size of the image? If the image is too large, it is possible it has not finished loading before you are trying to use it.
     
  5. CESIL

    CESIL

    Joined:
    Jun 25, 2019
    Posts:
    5
    Thanks for the reply. The size of the image wan't too big. I'm just so new to this, that I often have to fumble my way to success. I figured it out, finally. I had no choice but to use the obsolete www method. So it works, now. I just have to ignore the error messages. So this is basically it...

    {
    string url = System.IO.Path.Combine(Application.streamingAssetsPath, "image1.png");
    WWW www = new WWW(url);
    Texture2D texture = www.texture;

    GetComponent<Image>().sprite = TextureToSprite(texture);

    }
    Sprite TextureToSprite(Texture2D texture)
    {
    Rect rect = new Rect(0, 0, texture.width, texture.height);
    Vector2 pivot = new Vector2(0.5f, 0.5f);
    Sprite sprite = Sprite.Create(texture, rect, pivot);
    return sprite;
    }