1
\$\begingroup\$

i know how to load text, but idk how to load texture from json..

this my c#:

 public void soalbegin(){
     filepath = System.IO.Path.Combine(Application.streamingAssetsPath, "soal.json");
     StartCoroutine ("Json");
     soaldata = JsonMapper.ToObject(jsonString);

 }

 IEnumerator Json(){
 if(filepath.Contains("://")){
     WWW www = new WWW(filepath);
     yield return www;
     jsonString = www.text;
     }
     else {
     jsonString = System.IO.File.ReadAllText(filepath);
 }

public void OnClick(){
         soalbegin();
 GameObject.Find("Panel/soal/gambar/Text").GetComponentInChildren<Text>().text = soaldata["data"][numbersoal]["soal"].ToString();//this to load text from json

//i need variable to load sprite / texture from link json
}

and this is my json:

 {
 "data":[
 {
     "id":"1",
     "soal":"siapa tokoh ini",
     "gambar":"//1.png",
     "jawaban":[
         "natsu",
         "shank",
         "goku",
         "gildart"
         ]
 }
]}
\$\endgroup\$
2
  • \$\begingroup\$ JSON is just a means of storing text information right? Why not just hold a path to the sprite in the JSON file? Then whenever you obtain the information in the JSON file, call Resources.Load <Sprite> (jsonPath); ? \$\endgroup\$
    – Savlon
    Jan 31, 2016 at 9:43
  • \$\begingroup\$ thx Salvon, by the way im newbie.. plz can u give me example \$\endgroup\$ Feb 3, 2016 at 5:37

1 Answer 1

2
\$\begingroup\$

Expanded post on comment:"JSON is just a means of storing text information right? Why not just hold a path to the sprite in the JSON file? Then whenever you obtain the information in the JSON file, call Resources.Load (jsonPath); ?"

The JSON language cannot directly load Sprites into Unity3D. However, as JSON can deal with strings, we can quite easily hold a reference to the path which contains the sprite.

Say for instance you have this basic JSON file

{
"data":[
{
 "id":"1",
 "soal":"siapa tokoh ini",
 "gambar":"//1.png",
 "jawaban":[
     "natsu",
     "shank",
     "goku",
     "gildart"
     ]
}
]}

I'm assuming the below line needs to hold the reference to where the sprite is located.

"gambar":"//1.png",

You can quite easily use this to hold the path to the desired sprite. For this method to work, you'll need to have a folder in your Assets folder called "Resources".

"gambar":"1",

Unity is extremely user-friendly and allows us to leave out image format types (.png/.bmp etc...) when loading them via script.

The way you would load a Sprite (.png/.bmp/.jpeg etc) contained within the "Resources" folder of your Unity project would be like so. Resources.Load

Resources.Load <Sprite> (spritePath);

So to put all of this together, you could have a basic script like so.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour
{
    public string spritePath = "1"; //This should be used for the JSON string you're using to hold the reference to the path of your Sprite
    public Sprite exampleSprite;

    void Start ()
    {
        exampleSprite = Resources.Load <Sprite> (spritePath);
    }
}

You would then just replace the "spritePath" string variable with the string parsed in by your JSON class.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .