简体   繁体   中英

Display json result in textbox in unity

I have a script in unity that searches a json file and changes the texture of a RawImage to the url image address found in the corresponding result of the search. I now want to also change the text in a text box to display another field result.

The code i use to search and change the image is

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class UrlOpener : MonoBehaviour
{
public RawImage rawImage;
public InputField SearchBox;
public int displayIndex;
List<ArtImage> result;
public void Open()
{
    Debug.Log("start");
    rawImage.color = Color.white;

    //string imageaddress;
    using (StreamReader r = new StreamReader("Assets/run_results-2.json"))
    {
        string json = r.ReadToEnd();

        ArtImages imgs = JsonUtility.FromJson<ArtImages>(json);
        result = new List<ArtImage>();
        if (imgs.artwork.Length > 0)
        {
            foreach(ArtImage img in imgs.artwork)
            {
                if (img.name.ToLower().Contains(SearchBox.text.ToLower()))
                {
                    result.Add(img);


                    //StartCoroutine(applytexture(imageaddress,img.heightimg,img.widthimg)); // execute the section independently
                    //break;
                }
                                }
            if (result.Count > 0)
            {
                StartCoroutine(applytexture(result[0].image,result[0].heightimg,result[0].widthimg)); // execute the section independently
                displayIndex = 0;
            }


            // the following will be called even before the load finished
        }
    }
}

private IEnumerator applytexture(string imageaddress, float heightimg,float widthimg)
{


    Debug.Log("Loading ....");
    WWW wwwLoader = new WWW(imageaddress);   // create WWW object pointing to the url
    yield return wwwLoader;         // start loading whatever in that url ( delay happens here )

    Debug.Log("Loaded");
    rawImage.color = Color.white;              // set white
    rawImage.texture = wwwLoader.texture;
    GetComponent<RectTransform>().sizeDelta = new Vector2(widthimg,heightimg);

}

}




[Serializable]
public class ArtImage
{
public string name;
public string image;
public float widthimg;
public float heightimg;
}
[Serializable]
public class ArtImages
{
public ArtImage[] artwork;
}

I need to have a textbox display what is stored in the "name" field of that same result.

Any help would be much appreciated.

A quick note, im very much a beginner in c# so try and respond in a manner that id understand haha

Cheers

To display Results there are two ways you can either read out the Values and display the Json data directly or you could read it out and then display the values from your Data holding class directly

public void LoadGameData()
{
    if (File.Exists(filePath))
    {
        string dataAsJson = File.ReadAllText(filePath);
        jsonDataClass = JsonUtility.FromJson<JsonDataClass>(dataAsJson);
        text.text = dataAsJson;
    }
    else
    {
        text.text = "No JsonFile found";
    }
}

Before using this you need to have a Data holding class (Replace JSONDataClass with it) then you would need a public Text text which is your Textbox to display the Json data in. As a last Note you need to replace filepath with your Json filepath.

If you have some more questions onto the toppic I recommend looking into: Unity Json reader and Unity textfield Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM