(1.6.0) Release Commit
This commit is contained in:
@@ -12,6 +12,7 @@ namespace MxValheim.Patch.HUD
|
||||
{
|
||||
public static GameObject m_canvasObject;
|
||||
public static Text m_textComponent;
|
||||
public static RectTransform m_barFill;
|
||||
|
||||
// This replaces the missing GetTimeString() method
|
||||
public string GetFormattedTime()
|
||||
@@ -29,68 +30,62 @@ namespace MxValheim.Patch.HUD
|
||||
|
||||
public void CreateOverlay()
|
||||
{
|
||||
// 1. Root Canvas
|
||||
m_canvasObject = new GameObject("ModdedCanvas");
|
||||
Canvas canvas = m_canvasObject.AddComponent<Canvas>();
|
||||
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
canvas.sortingOrder = 999;
|
||||
|
||||
// 2. Background Panel
|
||||
// --- CLOCK PANEL ---
|
||||
GameObject panelObj = new GameObject("TextBackground");
|
||||
panelObj.transform.SetParent(m_canvasObject.transform, false);
|
||||
panelObj.AddComponent<Image>().color = new Color(0, 0, 0, 0.5f);
|
||||
|
||||
Image panelImage = panelObj.AddComponent<Image>();
|
||||
panelImage.color = new Color(0, 0, 0, 0.5f); // 50% transparent black
|
||||
ContentSizeFitter fitter = panelObj.AddComponent<ContentSizeFitter>();
|
||||
fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
|
||||
HorizontalLayoutGroup layout = panelObj.AddComponent<HorizontalLayoutGroup>();
|
||||
layout.padding = new RectOffset(15, 15, 5, 5);
|
||||
|
||||
RectTransform panelRect = panelObj.GetComponent<RectTransform>();
|
||||
panelRect.anchorMin = new Vector2(0.5f, 1.0f); // Top Center
|
||||
panelRect.anchorMin = new Vector2(0.5f, 1.0f);
|
||||
panelRect.anchorMax = new Vector2(0.5f, 1.0f);
|
||||
panelRect.pivot = new Vector2(0.5f, 1.0f);
|
||||
panelRect.anchoredPosition = new Vector2(0, -5); // 5px gap from top
|
||||
panelRect.sizeDelta = new Vector2(180, 35); // Width and Height of the bar
|
||||
panelRect.anchoredPosition = new Vector2(0, -5);
|
||||
|
||||
// 3. Text (Attached to Panel)
|
||||
// --- TEXT ---
|
||||
GameObject textObj = new GameObject("ModdedText");
|
||||
textObj.transform.SetParent(panelObj.transform, false);
|
||||
|
||||
m_textComponent = textObj.AddComponent<Text>();
|
||||
// Attempt to find Valheim's specific font, fallback to Arial if not found
|
||||
Font valheimFont = null;
|
||||
foreach (Font f in Resources.FindObjectsOfTypeAll<Font>())
|
||||
{
|
||||
if (f.name == "AveriaSerifLibre-Bold")
|
||||
{
|
||||
valheimFont = f;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Corrected fallback for older Unity versions used in Valheim
|
||||
if (valheimFont == null)
|
||||
{
|
||||
valheimFont = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
||||
}
|
||||
|
||||
m_textComponent.font = valheimFont;
|
||||
m_textComponent.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
||||
m_textComponent.fontSize = 18;
|
||||
m_textComponent.color = Color.white;
|
||||
m_textComponent.alignment = TextAnchor.MiddleCenter;
|
||||
m_textComponent.supportRichText = true;
|
||||
|
||||
// Add Shadow so it's visible in snow
|
||||
var shadow = textObj.AddComponent<Shadow>();
|
||||
shadow.effectColor = Color.black;
|
||||
shadow.effectDistance = new Vector2(2, 2);
|
||||
// --- PROGRESS BAR CONTAINER ---
|
||||
// This sits under the main rectangle
|
||||
GameObject barBgObj = new GameObject("BarBackground");
|
||||
barBgObj.transform.SetParent(m_canvasObject.transform, false);
|
||||
barBgObj.AddComponent<Image>().color = new Color(0.1f, 0.1f, 0.1f, 0.8f); // Dark background
|
||||
|
||||
// FIX: Correct way to make text fill the parent panel
|
||||
RectTransform textRect = textObj.GetComponent<RectTransform>();
|
||||
textRect.anchorMin = Vector2.zero; // (0, 0)
|
||||
textRect.anchorMax = Vector2.one; // (1, 1)
|
||||
textRect.pivot = new Vector2(0.5f, 0.5f);
|
||||
RectTransform barBgRect = barBgObj.GetComponent<RectTransform>();
|
||||
barBgRect.anchorMin = new Vector2(0.5f, 1.0f);
|
||||
barBgRect.anchorMax = new Vector2(0.5f, 1.0f);
|
||||
barBgRect.pivot = new Vector2(0.5f, 1.0f);
|
||||
// Positioned below the clock panel (Clock is 35px high + 5px gap = -40)
|
||||
barBgRect.anchoredPosition = new Vector2(0, -45);
|
||||
barBgRect.sizeDelta = new Vector2(180, 8); // Match width of clock, small height
|
||||
|
||||
// Resetting these to zero ensures the text box matches the panel exactly
|
||||
textRect.offsetMin = Vector2.zero;
|
||||
textRect.offsetMax = Vector2.zero;
|
||||
// --- PROGRESS BAR FILL ---
|
||||
GameObject fillObj = new GameObject("BarFill");
|
||||
fillObj.transform.SetParent(barBgObj.transform, false);
|
||||
fillObj.AddComponent<Image>().color = Color.yellow; // Classic Valheim XP color
|
||||
|
||||
m_barFill = fillObj.GetComponent<RectTransform>();
|
||||
m_barFill.anchorMin = new Vector2(0, 0);
|
||||
m_barFill.anchorMax = new Vector2(0.5f, 1); // This X value (0.5) will be updated via code
|
||||
m_barFill.pivot = new Vector2(0, 0.5f);
|
||||
m_barFill.offsetMin = Vector2.zero;
|
||||
m_barFill.offsetMax = Vector2.zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user