-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfoBox.cs
67 lines (58 loc) · 1.82 KB
/
InfoBox.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class InfoBox : MonoBehaviour {
// Start is called before the first frame update
GameObject canvas;
GameObject infoBox;
GameObject infoBackground;
private float timeLeft = 0f;
private bool timer = false;
private float timerSeconds = 3f;
[TextArea(10, 20)]
public string message;
private void Update() {
if (timer && timeLeft > 0) {
timeLeft -= Time.deltaTime;
if (timeLeft < 0) {
HideBox();
}
}
}
void Awake() {
canvas = GameObject.FindGameObjectWithTag("Canvas");
if (canvas == null) return;
infoBox = canvas.transform.Find("InfoBox").gameObject;
infoBackground = infoBox.transform.Find("InfoBackground").gameObject;
}
public void ShowBox(string message, bool timer) {
if (message == "" || canvas == null) return;
this.timer = timer;
infoBox.GetComponentInChildren<TextMeshProUGUI>().text = message;
infoBackground.GetComponent<Image>().color = new Color(255, 255, 255, 0.5f);
if (timer) {
StartTimer();
}
}
public void HideBox() {
if (canvas == null) return;
infoBox.GetComponentInChildren<TextMeshProUGUI>().text = "";
infoBackground.GetComponent<Image>().color = new Color(255, 255, 255, 0f);
}
private void OnTriggerEnter(Collider other) {
if (other.CompareTag("Player")) {
ShowBox(this.message, false);
}
}
private void OnTriggerExit(Collider other) {
if (other.CompareTag("Player") && !timer) {
HideBox();
}
}
private void StartTimer() {
timeLeft = timerSeconds;
}
}