using UnityEngine; using UnityEditor; using UnityEngine.UI; using UnityEngine.EventSystems; public static class SetupHealthBarHUD { [MenuItem("Tools/Setup Health Bar HUD")] public static void Setup() { var existingCanvas = GameObject.Find("HUD_Canvas"); if (existingCanvas != null) { Object.DestroyImmediate(existingCanvas); } // Canvas GameObject canvasGo = new GameObject("HUD_Canvas"); Canvas canvas = canvasGo.AddComponent(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; CanvasScaler scaler = canvasGo.AddComponent(); scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; scaler.referenceResolution = new Vector2(1920, 1080); scaler.matchWidthOrHeight = 0.5f; canvasGo.AddComponent(); // EventSystem if (Object.FindAnyObjectByType() == null) { GameObject esGo = new GameObject("EventSystem"); esGo.AddComponent(); esGo.AddComponent(); Undo.RegisterCreatedObjectUndo(esGo, "Create EventSystem"); } // HealthBar Root Container (Top-Right) GameObject hbContainer = new GameObject("HealthBar", typeof(RectTransform)); hbContainer.transform.SetParent(canvasGo.transform, false); RectTransform hbRect = hbContainer.GetComponent(); hbRect.anchorMin = new Vector2(1f, 1f); hbRect.anchorMax = new Vector2(1f, 1f); hbRect.pivot = new Vector2(1f, 1f); hbRect.anchoredPosition = new Vector2(-40f, -40f); hbRect.sizeDelta = new Vector2(280f, 36f); // Background GameObject bgGo = new GameObject("Background", typeof(RectTransform), typeof(Image)); bgGo.transform.SetParent(hbContainer.transform, false); RectTransform bgRect = bgGo.GetComponent(); bgRect.anchorMin = Vector2.zero; bgRect.anchorMax = Vector2.one; bgRect.offsetMin = Vector2.zero; bgRect.offsetMax = Vector2.zero; Image bgImg = bgGo.GetComponent(); bgImg.color = new Color(0.08f, 0.08f, 0.08f, 0.85f); // Health Fill (Filled Image) GameObject fillGo = new GameObject("HealthFill", typeof(RectTransform), typeof(Image)); fillGo.transform.SetParent(hbContainer.transform, false); RectTransform fillRect = fillGo.GetComponent(); fillRect.anchorMin = Vector2.zero; fillRect.anchorMax = Vector2.one; fillRect.offsetMin = new Vector2(4f, 4f); fillRect.offsetMax = new Vector2(-4f, -4f); Image fillImg = fillGo.GetComponent(); fillImg.type = Image.Type.Filled; fillImg.fillMethod = Image.FillMethod.Horizontal; fillImg.fillOrigin = (int)Image.OriginHorizontal.Left; fillImg.fillAmount = 1f; fillImg.color = new Color(0.2f, 0.85f, 0.3f, 1f); // Text (Current / Max HP) GameObject textGo = new GameObject("Text_HP", typeof(RectTransform), typeof(Text)); textGo.transform.SetParent(hbContainer.transform, false); RectTransform textRect = textGo.GetComponent(); textRect.anchorMin = Vector2.zero; textRect.anchorMax = Vector2.one; textRect.offsetMin = Vector2.zero; textRect.offsetMax = Vector2.zero; Text hpText = textGo.GetComponent(); hpText.text = "100 / 100"; hpText.alignment = TextAnchor.MiddleCenter; hpText.fontSize = 20; hpText.fontStyle = FontStyle.Bold; hpText.color = Color.white; Font defaultFont = Resources.GetBuiltinResource("LegacyRuntime.ttf"); if (defaultFont == null) defaultFont = Resources.GetBuiltinResource("Arial.ttf"); hpText.font = defaultFont; // PlayerHealthUI PlayerHealthUI uiComp = hbContainer.AddComponent(); uiComp.healthFillImage = fillImg; uiComp.healthText = hpText; uiComp.playerHealth = Object.FindAnyObjectByType(); // Crosshair / Center Cursor (Center of Screen) GameObject crosshairGo = new GameObject("Crosshair", typeof(RectTransform)); crosshairGo.transform.SetParent(canvasGo.transform, false); RectTransform crosshairRect = crosshairGo.GetComponent(); crosshairRect.anchorMin = new Vector2(0.5f, 0.5f); crosshairRect.anchorMax = new Vector2(0.5f, 0.5f); crosshairRect.pivot = new Vector2(0.5f, 0.5f); crosshairRect.anchoredPosition = Vector2.zero; crosshairRect.sizeDelta = new Vector2(8f, 8f); // Outline GameObject chOutlineGo = new GameObject("Outline", typeof(RectTransform), typeof(Image)); chOutlineGo.transform.SetParent(crosshairGo.transform, false); RectTransform chOutlineRect = chOutlineGo.GetComponent(); chOutlineRect.anchorMin = Vector2.zero; chOutlineRect.anchorMax = Vector2.one; chOutlineRect.offsetMin = new Vector2(-1f, -1f); chOutlineRect.offsetMax = new Vector2(1f, 1f); Image chOutlineImg = chOutlineGo.GetComponent(); chOutlineImg.color = new Color(0f, 0f, 0f, 0.7f); // Center Dot GameObject chDotGo = new GameObject("Dot", typeof(RectTransform), typeof(Image)); chDotGo.transform.SetParent(crosshairGo.transform, false); RectTransform chDotRect = chDotGo.GetComponent(); chDotRect.anchorMin = Vector2.zero; chDotRect.anchorMax = Vector2.one; chDotRect.offsetMin = Vector2.zero; chDotRect.offsetMax = Vector2.zero; Image chDotImg = chDotGo.GetComponent(); chDotImg.color = new Color(1f, 1f, 1f, 0.95f); // CrosshairUI component CrosshairUI chScript = crosshairGo.AddComponent(); chScript.dotImage = chDotImg; chScript.outlineImage = chOutlineImg; Undo.RegisterCreatedObjectUndo(canvasGo, "Create HUD_Canvas with HealthBar and Crosshair"); UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(canvasGo.scene); Debug.Log("HUD HealthBar и прицел в центре экрана успешно созданы!"); } }