The question is answered, right answer was accepted
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
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.
Follow this Question
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