Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Almostpic · Aug 01, 2014 at 03:51 PM · textjsondataboxcollider

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 !

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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   
                    }
              }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to Pass a Json as Post Data to WWWForm 2 Answers

How can I read data from a text file, putting a large amount of data into structures 2 Answers

JsonUtility.ToJson returns empty file 2 Answers

Inserting specific words into descriptions stored in XML 0 Answers

how i can write a text from another code ? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges