Repository navigation
Expand file tree
/
Copy pathTextOverlay.cs
More file actions
151 lines (127 loc) · 5.27 KB
/
Copy pathTextOverlay.cs
File metadata and controls
151 lines (127 loc) · 5.27 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
using BepInEx;
using Onward.UI;
namespace ModUpdater
{
public static class TextOverlay
{
private static GameObject canvasObject;
private static TextMeshProUGUI textComponent;
private static string lastText = "Checking for updates...";
private static NavigationToggle multiRef;
private static TextMeshProUGUI label;
public static void Init()
{
if (canvasObject != null)
Object.Destroy(canvasObject);
GameObject[] roots = SceneManager.GetActiveScene().GetRootGameObjects();
GameObject ui = null;
foreach (var root in roots)
{
if (root.name.Contains("::::") && root.name.Contains("UI"))
ui = root.gameObject;
if (root.name.Contains("NecessaryMainMenuComponents"))
ui = root.gameObject;
}
if (ui == null)
{
ModUpdaterPlugin.Log.LogError("couldn't find top level ui");
return;
}
GameObject topMenu = FindShallowest(ui, "Top Menu");
if (topMenu == null)
{
ModUpdaterPlugin.Log.LogError("Couldn't find `Top Menu` in the passed in UI");
return;
}
canvasObject = new GameObject("TextOverlayCanvas");
var canvas = canvasObject.AddComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
canvasObject.AddComponent<CanvasScaler>();
Transform anchor = topMenu.transform;
canvasObject.transform.position = anchor.position + anchor.up * 0.1f + anchor.right * 2.0625f;
canvasObject.transform.rotation = anchor.rotation;
// ModUpdaterPlugin.Log.LogError("Moved the Canvas to " + canvasObject.transform.position.ToString());
var canvasRect = canvasObject.GetComponent<RectTransform>();
canvasRect.sizeDelta = new Vector2(1000, 50);
canvasRect.localScale = Vector3.one * 0.003f;
var bgObj = new GameObject("Background");
bgObj.transform.SetParent(canvasObject.transform, false);
var bgImage = bgObj.AddComponent<Image>();
bgImage.color = new Color(0f, 0f, 0f, 0.6f);
var bgRect = bgObj.GetComponent<RectTransform>();
bgRect.anchorMin = Vector2.zero;
bgRect.anchorMax = Vector2.one;
bgRect.offsetMin = Vector2.zero;
bgRect.offsetMax = Vector2.zero;
var textObj = new GameObject("OverlayText");
textObj.transform.SetParent(canvasObject.transform, false);
textComponent = textObj.AddComponent<TextMeshProUGUI>();
textComponent.text = lastText;
textComponent.fontSize = 24;
textComponent.color = Color.white;
textComponent.alignment = TextAlignmentOptions.Center;
var textRect = textComponent.GetComponent<RectTransform>();
textRect.anchorMin = Vector2.zero;
textRect.anchorMax = Vector2.one;
textRect.offsetMin = Vector2.zero;
textRect.offsetMax = Vector2.zero;
GameObject multiplayerBtn = GameObject.Find("BTN Multiplayer");
if (multiplayerBtn != null)
{
multiRef = multiplayerBtn.GetComponentInChildren<NavigationToggle>();
if (multiRef == null)
ModUpdaterPlugin.Log.LogError("Found 'BTN Multiplayer' but no NavigationToggle component on it");
GameObject labelGameObject = FindShallowest(multiplayerBtn, "TXT Label");
if(labelGameObject != null)
{
label = labelGameObject.GetComponent<TextMeshProUGUI>();
}
}
else
{
ModUpdaterPlugin.Log.LogError("Couldn't find 'BTN Multiplayer' GameObject");
}
}
public static GameObject FindShallowest(GameObject parent, string name)
{
if (parent == null)
return null;
Transform t = parent.transform;
int count = t.childCount;
for (int i = 0; i < count; i++)
{
if (t.GetChild(i).name == name)
return t.GetChild(i).gameObject;
}
for (int i = 0; i < count; i++)
{
GameObject result = FindShallowest(t.GetChild(i).gameObject, name);
if (result != null)
return result;
}
return null;
}
public static void SetText(string text)
{
lastText = text;
if (textComponent != null)
textComponent.text = text;
}
public static void DisableMultiButton(bool disableIt)
{
if(multiRef != null)
multiRef.interactable = !disableIt;
if(label != null)
{
Color color = label.color;
color.a = disableIt ? 0.01f : 0.78431f;
label.color = color;
label.gameObject.SetActive(!disableIt);
}
}
}
}