using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; [Serializable] public class DrawingSettings { public Color startingColor = Color.white; public List palette = new List(); public float timeLimit = 99f; public string actName = "Untitled"; public string actDescription = "This Act is missing a description."; } public class DrawingManager : MonoBehaviour { [Header("Settings")] public string jsonString = ""; public DrawingSettings settings = null; [Header("Prefabs")] public GameObject drawableFramePrefab; public GameObject paletteButtonPrefab; public GameObject radiusSliderPrefab; public GameObject actNamePrefab; public GameObject actDescriptionPrefab; public GameObject timerPrefab; [Header("Objects")] protected Canvas canvas; protected PencilTool pencil; protected DrawableFrame drawableFrame; protected List paletteButtons; protected RadiusSlider radiusSlider; protected Text actName; protected Text actDescription; protected Timer timer; [Header("Results")] public byte[] png = null; 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)); } canvas = GameObject.FindGameObjectWithTag("Canvas").GetComponent(); drawableFrame = Instantiate(drawableFramePrefab, transform).GetComponent(); drawableFrame.startingColor = settings.startingColor; pencil = drawableFrame.GetComponent(); try { pencil.selectedColor = settings.palette[0]; } catch(ArgumentOutOfRangeException) { pencil.selectedColor = settings.startingColor; } paletteButtons = new List(); for(int i = 0; i < settings.palette.Count; i++) { PaletteButton button = Instantiate(paletteButtonPrefab, canvas.transform).GetComponent(); RectTransform btnTransform = button.GetComponent(); Image btnImage = button.GetComponent(); button.pencil = pencil; btnImage.color = settings.palette[i]; btnTransform.anchoredPosition = new Vector2(-420 + i * 110, 150); paletteButtons.Add(button); } radiusSlider = Instantiate(radiusSliderPrefab, canvas.transform).GetComponent(); radiusSlider.pencil = pencil; 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; ActStart(); } protected void ActStart() { timer.TimerStart(); drawableFrame.locked = false; } protected void ActEnd(object sender, EventArgs e) { drawableFrame.locked = true; png = drawableFrame.ToPNG(); } }