test
This commit is contained in:
@@ -181,6 +181,19 @@ public class InventoryItemFactory : MonoBehaviour
|
||||
rb.linearVelocity = velocity.Value + (Vector3.up * (config?.dropForce ?? 2f));
|
||||
}
|
||||
|
||||
// Если у ItemData нет связанной модели — создаём простой визуал (куб)
|
||||
if (itemData != null && itemData.modelPrefab == null && worldItem != null)
|
||||
{
|
||||
var visual = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
||||
visual.name = "Visual";
|
||||
visual.transform.SetParent(worldItem.transform, false);
|
||||
visual.transform.localPosition = Vector3.zero;
|
||||
visual.transform.localScale = Vector3.one * 0.3f;
|
||||
// Удаляем лишний коллайдер на визуале
|
||||
var col = visual.GetComponent<Collider>();
|
||||
if (col != null) GameObject.Destroy(col);
|
||||
}
|
||||
|
||||
return worldItem;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Events;
|
||||
using System.IO;
|
||||
|
||||
public class InventoryManager : MonoBehaviour
|
||||
{
|
||||
@@ -108,37 +109,22 @@ public class InventoryManager : MonoBehaviour
|
||||
var eventsGO = new GameObject("InventoryEvents");
|
||||
eventsGO.AddComponent<InventoryEvents>();
|
||||
}
|
||||
|
||||
// Попробуем загрузить сохранённый инвентарь
|
||||
LoadFromFile();
|
||||
|
||||
// Ensure PlayerInput exists and subscribe to input events
|
||||
if (PlayerInput.Instance == null)
|
||||
{
|
||||
var go = new GameObject("PlayerInput");
|
||||
go.AddComponent<PlayerInput>();
|
||||
}
|
||||
|
||||
PlayerInput.Instance.OnToggleInventory += ToggleInventory;
|
||||
PlayerInput.Instance.OnDropPressed += DropSelectedItem;
|
||||
PlayerInput.Instance.OnHotbarSelect += SelectHotbarSlot;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Toggle инвентаря на Tab
|
||||
if (Input.GetKeyDown(KeyCode.Tab))
|
||||
{
|
||||
ToggleInventory();
|
||||
}
|
||||
|
||||
// Закрыть на Escape
|
||||
if (Input.GetKeyDown(KeyCode.Escape) && inventoryPanel != null && inventoryPanel.activeSelf)
|
||||
{
|
||||
ToggleInventory();
|
||||
}
|
||||
|
||||
// Горячие клавиши 1-9 для хотбара
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Alpha1 + i))
|
||||
{
|
||||
SelectHotbarSlot(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Выброс предмета на Q
|
||||
if (Input.GetKeyDown(KeyCode.Q))
|
||||
{
|
||||
DropSelectedItem();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void ToggleInventory()
|
||||
{
|
||||
@@ -151,11 +137,15 @@ public class InventoryManager : MonoBehaviour
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
InventoryEvents.Instance?.OnInventoryOpened?.Invoke();
|
||||
if (PlayerInput.Instance != null)
|
||||
PlayerInput.Instance.Enabled = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
InventoryEvents.Instance?.OnInventoryClosed?.Invoke();
|
||||
if (PlayerInput.Instance != null)
|
||||
PlayerInput.Instance.Enabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -568,6 +558,49 @@ public class InventoryManager : MonoBehaviour
|
||||
return JsonUtility.ToJson(inventoryData, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Сохранить инвентарь в файл (Application.persistentDataPath)
|
||||
/// </summary>
|
||||
public void SaveToFile()
|
||||
{
|
||||
try
|
||||
{
|
||||
string json = ExportToJson();
|
||||
string path = Path.Combine(Application.persistentDataPath, "inventory_save.json");
|
||||
File.WriteAllText(path, json);
|
||||
Debug.Log($"[InventoryManager] Saved inventory to: {path}");
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Debug.LogError($"[InventoryManager] Failed to save inventory: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Загрузить инвентарь из файла (если есть)
|
||||
/// </summary>
|
||||
public void LoadFromFile()
|
||||
{
|
||||
try
|
||||
{
|
||||
string path = Path.Combine(Application.persistentDataPath, "inventory_save.json");
|
||||
if (File.Exists(path))
|
||||
{
|
||||
string json = File.ReadAllText(path);
|
||||
ImportFromJson(json);
|
||||
Debug.Log($"[InventoryManager] Loaded inventory from: {path}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("[InventoryManager] No save file found.");
|
||||
}
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Debug.LogError($"[InventoryManager] Failed to load inventory: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
public void ImportFromJson(string json)
|
||||
{
|
||||
var inventoryData = JsonUtility.FromJson<InventorySaveData>(json);
|
||||
@@ -598,4 +631,9 @@ public class InventoryManager : MonoBehaviour
|
||||
public string itemId;
|
||||
public int amount;
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
SaveToFile();
|
||||
}
|
||||
}
|
||||
@@ -90,7 +90,6 @@ public class InventorySceneSetup
|
||||
|
||||
// Компонент предмета
|
||||
var worldItem = itemGO.AddComponent<WorldItem>();
|
||||
worldItem.pickupDelay = 0f; // мгновенный подбор
|
||||
worldItem.useFactory = false;
|
||||
|
||||
// Тестовые данные
|
||||
@@ -101,8 +100,8 @@ public class InventorySceneSetup
|
||||
testItem.type = ItemType.Material;
|
||||
testItem.maxStack = 64;
|
||||
|
||||
worldItem.itemData = testItem;
|
||||
worldItem.amount = 5;
|
||||
// Используем Setup чтобы инициализировать визуал и включить мгновенный подбор
|
||||
worldItem.Setup(testItem, 5);
|
||||
|
||||
// Коллайдер для подбора (trigger для обычных коллайдеров)
|
||||
var box = itemGO.AddComponent<BoxCollider>();
|
||||
@@ -141,6 +140,15 @@ public class InventorySceneSetup
|
||||
canvas.sortingOrder = 100;
|
||||
canvasGO.AddComponent<GraphicRaycaster>();
|
||||
|
||||
// Ensure EventSystem exists for UI interaction
|
||||
var existingES = GameObject.FindObjectOfType<UnityEngine.EventSystems.EventSystem>();
|
||||
if (existingES == null)
|
||||
{
|
||||
var es = new GameObject("EventSystem");
|
||||
es.AddComponent<UnityEngine.EventSystems.EventSystem>();
|
||||
es.AddComponent<UnityEngine.EventSystems.StandaloneInputModule>();
|
||||
}
|
||||
|
||||
// Создаём панель инвентаря
|
||||
var inventoryPanel = CreatePanel(canvasGO.transform, "InventoryPanel", new Color(0.15f, 0.15f, 0.15f, 0.95f));
|
||||
var inventoryRect = inventoryPanel.GetComponent<RectTransform>();
|
||||
|
||||
Reference in New Issue
Block a user