private static void AddTemplateToHud(HudElement element, RectTransform rt) { // Should we add these to their own elements? Based on their group? // roots.TryGetValue(Groups.HudRoot, out Transform templateRoot); // Everything on hudRoot // roots.TryGetValue(element.group, out Transform templateRoot); Transform go = UnityEngine.Object.Instantiate(hudRoot.Find("BuildHud/SelectedInfo"), baseRoot); go.gameObject.name = $"{element.name}{templateSuffix}"; go.Find("selected_piece").gameObject.SetActive(false); go.Find("requirements").gameObject.SetActive(false); Text t = go.gameObject.AddComponent <Text>(); t.text = $"{element.displayName}"; t.font = Resources.GetBuiltinResource <Font>("Arial.ttf"); t.fontSize = 20; t.alignment = TextAnchor.MiddleCenter; go.gameObject.SetActive(false); // Have it hidden when added RectTransform templateRT = go.GetComponent <RectTransform>(); templateRT.pivot = rt.pivot; templateRT.anchorMin = rt.anchorMin; templateRT.anchorMax = rt.anchorMax; templateRT.offsetMin = rt.offsetMin; templateRT.offsetMax = rt.offsetMax; templateRT.sizeDelta = rt.sizeDelta; templateRT.anchoredPosition = rt.anchoredPosition; templateRT.position = rt.position; templateRT.localEulerAngles = rt.localEulerAngles; t.resizeTextForBestFit = true; }
public static void UpdatePosition(string name, Vector3 pos, float size) { HudElement element = elements.Find(e => e.name == name); if (element.name == name) { element.x += pos.x; element.y += pos.y; if (size != 0f) { element.SetScale(size); Player.m_localPlayer.Message(MessageHud.MessageType.Center, $"{element.displayName} size: {element.scale}"); } if (element.group == Groups.Inventory) { Vector3 newPos = Camera.main.ScreenToViewportPoint(new Vector3(pos.x, pos.y, pos.z)); //element.SetAnchors(newPos, newPos); //element.anchorMin += new Vector2(newPos.x, newPos.y); element.UpdateAnchors(newPos, newPos); //element.anchorMax += new Vector2(newPos.x, newPos.y); } // Update element & template position PositionTemplate(element); } }
// This creates issues with Text. Scaling is off! public static void UpdateDimensions(string name, float size) { HudElement element = elements.Find(e => e.name == name); if (element.name == name) { if (size != 0f) { element.SetDims(size); Player.m_localPlayer.Message(MessageHud.MessageType.Center, $"{element.displayName} dimensions: (1,{element.dimensions})"); // Update element & template position PositionTemplate(element); } } else { Helpers.DebugLine($"Invalid call when updating element: {name}", true, true); } }
private static void PositionTemplate(HudElement e) { try { RectTransform rt = LocateRectTransform(e.group, e.path); // Original object RectTransform tt = LocateTemplateRect(e.group, e.name); // Your generated template //Helpers.DebugLine($"{rt} {rt.anchorMin} {e.GetPosition()}"); if (rt) { if (e.group == Groups.Inventory) { float gameScale = GameObject.Find("GUI").GetComponent <CanvasScaler>().scaleFactor; //Helpers.DebugLine($"\n{e.GetPosition()}\n{gameScale}\n{Camera.main.ViewportToScreenPoint(e.GetAnchorMin())}\n{tt.position}"); //Helpers.DebugLine($"\n{e.GetPosition() / gameScale}"); // Original object are moved by anchors Vector3 cPos = Camera.main.ViewportToScreenPoint(e.GetAnchorMax()); Vector2 ePos = e.GetPosition(); rt.anchorMin = e.GetAnchorMin(); rt.anchorMax = e.GetAnchorMax(); tt.anchoredPosition = e.GetPosition() / gameScale; } else { rt.anchoredPosition = e.GetPosition(); tt.anchoredPosition = e.GetPosition(); //rt.anchoredPosition; } rt.localScale = new Vector3(e.GetScale(), e.GetScale() * e.GetDims()); tt.localScale = rt.localScale; } } catch { Helpers.DebugLine($"PositionTemplate Catch: {e.name}"); } }