I have a method to create objects from prefabs. The prefab consists of the texts "Title" and "Description", as well as the image "Image".
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".
But I am a beginner, so it is desirable that you explain something in simple terms.