Skip to content

Commit 5d15c53

Browse files
committed
fix: dont create own canvas, change pixelfix creation
1 parent c2d7ac2 commit 5d15c53

1 file changed

Lines changed: 20 additions & 64 deletions

File tree

JotunnLib/Managers/GUIManager.cs

Lines changed: 20 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ public static GUIManager Instance
4848
/// </summary>
4949
public static GameObject PixelFix { get; private set; }
5050

51-
/// <summary>
52-
/// Internal container for GUI elements created by the GUIManager.
53-
/// Acts as a static transform for the base GUI elements which can
54-
/// be instantiated with this classes helper methods.
55-
/// </summary>
56-
internal static GameObject GUIContainer;
57-
5851
/// <summary>
5952
/// Unity layer constant for UI objects.
6053
/// </summary>
@@ -193,21 +186,6 @@ public void Init()
193186
// Dont init on a headless server
194187
if (!IsHeadless())
195188
{
196-
GUIContainer = new GameObject("GUI");
197-
GUIContainer.transform.SetParent(Main.RootObject.transform);
198-
GUIContainer.layer = UILayer; // UI
199-
var canvas = GUIContainer.AddComponent<Canvas>();
200-
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
201-
canvas.sortingOrder = 1;
202-
GUIContainer.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);
203-
GUIContainer.AddComponent<GraphicRaycaster>();
204-
GUIContainer.AddComponent<CanvasScaler>();
205-
GUIContainer.GetComponent<RectTransform>().anchorMin = new Vector2(0.5f, 0.5f);
206-
GUIContainer.GetComponent<RectTransform>().anchorMax = new Vector2(0.5f, 0.5f);
207-
GUIContainer.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0);
208-
GUIContainer.GetComponent<Canvas>().planeDistance = 0.0f;
209-
GUIContainer.AddComponent<GuiScaler>().UpdateScale();
210-
211189
SceneManager.sceneLoaded += InitialLoad;
212190
SceneManager.sceneLoaded += SceneManager_sceneLoaded;
213191
On.KeyHints.UpdateHints += ShowCustomKeyHint;
@@ -216,12 +194,6 @@ public void Init()
216194

217195
internal void InitialLoad(Scene scene, LoadSceneMode loadMode)
218196
{
219-
// No resource loading on a headless server
220-
if (IsHeadless())
221-
{
222-
return;
223-
}
224-
225197
// Load valheim GUI assets
226198
if (scene.name == "start")
227199
{
@@ -361,41 +333,30 @@ private void SceneManager_sceneLoaded(Scene scene, LoadSceneMode loadMode)
361333
if (scene.name == "start" && !GUIInStart)
362334
{
363335
// Create a new PixelFix for start scene
364-
GUIContainer.SetActive(true);
365-
CreatePixelFix();
366-
GUIInStart = true;
336+
GameObject root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == "GuiRoot");
337+
Transform gui = root?.transform.Find("GUI");
338+
if (!gui)
339+
{
340+
Logger.LogWarning("StartGui not found, not creating custom GUI");
341+
return;
342+
}
343+
CreatePixelFix(gui);
367344

368-
InvokeOnPixelFixCreated();
345+
GUIInStart = true;
369346
}
370347
if (scene.name == "main" && GUIInStart)
371348
{
372-
// Destroy our own PixelFix from the start scene
373-
if (PixelFix)
374-
{
375-
GameObject.DestroyImmediate(PixelFix);
376-
}
377-
378349
// Create a new PixelFix for main scene
379350
GameObject root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == "_GameMain");
380-
if (!root)
381-
{
382-
Logger.LogWarning("_GameMain not found, not creating custom GUI");
383-
return;
384-
}
385-
386-
GameObject gui = root.transform.Find("GUI/PixelFix").gameObject;
351+
Transform gui = root?.transform.Find("GUI");
387352
if (!gui)
388353
{
389354
Logger.LogWarning("PixelFix not found, not creating custom GUI");
390355
return;
391356
}
392-
PixelFix = new GameObject("GUIFix", typeof(RectTransform));
393-
PixelFix.layer = UILayer; // UI
394-
PixelFix.transform.SetParent(gui.transform, false);
395-
GUIContainer.SetActive(false);
396-
GUIInStart = false;
357+
CreatePixelFix(gui);
397358

398-
InvokeOnPixelFixCreated();
359+
GUIInStart = false;
399360

400361
// Get the KeyHints transform for this scene to create new KeyHint objects
401362
KeyHintContainer = (RectTransform)root.transform.Find("GUI/PixelFix/IngameGui(Clone)/HUD/hudroot/KeyHints");
@@ -406,23 +367,18 @@ private void SceneManager_sceneLoaded(Scene scene, LoadSceneMode loadMode)
406367
}
407368
}
408369

409-
private void InvokeOnPixelFixCreated()
370+
private void CreatePixelFix(Transform parent)
410371
{
411-
OnPixelFixCreated?.SafeInvoke();
372+
PixelFix = new GameObject("CustomGUI", typeof(RectTransform), typeof(GuiPixelFix));
373+
PixelFix.layer = UILayer; // UI
374+
PixelFix.transform.SetParent(parent.transform, false);
375+
376+
InvokeOnPixelFixCreated();
412377
}
413378

414-
private void CreatePixelFix()
379+
private void InvokeOnPixelFixCreated()
415380
{
416-
PixelFix = new GameObject("GUIFix", typeof(RectTransform), typeof(GuiPixelFix));
417-
PixelFix.layer = UILayer; // UI
418-
PixelFix.transform.SetParent(GUIContainer.transform);
419-
PixelFix.GetComponent<RectTransform>().localScale = new Vector3(1, 1, 1);
420-
PixelFix.GetComponent<RectTransform>().anchorMin = new Vector2(0.5f, 0.5f);
421-
PixelFix.GetComponent<RectTransform>().anchorMax = new Vector2(0.5f, 0.5f);
422-
PixelFix.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, GUIContainer.GetComponent<RectTransform>().rect.width);
423-
PixelFix.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, GUIContainer.GetComponent<RectTransform>().rect.height);
424-
PixelFix.GetComponent<RectTransform>().anchoredPosition = new Vector2(GUIContainer.GetComponent<RectTransform>().rect.width / 2f,
425-
GUIContainer.GetComponent<RectTransform>().rect.height / 2f);
381+
OnPixelFixCreated?.SafeInvoke();
426382
}
427383

428384
/// <summary>

0 commit comments

Comments
 (0)