using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; [Serializable] public class TypingSettings { public float timeLimit = 99f; public string actName = "Untitled"; public string actDescription = "This Act is missing a description."; } public class TypingManager : MonoBehaviour { [Header("Settings")] public string jsonString = ""; public TypingSettings settings = null; [Header("Prefabs")] public GameObject actNamePrefab; public GameObject actDescriptionPrefab; public GameObject timerPrefab; public GameObject inputFieldPrefab; public GameObject submitPrefab; public GameObject submittedCountPrefab; [Header("Objects")] protected Text actName; protected Text actDescription; protected Timer timer; protected Canvas canvas; protected InputField inputField; protected Submit submit; protected Text submittedCount; protected EventSystem eventSystem; [Header("Results")] public List texts; protected void Start() { if(jsonString != "") { JsonUtility.FromJsonOverwrite(jsonString, settings); if(settings == null) { Debug.LogWarning("Invalid settings json string, using defaults."); } } else { Debug.Log(JsonUtility.ToJson(settings)); } texts = new List(); canvas = GameObject.FindGameObjectWithTag("Canvas").GetComponent(); eventSystem = GameObject.FindGameObjectWithTag("EventSystem").GetComponent(); actName = Instantiate(actNamePrefab, canvas.transform).GetComponent(); actName.text = settings.actName; actDescription = Instantiate(actDescriptionPrefab, canvas.transform).GetComponent(); actDescription.text = settings.actDescription; timer = Instantiate(timerPrefab, canvas.transform).GetComponent(); timer.TimerSet(settings.timeLimit); timer.OnTimeOut += ActEnd; inputField = Instantiate(inputFieldPrefab, canvas.transform).GetComponent(); Submit inputFieldSubmit = inputField.GetComponent(); inputFieldSubmit.typingManager = this; inputFieldSubmit.inputField = inputField; submit = Instantiate(submitPrefab, canvas.transform).GetComponent(); submit.typingManager = this; submit.inputField = inputField; submittedCount = Instantiate(submittedCountPrefab, canvas.transform).GetComponent(); submittedCount.text = ""; ActStart(); } protected void ActStart() { timer.TimerStart(); eventSystem.SetSelectedGameObject(inputField.gameObject); } protected void ActEnd(object sender, EventArgs e) { inputField.enabled = false; submit.enabled = false; } public void SubmitText(string text) { if(text != "") { texts.Add(text); submittedCount.text = String.Format("Submitted: {0}", texts.Count); } } }