Files
my-project/Assets/Inventory/Scripts/InventorySceneSetup.cs
2026-04-16 01:51:46 +05:00

134 lines
6.1 KiB
C#

using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;
/// <summary>
/// Tools > Setup Complete Scene — создаёт всё до запуска игры (editor-time).
/// Canvas инвентаря помещается внутрь Player.
/// InventoryManager получает все ссылки сразу.
/// </summary>
public static class InventorySceneSetup
{
[MenuItem("Tools/Setup Complete Scene")]
public static void SetupCompleteScene()
{
if (Object.FindObjectOfType<InventoryManager>() != null)
{
EditorUtility.DisplayDialog(
"Сцена уже настроена",
"InventoryManager уже существует. Удалите существующие объекты сцены перед повторной настройкой.",
"OK");
return;
}
// ── EventSystem ───────────────────────────────────────────────────────
if (Object.FindObjectOfType<EventSystem>() == null)
{
var esGO = new GameObject("EventSystem");
esGO.AddComponent<EventSystem>();
esGO.AddComponent<StandaloneInputModule>();
}
// ── InventoryEvents ───────────────────────────────────────────────────
new GameObject("InventoryEvents").AddComponent<InventoryEvents>();
// ── InventoryManager ──────────────────────────────────────────────────
var imGO = new GameObject("InventoryManager");
var im = imGO.AddComponent<InventoryManager>();
imGO.AddComponent<InventoryExampleItems>();
// ── Player ────────────────────────────────────────────────────────────
var player = CreatePlayerGO();
// ── InventoryCanvas (дочерний объект Player) ──────────────────────────
var canvasGO = new GameObject("InventoryCanvas");
canvasGO.transform.SetParent(player.transform, false);
var canvas = canvasGO.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
canvas.sortingOrder = 100;
canvasGO.AddComponent<GraphicRaycaster>();
var uiBuilder = canvasGO.AddComponent<InventoryUIBuilder>();
// Строим весь UI прямо сейчас, до запуска игры
uiBuilder.BuildInventoryUI();
// Назначаем ссылки в InventoryManager
im.slots = uiBuilder.GetInventorySlots();
im.hotbarSlots = uiBuilder.GetHotbarSlots();
im.inventoryPanel = uiBuilder.inventoryPanelRect != null ? uiBuilder.inventoryPanelRect.gameObject : null;
im.inventoryCanvas = canvas;
// Скрыть панель инвентаря по умолчанию
if (im.inventoryPanel != null)
im.inventoryPanel.SetActive(false);
// ── PlayerInput (дочерний объект Player) ──────────────────────────────
var piGO = new GameObject("PlayerInput");
piGO.transform.SetParent(player.transform, false);
var pi = piGO.AddComponent<PlayerInput>();
var inputActions = AssetDatabase.LoadAssetAtPath<UnityEngine.InputSystem.InputActionAsset>(
"Assets/Input/PlayerControls.inputactions");
if (inputActions != null)
pi.actions = inputActions;
else
Debug.LogWarning("[Setup] Назначьте Assets/Input/PlayerControls.inputactions на компонент PlayerInput вручную.");
// ── HandEquipSystem ───────────────────────────────────────────────────
player.AddComponent<HandEquipSystem>();
Selection.activeGameObject = player;
Debug.Log("[Setup] Готово! Назначьте кость руки (handBone) в HandEquipSystem на Player.");
}
// ── Вспомогательные методы ────────────────────────────────────────────────
static GameObject CreatePlayerGO()
{
var go = new GameObject("Player");
var cc = go.AddComponent<CharacterController>();
cc.height = 1.8f;
cc.radius = 0.5f;
cc.center = new Vector3(0, 0.9f, 0);
var ch = go.AddComponent<charecter>();
ch.walkSpeed = 5f;
ch.runSpeed = 9f;
ch.crouchSpeed = 2.5f;
ch.jumpHeight = 1.5f;
ch.gravity = -9.81f;
// Визуальная капсула
var body = GameObject.CreatePrimitive(PrimitiveType.Capsule);
body.name = "Body";
body.transform.SetParent(go.transform, false);
Object.DestroyImmediate(body.GetComponent<Collider>());
// Голова
var head = new GameObject("Head");
head.transform.SetParent(go.transform, false);
head.transform.localPosition = new Vector3(0, 0.8f, 0);
ch.head = head.transform;
// Камера
var camObj = new GameObject("PlayerCamera");
camObj.transform.SetParent(head.transform, false);
camObj.transform.localPosition = new Vector3(0, 0.1f, 0);
var cam = camObj.AddComponent<Camera>();
cam.tag = "MainCamera";
cam.nearClipPlane = 0.05f;
var oldCam = GameObject.FindWithTag("MainCamera");
if (oldCam != null && oldCam != camObj)
Object.DestroyImmediate(oldCam);
go.tag = "Player";
go.transform.position = new Vector3(0, 2, -5);
return go;
}
}