Skip to content

Commit 73303e8

Browse files
committed
3 Skripte werden revised
1 parent 97e40d9 commit 73303e8

File tree

3 files changed

+337
-0
lines changed

3 files changed

+337
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace SaveData
6+
{
7+
8+
[System.Serializable]
9+
public class QuestionCatalogue
10+
{
11+
public string fileName;
12+
public List<Question> questions = new List<Question>();
13+
}
14+
15+
[System.Serializable]
16+
public class Question
17+
{
18+
public string questionName;
19+
public List<string> answers;
20+
public int rightAnswerPosition;
21+
22+
public Question(string _questionName, List<string> _answers, int _rightAnswerPosition)
23+
{
24+
questionName = _questionName;
25+
answers = _answers;
26+
rightAnswerPosition = _rightAnswerPosition;
27+
}
28+
}
29+
30+
[System.Serializable]
31+
public class PlayerProfile
32+
{
33+
public string fileName = "Player";
34+
public string userName;
35+
public List<Statistic> statistics = new List<Statistic>();
36+
}
37+
38+
[System.Serializable]
39+
public class Statistic
40+
{
41+
public string label;
42+
public int score;
43+
44+
public Statistic(string _label, int _score)
45+
{
46+
label = _label;
47+
score = _score;
48+
}
49+
}
50+
51+
52+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using SaveData;
5+
using UnityEngine.UI;
6+
7+
public class SaveManager : MonoBehaviour
8+
{
9+
public List<QuestionCatalogue> questionCatalogueList;
10+
public PlayerProfile playerProfile;
11+
public QuestionCatalogue createdQuestionCatalogue;
12+
public Canvas globalCanvas;
13+
14+
private void Start()
15+
{
16+
playerProfile = SaveSystem.instance.loadPlayerProfileFromJson("Player");
17+
questionCatalogueList = SaveSystem.instance.loadQuestionCataloguesFromJson();
18+
19+
if (playerProfile.userName == null)
20+
{
21+
openUsernamePanel();
22+
}
23+
playerProfileStatistics();
24+
}
25+
26+
public void savePlayerProfileToFile()
27+
{
28+
SaveSystem.instance.savePlayerProfileToJson(playerProfile);
29+
Debug.Log("Saved Active PlayerProfile to FileSystem in " + Application.persistentDataPath);
30+
}
31+
32+
public void addQuestionToOwnQuestionCatalogue()
33+
{
34+
string questionName;
35+
int rightAnswerPosition = 0; //By Default the First Answer is always the correct answer
36+
List<string> answers = new List<string>(); //The Starting Index of such a List is 0. The First Element of this List can be accessed via "answers[0]".
37+
38+
questionName = GameObject.Find("InputField_QuestionName").GetComponentInChildren<Text>().text; //UI-InputField for QuestionName must be named "InputField_QuestionName"
39+
GameObject[] answerInputFields = GameObject.FindGameObjectsWithTag("Created_Question_Answer"); //UI-InputFields for Question-Answers must have Tag "Created_Question_Answer"
40+
41+
foreach (GameObject answerInputField in answerInputFields)
42+
{
43+
string answerText = answerInputField.GetComponentInChildren<Text>().text;
44+
if (string.IsNullOrWhiteSpace(answerText))
45+
{
46+
Debug.Log("An Answer was found to be Empty or unusable.\nQuestion Adding Process has been stopped.\nPlease Fill Answer InputFields with reasonable Information.");
47+
return; //Exits the Function
48+
}
49+
else
50+
{
51+
answers.Add(answerText);
52+
}
53+
}
54+
55+
GameObject[] answerToggles = GameObject.FindGameObjectsWithTag("Created_Question_Answer_Toggle"); //UI-InputFields for Question-Answers must have Tag "Created_Question_Answer"
56+
//The Toggles must all belong to the same Toggle-Group and an option to only nable one Toggle must be activated
57+
int answerID = 0;
58+
foreach (GameObject answerToggle in answerToggles)
59+
{
60+
bool active = answerToggle.GetComponent<Toggle>().isOn;
61+
if (active == true)
62+
{
63+
rightAnswerPosition = answerID;
64+
break;
65+
}
66+
else
67+
{
68+
answerID++;
69+
break;
70+
}
71+
}
72+
Question question = new Question(questionName, answers, rightAnswerPosition);
73+
createdQuestionCatalogue.questions.Add(question);
74+
Debug.Log("A new Question has been added to the currently active QuestionCatalogue.");
75+
}
76+
77+
public void displayQuestionCount()
78+
{
79+
int questionCount = createdQuestionCatalogue.questions.Count;
80+
GameObject.Find("Label_QuestionCount").GetComponentInChildren<Text>().text = questionCount.ToString(); //UI-Label for the Display of the current Question Count has to be named "Label_QuestionCount"
81+
}
82+
83+
public void saveQuestionCatalogueToFileSystem()
84+
{
85+
string fileName = GameObject.Find("InputField_QuestionCatalogueName").GetComponentInChildren<Text>().text;
86+
if (string.IsNullOrWhiteSpace(fileName))
87+
{
88+
Debug.Log("Catalogue Name was found to be Empty or unusable.\nCatalogue Saving Process has been stopped.\nPlease Fill the Catalogue Name InputField with reasonable Information.");
89+
return; //Exits the Function
90+
}
91+
else
92+
{
93+
createdQuestionCatalogue.fileName = fileName;
94+
}
95+
96+
if (createdQuestionCatalogue.questions.Count < 5)
97+
{
98+
Debug.Log("A Catalogue has to have a minimum of 5 added Questions.\nCatalogue Saving Process has been stopped.\nPlease add More Questions.");
99+
return; //Exits the Function
100+
}
101+
SaveSystem.instance.saveQuestionCatalogueToJson(createdQuestionCatalogue);
102+
103+
104+
}
105+
public void setUsername()
106+
{
107+
string username = GameObject.Find("InputField_Username").GetComponentInChildren<Text>().text;
108+
if (string.IsNullOrWhiteSpace(username))
109+
{
110+
Debug.Log("Username was found to be Empty or unusable.\nUsername Saving Process has been stopped.\nPlease Fill the Username InputField with reasonable Information.");
111+
return; //Exits the Function
112+
}
113+
else
114+
{
115+
playerProfile.userName = username;
116+
SaveSystem.instance.savePlayerProfileToJson(playerProfile);
117+
}
118+
}
119+
120+
public void openUsernamePanel()
121+
{
122+
//Open a UI-Panel with an InputField for the User to Input his Username
123+
GameObject usernamePanel = globalCanvas.transform.Find("Panel_Username").gameObject; //The Panel named "Panel_Username" must be a direct child of the global UI canvas
124+
usernamePanel.SetActive(true);
125+
}
126+
127+
public void playerProfileStatistics()
128+
{
129+
bool newStatistic = false;
130+
//Compares Available Statistics to Available Catalogues
131+
foreach (QuestionCatalogue questionCatalogue in questionCatalogueList)
132+
{
133+
bool found = false;
134+
foreach (Statistic statistic in playerProfile.statistics)
135+
{
136+
if (string.Equals(questionCatalogue.fileName, statistic.label))
137+
{
138+
found = true;
139+
}
140+
}
141+
if (found == false)
142+
{
143+
addStatistic(questionCatalogue.fileName, 0);
144+
newStatistic = true;
145+
}
146+
147+
}
148+
if (newStatistic == true)
149+
{
150+
SaveSystem.instance.savePlayerProfileToJson(playerProfile);
151+
}
152+
153+
}
154+
private void addStatistic(string label, int score)
155+
{
156+
Statistic statistic = new Statistic(label, score);
157+
playerProfile.statistics.Add(statistic);
158+
Debug.Log("A new Statistic " + statistic.label + " has been added." );
159+
}
160+
161+
public void updateStatistic(string label, int score) //Can be called after every QuestionCatalogue-Session
162+
{
163+
bool changed = false;
164+
foreach (Statistic statistic in playerProfile.statistics)
165+
{
166+
if (string.Equals(label, statistic.label) && score > statistic.score)
167+
{
168+
statistic.score = score;
169+
changed = true;
170+
}
171+
}
172+
if (changed == true)
173+
{
174+
SaveSystem.instance.savePlayerProfileToJson(playerProfile);
175+
}
176+
}
177+
178+
179+
180+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using System.IO;
5+
using SaveData;
6+
7+
public class SaveSystem : MonoBehaviour
8+
{
9+
//Singleton Declaration
10+
public static SaveSystem instance;
11+
12+
string filePath;
13+
string playerProfilePath;
14+
15+
16+
private void Awake()
17+
{
18+
filePath = Application.persistentDataPath + "/Test.cat";
19+
playerProfilePath = Application.persistentDataPath + "/Player.pp";
20+
21+
Debug.Log(filePath);
22+
Debug.Log(playerProfilePath);
23+
24+
//Singleton Pattern
25+
if (SaveSystem.instance == null && instance == null)
26+
{
27+
Debug.Log("No existing SaveSystem Instance found. Creating new one.");
28+
instance = this;
29+
}
30+
else
31+
{
32+
Debug.Log("Existing SaveSystem Instance found. Secure Self Destruction executed.");
33+
Destroy(gameObject);
34+
}
35+
36+
}
37+
38+
public void saveQuestionCatalogueToJson(QuestionCatalogue _questionCatalogue)
39+
{
40+
string fileName = "/" + _questionCatalogue.fileName + ".cat";
41+
string content = JsonUtility.ToJson(_questionCatalogue);
42+
System.IO.File.WriteAllText(Application.persistentDataPath + fileName, content);
43+
Debug.Log("QuestionCatalogue has been successfully saved to the FileSystem");
44+
}
45+
46+
public void savePlayerProfileToJson(PlayerProfile _playerProfile)
47+
{
48+
string fileName = "/" + _playerProfile.fileName + ".pp";
49+
string content = JsonUtility.ToJson(_playerProfile);
50+
System.IO.File.WriteAllText(Application.persistentDataPath + fileName, content);
51+
Debug.Log("PlayerProfile has been successfully saved to the FileSystem");
52+
}
53+
54+
55+
public List<QuestionCatalogue> loadQuestionCataloguesFromJson()
56+
{
57+
string extension = ".cat";
58+
List<QuestionCatalogue> allCatalogues = new List<QuestionCatalogue>();
59+
string[] Files = Directory.GetFiles(Application.persistentDataPath, extension);
60+
foreach (string file in Files)
61+
{
62+
Debug.Log("The following file has been found: " + Path.GetFileName(file));
63+
string DataFromJson = File.ReadAllText(file);
64+
QuestionCatalogue questionCatalogue = JsonUtility.FromJson<QuestionCatalogue>(DataFromJson);
65+
allCatalogues.Add(questionCatalogue);
66+
Debug.Log("A catalogue has been added to the List.");
67+
}
68+
if (Files.Length == 0)
69+
{
70+
Debug.Log("No files with the extension " + extension + " have been found.");
71+
return null;
72+
}
73+
Debug.Log("The List of found QuestionCatalogues contains " + allCatalogues.Count + "QuestionCatalogues.");
74+
return allCatalogues;
75+
}
76+
77+
public PlayerProfile loadPlayerProfileFromJson(string fileName)
78+
{
79+
string extension = ".pp";
80+
string[] Files = Directory.GetFiles(Application.persistentDataPath, extension);
81+
foreach (string file in Files)
82+
{
83+
Debug.Log("The following file has been found: " + Path.GetFileName(file));
84+
if (string.Equals(fileName, Path.GetFileName(file)))
85+
{
86+
string DataFromJson = File.ReadAllText(file);
87+
PlayerProfile playerProfile = JsonUtility.FromJson<PlayerProfile>(DataFromJson);
88+
89+
Debug.Log("----------\nERFOLGREICH\n----------");
90+
return playerProfile;
91+
}
92+
}
93+
if (Files.Length == 0)
94+
{
95+
Debug.Log("No files have been found with the extension " + extension);
96+
return null;
97+
}
98+
Debug.Log("----------\nOPERATION FAILED\n----------");
99+
return null;
100+
}
101+
102+
103+
104+
105+
}

0 commit comments

Comments
 (0)