0

I have a method to create objects from prefabs. The prefab consists of the texts "Title" and "Description", as well as the image "Image".
enter image description here

In the method below, I get the text for "Title", "Description" and the URL of the image for "Image".

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json.Linq;

using System.Threading.Tasks;
using UnityEngine.Networking;

public class CreateMonumentButtons : MonoBehaviour
{
    public RectTransform prefarb;
    public RectTransform content;

    private string City;
    private string URL = DataHolders.URL;

    async private void Awake()
    {
        City = DataHolders.City;
        StartCoroutine(Get());
    }

    void InitializeItemView (GameObject viewGameObject, string TextTitle, string TextDescription, string URLImg)
    {
        TestItemView view = new TestItemView(viewGameObject.transform);
        view.Title.text = TextTitle;
        view.Description.text = TextDescription;

        File fileData;
        Texture2d txture;
        (File.Exists(URLImg))     {
            fileData = File.ReadAllBytes(URLImg); //read the bytes from the file at URLImg's destination
            txture = new Texture2D(0,0);//makes the new texture
            txture.LoadImage(fileData);//loads the image from the file
            view.Img.sprite = Sprite.Create(txture, new Rect(0,0,200,200), new Vector2());
            //set Img.sprite to a new sprite at 0,0 with the dimensions 200x200
        }
    }

    public class TestItemView
    {
        public Text Title;
        public Text Description;
        // public Image Image;

        public TestItemView (Transform rootView)
        {
            Title = rootView.Find("Title").GetComponent<Text>();
            Description = rootView.Find("Description").GetComponent<Text>();
            // Image = rootView.Find("Image").GetComponent<Image>();
        }
    }

    private IEnumerator Get()
    {
        WWWForm formT = new WWWForm();

        formT.AddField("City", City);

        WWW wwwT = new WWW(URL + "get_monuments.php", formT);
        yield return wwwT;
        if (wwwT.error != null)
        {
            Debug.Log("Ошибка: " + wwwT.error);
            yield break;
        }

        WWWForm formD = new WWWForm();

        formD.AddField("City", City);

        WWW wwwD = new WWW(URL + "get_description.php", formD);
        yield return wwwD;
        if (wwwD.error != null)
        {
            Debug.Log("Ошибка: " + wwwD.error);
            yield break;
        }

        WWWForm formI = new WWWForm();

        formI.AddField("City", City);

        WWW wwwI = new WWW(URL + "get_img.php", formI);
        yield return wwwI;
        if (wwwI.error != null)
        {
            Debug.Log("Ошибка: " + wwwI.error);
            yield break;
        }

        WWWForm formL = new WWWForm();

        formL.AddField("City", City);

        WWW wwwL = new WWW(URL + "get_lenLists.php", formL);
        yield return wwwL;
        if (wwwL.error != null)
        {
            Debug.Log("Ошибка: " + wwwL.error);
            yield break;
        }

        DataHolders.listLen = int.Parse(wwwL.text);
        DataHolders.listImg = wwwI.text;
        DataHolders.listDescription = wwwD.text;  
        DataHolders.listMonuments = wwwT.text; 

        var ListTitls = JObject.Parse(DataHolders.listMonuments);
        var ListDescriptions = JObject.Parse(DataHolders.listDescription);
        var ListImgs = JObject.Parse(DataHolders.listImg);

        for( int i = 0; i < DataHolders.listLen; i++ )
        {
            // Debug.Log(i.ToString());
            var Title = (string) ListTitls[i.ToString()];
            var Description = (string) ListDescriptions[i.ToString()];
            var Img = (string) ListImgs[i.ToString()];

            var instance = GameObject.Instantiate(prefarb.gameObject) as GameObject;
            instance.transform.SetParent(content, false);
            InitializeItemView(instance, Title, Description, Img);
        }      
    }
}

I don't understand how can I get an image from an image url and set its value to "Image". enter image description here

But I am a beginner, so it is desirable that you explain something in simple terms.

  • It can be Googled as `unity image from url`, some links: [forum 2020](https://forum.unity.com/threads/image-from-url.1052135/), [stackoverflow question 2015](https://stackoverflow.com/questions/31765518/how-to-load-an-image-from-url-with-unity) – Alexey S. Larionov Mar 11 '22 at 17:03
  • [Retrieving a Texture from an HTTP Server (GET)](https://docs.unity3d.com/Manual/UnityWebRequest-RetrievingTexture.html) – derHugo Mar 11 '22 at 18:58

3 Answers3

0

Image doesn't have an attribute of Text, and instead you should get the file, load the image to a texture and set the Image's sprite as the texture.

File fileData;
Texture2d txture;
if (File.Exists(URLImg))     {
         fileData = File.ReadAllBytes(URLImg); //read the bytes from the file at URLImg's destination
         txture = new Texture2D(0,0);//makes the new texture
         txture.LoadImage(fileData);//loads the image from the file
         view.Img.sprite = Sprite.Create(txture, new Rect(0,0,200,200), new Vector2());
         //set Img.sprite to a new sprite at 0,0 with the dimensions 200x200
     }

with view.Img.sprite as an image rather than "text" because it's not painting text and rather an image.

Rage Man
  • 41
  • 1
  • 6
0

Unity Beginner has explained it easily in just 1.5 minutes to Load Image from WebLink or URL.

But here, they used WWW, which is obsolete, so use UnityWebRequest instead. Here are the changes you need to perform while changing from WWW to UnityWebRequest.

Mohit Arora
  • 86
  • 1
  • 7
0

I made a simple plugin for this. If someone is interested, check this repo

47 kk
  • 71
  • 6