task timer + logic

This commit is contained in:
tom.hempel
2025-10-12 14:31:20 +02:00
parent 568be7e628
commit 6e08e6a660
13 changed files with 327 additions and 16 deletions

BIN
Unity/Assets/Scenes/AR/AR-Task1.unity (Stored with Git LFS)

Binary file not shown.

BIN
Unity/Assets/Scenes/AR/AR-Task2.unity (Stored with Git LFS)

Binary file not shown.

BIN
Unity/Assets/Scenes/AR/AR-Task3.unity (Stored with Git LFS)

Binary file not shown.

BIN
Unity/Assets/Scenes/AR/AR-Task4.unity (Stored with Git LFS)

Binary file not shown.

BIN
Unity/Assets/Scenes/VR/VR -Task2.unity (Stored with Git LFS)

Binary file not shown.

BIN
Unity/Assets/Scenes/VR/VR -Task3.unity (Stored with Git LFS)

Binary file not shown.

BIN
Unity/Assets/Scenes/VR/VR -Task4.unity (Stored with Git LFS)

Binary file not shown.

BIN
Unity/Assets/Scenes/VR/VR-Task1.unity (Stored with Git LFS)

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: dc569e3aee2d48840afbc77565086020
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View 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.");
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 83c1e1e5c642f3f4bbdb2d5b8d4a82fe
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,159 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public class TaskTimer : MonoBehaviour
{
[Header("Timer Settings")]
[Tooltip("Total time for the task in seconds")]
public float taskDuration = 300f; // 5 minutes default
[Tooltip("Start timer automatically on scene load")]
public bool autoStart = true;
[Tooltip("Show warning when time is low (in seconds)")]
public float warningThreshold = 30f;
[Header("UI References")]
[Tooltip("TextMeshPro component to display timer (recommended for VR)")]
public TextMeshProUGUI timerTextTMP;
[Tooltip("Legacy UI Text component (if not using TextMeshPro)")]
public Text timerTextLegacy;
[Header("Visual Settings")]
[Tooltip("Normal color for timer text")]
public Color normalColor = Color.white;
[Tooltip("Warning color when time is running low")]
public Color warningColor = Color.red;
[Tooltip("Format: 'MM:SS' or 'HH:MM:SS'")]
public bool showHours = false;
private float timeRemaining;
private bool isRunning;
private void Start()
{
timeRemaining = taskDuration;
if (autoStart)
{
StartTimer();
}
UpdateTimerDisplay();
}
private void Update()
{
if (!isRunning)
return;
timeRemaining -= Time.deltaTime;
// Check if time is up
if (timeRemaining <= 0)
{
timeRemaining = 0;
OnTimerExpired();
}
UpdateTimerDisplay();
}
public void StartTimer()
{
isRunning = true;
Debug.Log($"TaskTimer: Timer started for {taskDuration} seconds");
}
public void PauseTimer()
{
isRunning = false;
Debug.Log("TaskTimer: Timer paused");
}
public void ResumeTimer()
{
isRunning = true;
Debug.Log("TaskTimer: Timer resumed");
}
public void ResetTimer()
{
timeRemaining = taskDuration;
UpdateTimerDisplay();
Debug.Log("TaskTimer: Timer reset");
}
public void AddTime(float seconds)
{
timeRemaining += seconds;
Debug.Log($"TaskTimer: Added {seconds} seconds. New time: {timeRemaining}");
}
private void UpdateTimerDisplay()
{
string timeText = FormatTime(timeRemaining);
// Update TextMeshPro
if (timerTextTMP != null)
{
timerTextTMP.text = timeText;
timerTextTMP.color = timeRemaining <= warningThreshold ? warningColor : normalColor;
}
// Update Legacy Text
if (timerTextLegacy != null)
{
timerTextLegacy.text = timeText;
timerTextLegacy.color = timeRemaining <= warningThreshold ? warningColor : normalColor;
}
}
private string FormatTime(float time)
{
int hours = Mathf.FloorToInt(time / 3600f);
int minutes = Mathf.FloorToInt((time % 3600f) / 60f);
int seconds = Mathf.FloorToInt(time % 60f);
if (showHours)
{
return string.Format("{0:00}:{1:00}:{2:00}", hours, minutes, seconds);
}
else
{
int totalMinutes = Mathf.FloorToInt(time / 60f);
return string.Format("{0:00}:{1:00}", totalMinutes, seconds);
}
}
private void OnTimerExpired()
{
isRunning = false;
Debug.Log("TaskTimer: Time expired! Returning to Lobby...");
// Load Lobby scene
SceneManager.LoadScene("Lobby");
}
// Public methods for external control
public float GetTimeRemaining()
{
return timeRemaining;
}
public bool IsRunning()
{
return isRunning;
}
public float GetProgress()
{
return 1f - (timeRemaining / taskDuration);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 50a2ca0ac07ab3f40b2ffe147eb4bc90
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: