test
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user