- Home /
Create a glossary using Simple JSON, loading data dynamically
I everyone, I am not a programmer so my knowledge of code is a bit sketchy. I am creating an app with inside a glossary to display. I use a JSON file containing all the informations (glossary entries and their definition) and I want to display it according to the hierarchy of content. This means 3 different levels of information :
a level with all the letters (a, b, c, ..., z)
a level with all the words starting with the selected letter (Antivirus, Authentification, ...)
a level with the definition of the selected word
Here is the JSON file (not finished, just the structure) :
{
"data":
{
"a":
[
{
"word" : "Adresse IP",
"def" : "blablabla"
},
{
"word" : "Alphabétisation numérique",
"def" : "bliblibli"
}
],
"b":
[
{
"word" : "Baladodiffusion",
"def" : "blobloblo"
},
{
"word" : "Balise",
"def" : "blublublu"
}
]
}
}
I managed to parse my JSON file and display the list of all the words starting with "a" :
TextMesh myText;
BoxCollider myTextCollider;
public Camera camera;
void Start ()
{
ListOfWords();
}
void ListOfWords()
{
SimpleJSON.JSONNode node = SimpleJSON.JSONNode.Parse(Resources.Load<TextAsset>
("JSON/test").text);
int numberOfDefInA = node["data"]["a"].Count;
//Debug.Log(numberOfDefInA);
// Creating a list of the words starting with "a"
for(int i = 0; i < numberOfDefInA; i++)
{
string currentWord = node["data"]["a"][i]["word"];
//Debug.Log(currentWord);
// Prefab
GameObject Word = (GameObject)Instantiate(Resources.Load("word"));
Vector3 myTextPos = transform.position;
myTextPos.x = -5;
myTextPos.y = (float)(2 - 0.6 * i);
Word.transform.position = myTextPos;
myText = Word.GetComponent<TextMesh>();
myText.text = currentWord;
// adding a collider to each word, scaling it to the mesh renderer
myTextCollider = Word.AddComponent<BoxCollider>();
myTextCollider.center = new Vector3 (Word.renderer.bounds.extents.x, Word.renderer.bounds.extents.y - Word.renderer.bounds.size.y / 2, myTextPos.z);
myTextCollider.size = new Vector3 (Word.renderer.bounds.size.x, Word.renderer.bounds.size.y, 1);
}
}
It creates a GameObject for each word, position it below the previous one and add a collider that fits the mesh renderer (A big thanks to MikeNewall for that bit of code !). Now my problem is : I don't know how to transform these words into buttons, meaning when you click on a word or touch it (for IOS) it displays its definition.
I started to detect a mouse click using Raycast inside the Update :
void Update ()
{
if (Input.GetMouseButtonDown(0)){ // if left button pressed...
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)){
// the object identified by hit.transform was clicked
// do whatever you want
Debug.Log(ray);
}
}
}
But I am now stuck, I can't figure how to retrieve the data from the loop for inside the ListOfWords() function and use it with the Update, but maybe this is not how to proceed ? I would be very grateful if someone could help me on this one,
Thank you in advance !
Answer by Airan · Oct 30, 2014 at 06:02 PM
Place your node variable outside the scope of the ListOfWords method to make it a global variable.
When the raycast hits on a button, take the text value of the button, then look for the first letter of the text. Then, use a loop to iterate through that subset of terms in your glossary node.
eg (pseudo-code):
TextMesh myText;
BoxCollider myTextCollider;
public Camera camera;
SimpleJSON.JSONNode node; //place your node here, then store the JSON data in to it inside the ListOfWords method as before
...
if (Physics.Raycast(ray, out hit)){
//get gameobject that was hit
//get text of the game object
//get first letter of text (substring, etc)
//for int loop
for(int i = 0; i < node["data"]["a"].Count; i++)
{
if(node["data"]["a"][i].value == text)
//display definition
}
}