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 /
  • Help Room /
This question was closed Aug 23, 2020 at 12:32 PM by wenjie7788 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by wenjie7788 · Aug 12, 2020 at 02:50 PM · databasejsoninstantiate prefabcallbackstartcoroutine

How to load different content from database when select different option?

I'm developing a mobile app which user have to log in, select a course then select a chapter of the course. My question is how to load different content when going to different courses? The database I used here is phpmyadmin. The details are explained below.


Courses.cs

Attached in CourseList. To link the courses registered under the user based on the username then instantiate the button's prefab based on the courses. A method to activate the content scene(game object) is set in the btn.onClick.AddListener.

 public class Courses : MonoBehaviour
 {
     Action<string> _createCoursesCallback;
 
     // Start is called before the first frame update
     void Start()
     {
         // Define callback
         _createCoursesCallback = (jsonArrayString) =>
         {
             StartCoroutine(CreateCoursesRoutine(jsonArrayString));
         };
 
         CreateCourses();
     }
 
     public void CreateCourses()
     {
         string username = Main.Instance.UserInfo.username;
         StartCoroutine(Main.Instance.Web.GetCourseCode(username, _createCoursesCallback));
     }
 
     IEnumerator CreateCoursesRoutine(string jsonArrayString)
     {
         // Parsing json array string as an array
         JSONArray jsonArray = JSON.Parse(jsonArrayString) as JSONArray;
 
         for (int i = 0; i < jsonArray.Count; i++)
         {
             // Create local variables
             bool isDone = false; // are we done downloading?
             string courseCode = jsonArray[i].AsObject["coursename"]; // key to get value
             JSONObject courseInfoJson = new JSONObject();
 
             // Create a callback to get the information from WebRequest.cs
             Action<string> getCourseInfoCallback = (courseInfo) =>
             {
                 isDone = true;
                 JSONArray tempArray = JSON.Parse(courseInfo) as JSONArray;
                 courseInfoJson = tempArray[0].AsObject;
             };
 
             StartCoroutine(Main.Instance.Web.GetCourse(courseCode, getCourseInfoCallback));
 
             // Wait until the callback is called from WEB (info finished downloading)
             yield return new WaitUntil(() => isDone == true);
 
             // Instantiate Gameobject (item prefab)
             GameObject course = Instantiate(Resources.Load("Prefabs/Button_courses") as GameObject);
             course.transform.SetParent(this.transform);
             course.transform.localScale = Vector3.one;
             course.transform.localPosition = Vector3.zero;
 
             // Fill Information***
             course.transform.Find("Text").GetComponent<Text>().text = courseInfoJson["coursename"];
 
             // Set Button
             Button btn = course.GetComponent<Button>();
             String courseN = courseInfoJson["coursename"];
             
             btn.onClick.AddListener(() =>
             {    
                 Main.Instance.CourseContent.SetActive(true);
                 StartCoroutine(Main.Instance.Web.EnterCourse(courseN));
             });
 
             // Continue to the next course
         }
     }
 }


Contents.cs Attached in ChapterList. To link the content registered under the course based on the course then instantiate the image's prefab based on the content in the database.

 public class Contents : MonoBehaviour
 {
     Action<string> _createContentCallback;
     string course;

     // Start is called before the first frame update
     private void Start()
     {
         
         // Define callback
         _createContentCallback = (jsonArrayString) =>
         {
             StartCoroutine(CreateContentRoutine(jsonArrayString));
         };
 
         CreateContent();
     }
 
     public void CreateContent()
     {
         course = Main.Instance.UserInfo.course;
         StartCoroutine(Main.Instance.Web.GetChapter(course, _createContentCallback));
     }
 
     IEnumerator CreateContentRoutine(string jsonArrayString)
     {
         // Parsing json array string as an array
         JSONArray jsonArray = JSON.Parse(jsonArrayString) as JSONArray;
 
         for (int i = 0; i < jsonArray.Count; i++)
         {
             // Create local variables
             bool isDone = false; // are we done downloading?
             string chapter = jsonArray[i].AsObject["coursechapter"]; // key to get value
             JSONObject contentJson = new JSONObject();
 
             // Create a callback to get the information from WebRequest.cs
             Action<string> getContentCallback = (contentInfo) =>
             {
                 isDone = true;
                 JSONArray tempArray = JSON.Parse(contentInfo) as JSONArray;
                 contentJson = tempArray[0].AsObject;
             };
 
             StartCoroutine(Main.Instance.Web.GetContent(chapter, getContentCallback));
 
             // Wait until the callback is called from WEB (info finished downloading)
             yield return new WaitUntil(() => isDone == true);
 
             // Instantiate Gameobject (item prefab)
             GameObject chapterBox = Instantiate(Resources.Load("Prefabs/Image_chapter") as GameObject);
             chapterBox.transform.SetParent(this.transform);
             chapterBox.transform.localScale = Vector3.one;
             chapterBox.transform.localPosition = Vector3.zero;
 
             // Fill Information***
             chapterBox.transform.Find("Text_name").GetComponent<Text>().text = contentJson["coursechapter"];
             chapterBox.transform.Find("Button_image").transform.Find("Text_image").GetComponent<Text>().text = contentJson["fileimage"];
             chapterBox.transform.Find("Button_video").transform.Find("Text_video").GetComponent<Text>().text = contentJson["filevideo"];
             chapterBox.transform.Find("Button_3dmodel").transform.Find("Text_3dmodel").GetComponent<Text>().text = contentJson["file3d"];
 
             // Continue to the next course
         }
     }
 }


The Hierarchy alt text


So, when I click into a course, the scripts above can show the content well according to the database. But if I back to the course menu and go to another course, the content shown belongs to the previous course selected. It may because of the data only be loaded once in the start method.

Therefore, how to load different content when I go to different courses.

capture.jpg (35.7 kB)
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

213 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Whats the best option for an item database for a single player game 2 Answers

What should I use, a Dictionary with 100 entries or a class with 100 variables? 0 Answers

How to add item to inventory with these scripts 0 Answers

Extract database from firebase in json to struct 0 Answers

Attacking an enemy (2D platformer) 0 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