Basic saving, currently hardcoded to the one "chest" inventory in HomeBase.
Loads when scene name contains "Homebase" and saves when button pressed.
This commit is contained in:
@@ -3,7 +3,6 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using Unity.Mathematics;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.Interaction.Toolkit;
|
||||
|
||||
@@ -13,15 +12,13 @@ public class InventoryItem : MonoBehaviour
|
||||
|
||||
public GameObject itemPrefab;
|
||||
|
||||
private int _itemId;
|
||||
|
||||
private GameObject _childPrefab;
|
||||
|
||||
public int Count { get; private set; }
|
||||
private TMP_Text itemCounterText;
|
||||
|
||||
public TMP_Text itemCounterText;
|
||||
private ItemData itemData;
|
||||
|
||||
private ItemData _itemData;
|
||||
public ItemSaveData saveData;
|
||||
|
||||
|
||||
private void OnEnable()
|
||||
@@ -37,13 +34,15 @@ public class InventoryItem : MonoBehaviour
|
||||
|
||||
if (itemCounterText == null) itemCounterText = GetComponentInChildren<TMP_Text>();
|
||||
|
||||
_childPrefab = GetComponentInChildren<ItemData>()?.gameObject;
|
||||
_childPrefab = GetComponentInChildren<ItemData>()?.prefab.gameObject;
|
||||
//Only attaches the prefab if it doesn't already have one ([ExecuteAlways] causes one to be spawned in editor).
|
||||
if (_childPrefab != null) return;
|
||||
|
||||
_childPrefab = Instantiate(itemPrefab, transform);
|
||||
_childPrefab.transform.localPosition = new Vector3(0, -0.45f, 0);
|
||||
_childPrefab.transform.localScale = new Vector3(2, 2, 2);
|
||||
|
||||
//todo switch to multiply instead so items with non 1,1,1 scale will retain their proportions
|
||||
_childPrefab.transform.localPosition = GetComponentInChildren<ItemData>().positionInContainer;
|
||||
_childPrefab.transform.localScale = GetComponentInChildren<ItemData>().scaleInContainer;
|
||||
_childPrefab.transform.rotation = quaternion.identity;
|
||||
|
||||
//Disable prefab components that arent relevant.
|
||||
@@ -59,24 +58,29 @@ public class InventoryItem : MonoBehaviour
|
||||
|
||||
var xrGrabInteractableComponent = _childPrefab.GetComponent<XRGrabInteractable>();
|
||||
if (xrGrabInteractableComponent != null) xrGrabInteractableComponent.enabled = false;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_itemData = _childPrefab.GetComponent<ItemData>();
|
||||
itemData = _childPrefab.GetComponent<ItemData>();
|
||||
|
||||
if (_itemData == null) Debug.LogError("Item prefab has no attached ItemData", _childPrefab);
|
||||
|
||||
|
||||
_itemId = _itemData.itemId;
|
||||
if (Count == 0) Count = 1;
|
||||
if (itemData == null) Debug.LogError("Item prefab has no attached ItemData", _childPrefab);
|
||||
|
||||
Debug.Log(saveData);
|
||||
if (string.IsNullOrEmpty(saveData.assetPath))
|
||||
{
|
||||
saveData.assetPath = itemData.assetLocation;
|
||||
saveData.count = saveData.count == 0 ? 1 : saveData.count;
|
||||
}
|
||||
UpdateText();
|
||||
}
|
||||
|
||||
private void UpdateText()
|
||||
{
|
||||
itemCounterText.text = _itemData.canStack ? Count.ToString() : "";
|
||||
itemCounterText.text = itemData.canStack ? saveData.count.ToString() : "";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,11 +89,11 @@ public class InventoryItem : MonoBehaviour
|
||||
*/
|
||||
public bool ChangeCount(int amount)
|
||||
{
|
||||
if (Count + amount < 0) return false;
|
||||
if (saveData.count + amount < 0) return false;
|
||||
|
||||
Count += amount;
|
||||
saveData.count += amount;
|
||||
|
||||
if (Count == 0)
|
||||
if (saveData.count == 0)
|
||||
{
|
||||
//Destroys this gameobject if the count is 0 or lower
|
||||
Destroy(gameObject);
|
||||
@@ -103,10 +107,11 @@ public class InventoryItem : MonoBehaviour
|
||||
|
||||
public void PopItem()
|
||||
{
|
||||
GameObject item = Instantiate(_itemData.prefab, transform.position + transform.forward, Quaternion.identity);
|
||||
GameObject item = Instantiate(itemData.prefab, transform.position + transform.forward, Quaternion.identity);
|
||||
|
||||
|
||||
item.transform.localScale = Vector3.one;
|
||||
item.transform.name = _itemData.name;
|
||||
item.transform.name = itemData.itemName;
|
||||
|
||||
var colliderComponent = item.GetComponent<Collider>();
|
||||
if (colliderComponent != null) colliderComponent.enabled = true;
|
||||
@@ -127,13 +132,13 @@ public class InventoryItem : MonoBehaviour
|
||||
|
||||
public int GetItemid()
|
||||
{
|
||||
return _itemId;
|
||||
return itemData.itemId;;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{base.ToString()}, Count: {Count}, ItemData: {_itemData}";
|
||||
return $"{base.ToString()}, Count: {saveData.count}, ItemData: {itemData}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[Serializable]
|
||||
public class InventorySaveData
|
||||
{
|
||||
public List<ItemSaveData> inventoryContents;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e60ec35305ed0b94099d18a09b488310
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,16 +1,44 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
|
||||
[Serializable]
|
||||
public class ItemData : MonoBehaviour
|
||||
{
|
||||
[Tooltip("Location is populated automatically")]
|
||||
public string assetLocation;
|
||||
public int itemId;
|
||||
public bool canStack;
|
||||
public string itemName;
|
||||
public string itemDescription;
|
||||
public GameObject prefab;
|
||||
public Vector3 scaleInContainer;
|
||||
public Vector3 positionInContainer;
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(prefab))) assetLocation = AssetDatabase.GetAssetPath(prefab);
|
||||
|
||||
AdjustPath();
|
||||
}
|
||||
|
||||
private void AdjustPath()
|
||||
{
|
||||
//trims the location for use with Resources.Load later
|
||||
if (assetLocation.StartsWith("Assets/Resources/"))
|
||||
assetLocation = assetLocation[17 .. ^7];
|
||||
else if (!string.IsNullOrEmpty(assetLocation))
|
||||
if (assetLocation.StartsWith("Assets"))
|
||||
{
|
||||
Debug.LogError(itemName +
|
||||
": item with this script should be placed in the Assets/Resources/ folder. Currently: " +
|
||||
assetLocation);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
|
||||
12
Assets/Project Files/Scripts/Helar/Items/ItemSaveData.cs
Normal file
12
Assets/Project Files/Scripts/Helar/Items/ItemSaveData.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[Serializable]
|
||||
public class ItemSaveData
|
||||
{
|
||||
public string assetPath;
|
||||
public int count;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2e553150cec08514e9aacc82fe8da007
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user