task timer + logic
This commit is contained in:
122
Unity/Assets/Scripts/Editor/TaskTimerSetup.cs
Normal file
122
Unity/Assets/Scripts/Editor/TaskTimerSetup.cs
Normal file
@ -0,0 +1,122 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
public class TaskTimerSetup : EditorWindow
|
||||
{
|
||||
private float taskDuration = 300f;
|
||||
private bool useTextMeshPro = true;
|
||||
private float canvasDistance = 2f;
|
||||
private Vector3 canvasPosition = new Vector3(0, 1.5f, 2f);
|
||||
|
||||
[MenuItem("Tools/Setup Task Timer for VR")]
|
||||
public static void ShowWindow()
|
||||
{
|
||||
GetWindow<TaskTimerSetup>("Task Timer Setup");
|
||||
}
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
GUILayout.Label("Task Timer VR Setup", EditorStyles.boldLabel);
|
||||
GUILayout.Space(10);
|
||||
|
||||
taskDuration = EditorGUILayout.FloatField("Task Duration (seconds)", taskDuration);
|
||||
useTextMeshPro = EditorGUILayout.Toggle("Use TextMeshPro", useTextMeshPro);
|
||||
canvasDistance = EditorGUILayout.FloatField("Distance from Camera", canvasDistance);
|
||||
canvasPosition = EditorGUILayout.Vector3Field("Canvas Position", canvasPosition);
|
||||
|
||||
GUILayout.Space(10);
|
||||
|
||||
if (GUILayout.Button("Create Task Timer UI", GUILayout.Height(30)))
|
||||
{
|
||||
CreateTaskTimerUI();
|
||||
}
|
||||
|
||||
GUILayout.Space(10);
|
||||
EditorGUILayout.HelpBox(
|
||||
"This will create:\n" +
|
||||
"• World Space Canvas for VR\n" +
|
||||
"• Timer display (TextMeshPro or UI Text)\n" +
|
||||
"• TaskTimer script configured and ready\n\n" +
|
||||
"The timer will automatically switch to Lobby scene when time expires.",
|
||||
MessageType.Info);
|
||||
}
|
||||
|
||||
private void CreateTaskTimerUI()
|
||||
{
|
||||
// Create Canvas
|
||||
GameObject canvasObj = new GameObject("TaskTimer_Canvas");
|
||||
Canvas canvas = canvasObj.AddComponent<Canvas>();
|
||||
canvas.renderMode = RenderMode.WorldSpace;
|
||||
|
||||
CanvasScaler scaler = canvasObj.AddComponent<CanvasScaler>();
|
||||
scaler.dynamicPixelsPerUnit = 10;
|
||||
|
||||
canvasObj.AddComponent<GraphicRaycaster>();
|
||||
|
||||
// Set canvas transform for VR
|
||||
RectTransform canvasRect = canvasObj.GetComponent<RectTransform>();
|
||||
canvasRect.sizeDelta = new Vector2(400, 100);
|
||||
canvasRect.localScale = new Vector3(0.01f, 0.01f, 0.01f);
|
||||
canvasRect.position = canvasPosition;
|
||||
|
||||
// Create Panel Background
|
||||
GameObject panelObj = new GameObject("Background_Panel");
|
||||
panelObj.transform.SetParent(canvasObj.transform, false);
|
||||
|
||||
Image panelImage = panelObj.AddComponent<Image>();
|
||||
panelImage.color = new Color(0, 0, 0, 0.7f);
|
||||
|
||||
RectTransform panelRect = panelObj.GetComponent<RectTransform>();
|
||||
panelRect.anchorMin = new Vector2(0, 0);
|
||||
panelRect.anchorMax = new Vector2(1, 1);
|
||||
panelRect.sizeDelta = Vector2.zero;
|
||||
|
||||
// Create Timer Text
|
||||
GameObject textObj = new GameObject("Timer_Text");
|
||||
textObj.transform.SetParent(panelObj.transform, false);
|
||||
|
||||
TaskTimer timerScript = canvasObj.AddComponent<TaskTimer>();
|
||||
timerScript.taskDuration = taskDuration;
|
||||
timerScript.autoStart = true;
|
||||
|
||||
RectTransform textRect;
|
||||
|
||||
if (useTextMeshPro)
|
||||
{
|
||||
TextMeshProUGUI tmpText = textObj.AddComponent<TextMeshProUGUI>();
|
||||
tmpText.text = "00:00";
|
||||
tmpText.fontSize = 60;
|
||||
tmpText.alignment = TextAlignmentOptions.Center;
|
||||
tmpText.color = Color.white;
|
||||
tmpText.font = Resources.Load<TMP_FontAsset>("Fonts & Materials/LiberationSans SDF");
|
||||
|
||||
timerScript.timerTextTMP = tmpText;
|
||||
textRect = textObj.GetComponent<RectTransform>();
|
||||
}
|
||||
else
|
||||
{
|
||||
Text legacyText = textObj.AddComponent<Text>();
|
||||
legacyText.text = "00:00";
|
||||
legacyText.fontSize = 60;
|
||||
legacyText.alignment = TextAnchor.MiddleCenter;
|
||||
legacyText.color = Color.white;
|
||||
legacyText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
||||
|
||||
timerScript.timerTextLegacy = legacyText;
|
||||
textRect = textObj.GetComponent<RectTransform>();
|
||||
}
|
||||
|
||||
// Configure RectTransform after UI component is added
|
||||
textRect.anchorMin = new Vector2(0, 0);
|
||||
textRect.anchorMax = new Vector2(1, 1);
|
||||
textRect.sizeDelta = Vector2.zero;
|
||||
|
||||
// Select the created object
|
||||
Selection.activeGameObject = canvasObj;
|
||||
|
||||
Debug.Log("Task Timer UI created successfully! Configure position as needed for your VR setup.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user