using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using System.IO; using System; using System.Reflection; #if UNITY_EDITOR using UnityEditor; #endif public class inventoryListManager : MonoBehaviour { public List inventoryList = new List (); public List inventoryCategoryInfoList = new List (); public GameObject emptyInventoryPrefab; public string relativePathCaptures = "Assets/Game Kit Controller/Prefabs/Inventory/Captures"; public Camera inventoryCamera; public Transform lookObjectsPosition; public GameObject inventoryCameraParentGameObject; public string relativeInventoryPath = "Assets/Game Kit Controller/Prefabs/Inventory/Usable"; public inventoryBankManager mainInventoryBankManager; [TextArea (3, 10)] public string inventoryListMessage = "IMPORTANT: THIS IS A DEBUG LIST OF INVENTORY OBJECTS, USE THE INVENTORY CATEGORY INFO LIST ABOVE TO ADD NEW ONES."; [TextArea (3, 10)] public string inventoryListUpdateMessage = "Press the Update Inventory List Button when you change the settings of any inventory object in the Inventory Category Info List"; public inventoryListManagerData mainInventoryListManagerData; public bool inventoryCaptureToolOpen; public bool removeInventoryPrefabWhenDeletingInventoryObject; public bool addNewInventoryObjectToAddThroughEditorList; public GameObject newInventoryObjectToAddThroughEditor; public List newInventoryObjectToAddThroughEditorList = new List (); public bool useNewCategoryToAddObject; public string newCategoryToAddObject; public bool updateInventoryListScriptableObjectOnCreateEditRemove = true; public fullCraftingBlueprintInfoTemplateData mainFullCraftingBlueprintInfoTemplateData; public const string mainManagerName = "Main Inventory Manager"; public static string getMainManagerName () { return mainManagerName; } private static inventoryListManager _inventoryListManagerInstance; public static inventoryListManager Instance { get { return _inventoryListManagerInstance; } } bool instanceInitialized; public void getComponentInstance () { if (instanceInitialized) { return; } if (_inventoryListManagerInstance != null && _inventoryListManagerInstance != this) { Destroy (this.gameObject); return; } _inventoryListManagerInstance = this; instanceInitialized = true; } void Awake () { getComponentInstance (); } private void Start () { foreach (var inventoryInfo in inventoryList) { inventoryInfo.InitializeAudioElements (); } } public fullCraftingBlueprintInfoTemplateData getFullCraftingBlueprintInfoTemplateData () { return mainFullCraftingBlueprintInfoTemplateData; } public List getDisassemblePiecesOfCraftingObjectByName (string objectName, ref List amountList) { if (mainFullCraftingBlueprintInfoTemplateData == null) { return null; } int dataListCount = mainFullCraftingBlueprintInfoTemplateData.craftingBlueprintInfoTemplateDataList.Count; for (int i = 0; i < dataListCount; i++) { List craftingBlueprintInfoTemplateDataList = mainFullCraftingBlueprintInfoTemplateData.craftingBlueprintInfoTemplateDataList; int craftingBlueprintInfoTemplateDataListCount = craftingBlueprintInfoTemplateDataList.Count; for (int j = 0; j < craftingBlueprintInfoTemplateDataListCount; j++) { int currentIndex = craftingBlueprintInfoTemplateDataList [j].craftingBlueprintInfoTemplateList.FindIndex (s => s.Name.Equals (objectName)); if (currentIndex > -1) { List currentIngredientObjectInfoList = craftingBlueprintInfoTemplateDataList [j].craftingBlueprintInfoTemplateList [currentIndex].disassembleIngredientObjectInfoList; List objectList = new List (); for (int k = 0; k < currentIngredientObjectInfoList.Count; k++) { GameObject currentObject = getInventoryPrefabByName (currentIngredientObjectInfoList [k].Name); if (currentObject != null) { objectList.Add (currentObject); amountList.Add (currentIngredientObjectInfoList [k].amountRequired); } } return objectList; } } } return null; } //INGAME FUNCTIONS public bool existInventoryInfoFromName (string objectName) { objectName = objectName.ToLower (); int inventoryListCount = inventoryList.Count; for (int i = 0; i < inventoryListCount; i++) { if (inventoryList [i].Name.ToLower ().Equals (objectName)) { return true; } } return false; } public inventoryInfo getInventoryInfoFromName (string objectName) { int inventoryListCount = inventoryList.Count; for (int i = 0; i < inventoryListCount; i++) { inventoryInfo currentInventoryInfo = inventoryList [i]; if (currentInventoryInfo.Name.Equals (objectName)) { return currentInventoryInfo; } } return null; } public inventoryInfo getInventoryInfoFromInventoryGameObject (GameObject objectToFind) { int inventoryListCount = inventoryList.Count; for (int i = 0; i < inventoryListCount; i++) { inventoryInfo currentInventoryInfo = inventoryList [i]; if (currentInventoryInfo.inventoryGameObject == objectToFind) { return currentInventoryInfo; } } return null; } public inventoryInfo getInventoryInfoFromCategoryListByName (string inventoryObjectName) { inventoryCategoryInfo currentInventoryCategoryInfo = null; inventoryInfo currentInventoryInfo = null; int inventoryCategoryInfoListCount = inventoryCategoryInfoList.Count; for (int i = 0; i < inventoryCategoryInfoListCount; i++) { currentInventoryCategoryInfo = inventoryCategoryInfoList [i]; int inventoryListCount = currentInventoryCategoryInfo.inventoryList.Count; for (int j = 0; j < inventoryListCount; j++) { currentInventoryInfo = currentInventoryCategoryInfo.inventoryList [j]; if (currentInventoryInfo.Name.Equals (inventoryObjectName)) { return currentInventoryInfo; } } } return null; } public int getInventoryAmountPerUnitFromInventoryGameObject (GameObject objectToFind) { int inventoryListCount = inventoryList.Count; for (int i = 0; i < inventoryListCount; i++) { inventoryInfo currentInventoryInfo = inventoryList [i]; if (currentInventoryInfo.inventoryGameObject == objectToFind) { return currentInventoryInfo.amountPerUnit; } } return -1; } public int getInventoryCategoryIndexByName (string categoryName) { int inventoryCategoryInfoListCount = inventoryCategoryInfoList.Count; for (int i = 0; i < inventoryCategoryInfoListCount; i++) { if (inventoryCategoryInfoList [i].Name.Equals (categoryName)) { return i; } } return -1; } public int getInventoryInfoIndexByName (string inventoryObjectName) { inventoryCategoryInfo currentInventoryCategoryInfo = null; inventoryInfo currentInventoryInfo = null; int inventoryCategoryInfoListCount = inventoryCategoryInfoList.Count; for (int i = 0; i < inventoryCategoryInfoListCount; i++) { currentInventoryCategoryInfo = inventoryCategoryInfoList [i]; int inventoryListCount = currentInventoryCategoryInfo.inventoryList.Count; for (int j = 0; j < inventoryListCount; j++) { currentInventoryInfo = currentInventoryCategoryInfo.inventoryList [j]; if (currentInventoryInfo.Name.Equals (inventoryObjectName)) { return j; } } } return -1; } public GameObject getInventoryPrefab (GameObject inventoryGameObject) { int inventoryListCount = inventoryList.Count; for (int i = 0; i < inventoryListCount; i++) { inventoryInfo currentInventoryInfo = inventoryList [i]; if (currentInventoryInfo.inventoryGameObject == inventoryGameObject) { return currentInventoryInfo.inventoryObjectPrefab; } } return null; } public GameObject getInventoryPrefabByName (string objectName) { int inventoryListCount = inventoryList.Count; for (int i = 0; i < inventoryListCount; i++) { inventoryInfo currentInventoryInfo = inventoryList [i]; if (currentInventoryInfo.Name.Equals (objectName)) { return currentInventoryInfo.inventoryObjectPrefab; } } return null; } public GameObject getInventoryMeshByName (string objectName) { int inventoryListCount = inventoryList.Count; for (int i = 0; i < inventoryListCount; i++) { inventoryInfo currentInventoryInfo = inventoryList [i]; if (currentInventoryInfo.Name.Equals (objectName)) { return currentInventoryInfo.inventoryGameObject; } } return null; } public inventoryObject getInventoryObjectComponentByInventoryGameObject (GameObject inventoryGameObjectToSearch) { int inventoryListCount = inventoryList.Count; for (int i = 0; i < inventoryListCount; i++) { inventoryInfo currentInventoryInfo = inventoryList [i]; if (currentInventoryInfo.inventoryGameObject == inventoryGameObjectToSearch) { return currentInventoryInfo.inventoryObjectPrefab.GetComponentInChildren (); } } return null; } public int getInventoryObjectAmountPerUnitByName (string inventoryObjectName) { int inventoryListCount = inventoryList.Count; for (int i = 0; i < inventoryListCount; i++) { inventoryInfo currentInventoryInfo = inventoryList [i]; if (currentInventoryInfo.Name.Equals (inventoryObjectName)) { return currentInventoryInfo.amountPerUnit; } } return 0; } public Texture getInventoryObjectIconByName (string inventoryObjectName) { int inventoryListCount = inventoryList.Count; for (int i = 0; i < inventoryListCount; i++) { inventoryInfo currentInventoryInfo = inventoryList [i]; if (currentInventoryInfo.Name.Equals (inventoryObjectName)) { return currentInventoryInfo.icon; } } return null; } public float getMaxDurabilityValueOnObjectByName (string objectName) { int inventoryListCount = inventoryList.Count; for (int i = 0; i < inventoryListCount; i++) { inventoryInfo currentInventoryInfo = inventoryList [i]; if (currentInventoryInfo.useDurability) { if (currentInventoryInfo.Name.Equals (objectName)) { return currentInventoryInfo.maxDurabilityAmount; } } } return -1; } //EDITOR FUNCTIONS public void setInventoryCaptureIcon (inventoryInfo info, Texture2D texture) { inventoryCategoryInfo currentInventoryCategoryInfo = null; inventoryInfo currentInventoryInfo = null; int inventoryCategoryInfoListCount = inventoryCategoryInfoList.Count; for (int i = 0; i < inventoryCategoryInfoListCount; i++) { currentInventoryCategoryInfo = inventoryCategoryInfoList [i]; for (int j = 0; j < currentInventoryCategoryInfo.inventoryList.Count; j++) { currentInventoryInfo = currentInventoryCategoryInfo.inventoryList [j]; if (currentInventoryInfo == info) { currentInventoryInfo.icon = texture; } } } updateComponent (); } public string getDataPath () { string dataPath = ""; if (!Directory.Exists (relativePathCaptures)) { Directory.CreateDirectory (relativePathCaptures); } dataPath = relativePathCaptures + "/"; return dataPath; } public void addNewInventoryObject (int categoryIndex, inventoryInfo newInventoryInfo) { inventoryCategoryInfo currentInventoryCategoryInfo = inventoryCategoryInfoList [categoryIndex]; newInventoryInfo.categoryName = currentInventoryCategoryInfo.Name; newInventoryInfo.categoryIndex = categoryIndex; newInventoryInfo.elementIndex = currentInventoryCategoryInfo.inventoryList.Count + 1; currentInventoryCategoryInfo.inventoryList.Add (newInventoryInfo); inventoryList.Add (newInventoryInfo); inventoryCaptureToolOpen = false; updateComponent (); } public void addNewInventoryObject (int categoryIndex) { inventoryCategoryInfo currentInventoryCategoryInfo = inventoryCategoryInfoList [categoryIndex]; inventoryInfo newObject = new inventoryInfo (); newObject.categoryName = currentInventoryCategoryInfo.Name; newObject.categoryIndex = categoryIndex; newObject.elementIndex = currentInventoryCategoryInfo.inventoryList.Count + 1; currentInventoryCategoryInfo.inventoryList.Add (newObject); inventoryList.Add (newObject); inventoryCaptureToolOpen = false; updateComponent (); } public void removeInventoryObjectByName (string objectToRemoveName) { inventoryCategoryInfo currentInventoryCategoryInfo = null; inventoryInfo currentInventoryInfo = null; int inventoryCategoryInfoListCount = inventoryCategoryInfoList.Count; for (int i = 0; i < inventoryCategoryInfoListCount; i++) { currentInventoryCategoryInfo = inventoryCategoryInfoList [i]; int inventoryListCount = currentInventoryCategoryInfo.inventoryList.Count; for (int j = 0; j < inventoryListCount; j++) { currentInventoryInfo = currentInventoryCategoryInfo.inventoryList [j]; if (currentInventoryInfo.Name.Equals (objectToRemoveName)) { removeInventoryObject (i, j); return; } } } } public void removeInventoryObject (int categoryIndex, int objectIndex) { inventoryCategoryInfo currentInventoryCategoryInfo = inventoryCategoryInfoList [categoryIndex]; string objectName = currentInventoryCategoryInfo.inventoryList [objectIndex].Name; currentInventoryCategoryInfo.inventoryList.RemoveAt (objectIndex); bool objectFound = false; for (int i = 0; i < inventoryList.Count; i++) { if (!objectFound && inventoryList [i].Name.Equals (objectName)) { inventoryList.RemoveAt (i); objectFound = true; } } updateSingleInventoryList (); print ("Object " + objectName + " removed from inventory list"); updateComponent (); checkIfUpdateInventoryListScriptableObjectOnCreateEditRemove (); } public void createInventoryPrafab (int categoryIndex, int index) { #if UNITY_EDITOR inventoryCategoryInfo currentInventoryCategoryInfo = inventoryCategoryInfoList [categoryIndex]; inventoryInfo currentInventoryInfo = currentInventoryCategoryInfo.inventoryList [index]; if (currentInventoryInfo.inventoryGameObject == null) { print ("Please, the Inventory Object Mesh to create the prefab"); return; } GameObject prefabToInstantiate = currentInventoryInfo.emptyInventoryPrefab; if (prefabToInstantiate == null) { prefabToInstantiate = currentInventoryCategoryInfo.emptyInventoryPrefab; } if (prefabToInstantiate == null) { prefabToInstantiate = emptyInventoryPrefab; } if (prefabToInstantiate == null) { print ("WARNING: no empty inventory prefab has been found on the category " + currentInventoryInfo.categoryName + " " + "Make sure to configure one of the empty inventory object prefabs on this category or this specific new object"); } GameObject newEmptyInventoryPrefab = Instantiate (prefabToInstantiate); inventoryObject currentInventoryObject = newEmptyInventoryPrefab.GetComponentInChildren (); currentInventoryObject.inventoryObjectInfo = new inventoryInfo (currentInventoryInfo); GameObject newInventoryObjectMesh = Instantiate (currentInventoryInfo.inventoryGameObject); newInventoryObjectMesh.transform.SetParent (newEmptyInventoryPrefab.transform); newInventoryObjectMesh.transform.localPosition = Vector3.zero; newInventoryObjectMesh.transform.localRotation = Quaternion.identity; newInventoryObjectMesh.name = newInventoryObjectMesh.name.Replace ("(Clone)", ""); string newLayer = "Default"; int newLayerIndex = LayerMask.NameToLayer (newLayer); Component [] components = newInventoryObjectMesh.GetComponentsInChildren (typeof (Transform)); foreach (Component c in components) { c.gameObject.layer = newLayerIndex; } Component [] colliders = newInventoryObjectMesh.GetComponentsInChildren (typeof (Collider)); foreach (Component c in colliders) { Type type = c.GetComponent ().GetType (); Component copy = newEmptyInventoryPrefab.AddComponent (type); BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default | BindingFlags.DeclaredOnly; PropertyInfo [] pinfos = type.GetProperties (flags); for (int i = 0; i < pinfos.Length; i++) { pinfos [i].SetValue (copy, pinfos [i].GetValue (c.GetComponent (), null), null); } } foreach (Component c in colliders) { DestroyImmediate (c.GetComponent ()); } deviceStringAction currentDeviceStringAction = newEmptyInventoryPrefab.GetComponentInChildren (); if (currentDeviceStringAction != null) { currentDeviceStringAction.setNewDeviceName (currentInventoryInfo.Name); } string relativePath = relativeInventoryPath + "/" + currentInventoryCategoryInfo.Name; if (!Directory.Exists (relativePath)) { print ("Inventory prefab folder " + currentInventoryCategoryInfo.Name + " doesn't exist, created a new one with that name"); Directory.CreateDirectory (relativePath); } pickUpObject currentPickupObject = newEmptyInventoryPrefab.GetComponent (); if (currentPickupObject != null) { currentPickupObject.amount = 1; } inventoryPrefabCreationSystem currentInventoryPrefabCreationSystem = newEmptyInventoryPrefab.GetComponent (); if (currentInventoryPrefabCreationSystem != null) { currentInventoryPrefabCreationSystem.createInventoryPrefabObject (); DestroyImmediate (currentInventoryPrefabCreationSystem); } string prefabFilePath = relativePath + "/" + currentInventoryInfo.Name + " (inventory)" + ".prefab"; bool prefabExists = false; if ((GameObject)AssetDatabase.LoadAssetAtPath (prefabFilePath, typeof (GameObject)) != null) { prefabExists = true; } if (prefabExists) { UnityEngine.Object prefab = (GameObject)AssetDatabase.LoadAssetAtPath (prefabFilePath, typeof (GameObject)); //PrefabUtility.ReplacePrefab (newEmptyInventoryPrefab, prefab, ReplacePrefabOptions.ReplaceNameBased); GKC_Utils.replacePrefabNameBased (newEmptyInventoryPrefab, prefab); print ("Replacing prefab in path " + prefabFilePath); } else { GKC_Utils.replacePrefabConnectToPrefab (newEmptyInventoryPrefab, prefabFilePath); //UnityEngine.Object prefab = PrefabUtility.CreateEmptyPrefab (prefabFilePath); //PrefabUtility.ReplacePrefab (newEmptyInventoryPrefab, prefab, ReplacePrefabOptions.ConnectToPrefab); print ("Creating new prefab in path " + prefabFilePath); } currentInventoryInfo.inventoryObjectPrefab = (GameObject)AssetDatabase.LoadAssetAtPath (prefabFilePath, typeof (GameObject)); DestroyImmediate (newEmptyInventoryPrefab); updateComponent (); updateInventoryList (); checkIfUpdateInventoryListScriptableObjectOnCreateEditRemove (); #endif } public void updateInventoryPrefab (int categoryIndex, int index) { inventoryInfo currentInventoryInfo = inventoryCategoryInfoList [categoryIndex].inventoryList [index]; updateSingleInventoryList (); print ("\n\n"); print ("UPDATING INVENTORY PREFAB"); print ("\n\n"); if (currentInventoryInfo.inventoryObjectPrefab != null) { inventoryObject currentInventoryObject = currentInventoryInfo.inventoryObjectPrefab.GetComponentInChildren (); if (currentInventoryObject != null) { bool settingNewInventoryInfoNameResult = false; if (currentInventoryInfo.Name != currentInventoryObject.inventoryObjectInfo.Name) { settingNewInventoryInfoNameResult = true; } currentInventoryObject.inventoryObjectInfo = new inventoryInfo (currentInventoryInfo); if (settingNewInventoryInfoNameResult) { print ("\n\n"); print ("Renaming the inventory object name prefab with " + currentInventoryInfo.Name); print ("\n\n"); #if UNITY_EDITOR string assetPath = AssetDatabase.GetAssetPath (currentInventoryInfo.inventoryObjectPrefab); AssetDatabase.RenameAsset (assetPath, currentInventoryInfo.Name + " (inventory)"); #endif } GKC_Utils.updateComponent (currentInventoryObject); if (settingNewInventoryInfoNameResult) { currentInventoryObject.inventoryObjectInfo.setNewName (currentInventoryInfo.Name); } print ("Inventory object " + currentInventoryInfo.Name + " updated in the list and its prefab"); pickUpObject currentpickUpObject = currentInventoryInfo.inventoryObjectPrefab.GetComponentInChildren (); if (currentpickUpObject != null) { currentpickUpObject.assignPickupElementsOnEditor (); } if (settingNewInventoryInfoNameResult) { pickupType currentPickupType = currentInventoryInfo.inventoryObjectPrefab.GetComponentInChildren (); if (currentPickupType != null) { currentPickupType.renameObject (currentInventoryInfo.Name); } } deviceStringAction currentDeviceStringAction = currentInventoryInfo.inventoryObjectPrefab.GetComponentInChildren (); if (currentDeviceStringAction != null) { currentDeviceStringAction.setNewDeviceName (currentInventoryObject.inventoryObjectInfo.Name); } } else { print ("WARNING: the inventory object called " + currentInventoryInfo.Name + " hasn't an inventory object component attached, make sure it has it assigned"); } } else { print ("WARNING: the inventory object called " + currentInventoryInfo.Name + " hasn't an inventory prefab created, make sure it is created"); } print ("\n\n"); print ("Inventory prefab updated"); print ("\n\n"); updateComponent (); GKC_Utils.refreshAssetDatabase (); } public void removeInventoryPrafab (int categoryIndex, int index) { #if UNITY_EDITOR inventoryCategoryInfo currentInventoryCategoryInfo = inventoryCategoryInfoList [categoryIndex]; inventoryInfo currentInventoryInfo = currentInventoryCategoryInfo.inventoryList [index]; if (currentInventoryInfo.inventoryObjectPrefab == null) { print ("The Inventory Object hasn't a prefab assigned"); return; } string relativePath = relativeInventoryPath + "/" + currentInventoryCategoryInfo.Name; if (Directory.Exists (relativePath)) { string prefabFilePath = relativePath + "/" + currentInventoryInfo.Name + " (inventory)" + ".prefab"; bool prefabExists = false; if ((GameObject)AssetDatabase.LoadAssetAtPath (prefabFilePath, typeof (GameObject)) != null) { prefabExists = true; } if (prefabExists) { UnityEngine.Object prefab = (GameObject)AssetDatabase.LoadAssetAtPath (prefabFilePath, typeof (GameObject)); // prefab.name = "Inventory Object Removed (Delete)"; print ("Removing prefab in path " + prefabFilePath); currentInventoryCategoryInfo.inventoryList.RemoveAt (index); updateComponent (); updateInventoryList (); if (removeInventoryPrefabWhenDeletingInventoryObject) { DestroyImmediate (prefab, true); } print ("Check the prefab called " + prefab.name + " to be removed in the Project window"); } else { print ("Prefab doesn't exists in path " + prefabFilePath); } } else { print ("Inventory prefab folder " + currentInventoryCategoryInfo.Name + " doesn't exist, created a new one with that name"); } #endif } public inventoryBankManager getMainInventoryBankManager () { return mainInventoryBankManager; } public void addNewCategory () { inventoryCategoryInfo newInventoryCategoryInfo = new inventoryCategoryInfo (); inventoryCategoryInfoList.Add (newInventoryCategoryInfo); updateComponent (); } public void removeCategory (int categoryIndex) { inventoryCategoryInfo currentInventoryCategoryInfo = inventoryCategoryInfoList [categoryIndex]; for (int i = 0; i < currentInventoryCategoryInfo.inventoryList.Count; i++) { for (int j = 0; j < inventoryList.Count; j++) { if (inventoryList [j].Name.Equals (currentInventoryCategoryInfo.inventoryList [i].Name)) { inventoryList.RemoveAt (j); j = 0; } } } inventoryCategoryInfoList.RemoveAt (categoryIndex); updateSingleInventoryList (); updateComponent (); checkIfUpdateInventoryListScriptableObjectOnCreateEditRemove (); } public void removeAllCategories () { inventoryCategoryInfoList.Clear (); inventoryList.Clear (); updateComponent (); checkIfUpdateInventoryListScriptableObjectOnCreateEditRemove (); } public void updateSingleInventoryList () { inventoryList.Clear (); inventoryCategoryInfo currentInventoryCategoryInfo = null; inventoryInfo currentInventoryInfo = null; int inventoryCategoryInfoListCount = inventoryCategoryInfoList.Count; for (int i = 0; i < inventoryCategoryInfoListCount; i++) { currentInventoryCategoryInfo = inventoryCategoryInfoList [i]; for (int j = 0; j < currentInventoryCategoryInfo.inventoryList.Count; j++) { currentInventoryInfo = currentInventoryCategoryInfo.inventoryList [j]; currentInventoryInfo.categoryIndex = i; currentInventoryInfo.elementIndex = j; inventoryList.Add (currentInventoryInfo); } } GKC_Utils.refreshAssetDatabase (); } public void updateInventoryList () { updateSingleInventoryList (); print ("\n\n"); print ("UPDATING INVENTORY LIST"); print ("\n\n"); for (int i = 0; i < inventoryList.Count; i++) { if (inventoryList [i].inventoryObjectPrefab != null) { inventoryObject currentInventoryObject = inventoryList [i].inventoryObjectPrefab.GetComponentInChildren (); if (currentInventoryObject != null) { currentInventoryObject.inventoryObjectInfo = new inventoryInfo (inventoryList [i]); GKC_Utils.updateComponent (currentInventoryObject); print ("Inventory object " + inventoryList [i].Name + " updated in the list and its prefab"); pickUpObject currentpickUpObject = inventoryList [i].inventoryObjectPrefab.GetComponentInChildren (); if (currentpickUpObject != null) { currentpickUpObject.assignPickupElementsOnEditor (); } deviceStringAction currentDeviceStringAction = inventoryList [i].inventoryObjectPrefab.GetComponentInChildren (); if (currentDeviceStringAction != null) { currentDeviceStringAction.setNewDeviceName (currentInventoryObject.inventoryObjectInfo.Name); } } else { print ("WARNING: the inventory object called " + inventoryList [i].Name + " hasn't an inventory object component attached, make sure it has it assigned"); } } else { print ("WARNING: the inventory object called " + inventoryList [i].Name + " hasn't an inventory prefab created, make sure it is created"); } } print ("\n\n"); print ("Inventory list and prefabs updated"); print ("\n\n"); updateComponent (); //AssetDatabase.SaveAssets (); //AssetDatabase.Refresh (); GKC_Utils.refreshAssetDatabase (); } public void updateCategory () { inventoryCategoryInfo currentInventoryCategoryInfo = null; inventoryInfo currentInventoryInfo = null; int inventoryCategoryInfoListCount = inventoryCategoryInfoList.Count; for (int i = 0; i < inventoryCategoryInfoListCount; i++) { currentInventoryCategoryInfo = inventoryCategoryInfoList [i]; for (int j = 0; j < currentInventoryCategoryInfo.inventoryList.Count; j++) { currentInventoryInfo = currentInventoryCategoryInfo.inventoryList [j]; currentInventoryInfo.categoryName = currentInventoryCategoryInfo.Name; currentInventoryInfo.categoryIndex = i; currentInventoryInfo.elementIndex = j; } } updateComponent (); } public void addCopyOfInventoryObject (int categoryIndex, int index) { inventoryCategoryInfo currentInventoryCategoryInfo = inventoryCategoryInfoList [categoryIndex]; inventoryInfo currentInventoryInfo = currentInventoryCategoryInfo.inventoryList [index]; inventoryInfo newInventoryInfo = new inventoryInfo (currentInventoryInfo); newInventoryInfo.Name = ""; newInventoryInfo.inventoryGameObject = null; newInventoryInfo.inventoryObjectPrefab = null; if (currentInventoryInfo.emptyInventoryPrefab != null) { newInventoryInfo.emptyInventoryPrefab = currentInventoryInfo.emptyInventoryPrefab; } currentInventoryCategoryInfo.inventoryList.Insert (index + 1, newInventoryInfo); updateComponent (); } public static GameObject spawnInventoryObject (GameObject currentInventoryObjectPrefab, Transform positionToInstantiate, int amount, inventoryInfo newInventoryInfo) { // print (currentInventoryObjectPrefab.name); GameObject inventoryObjectClone = (GameObject)Instantiate (currentInventoryObjectPrefab, positionToInstantiate.position, positionToInstantiate.rotation); inventoryObject inventoryObjectManager = inventoryObjectClone.GetComponentInChildren (); if (inventoryObjectManager != null) { inventoryObjectManager.inventoryObjectInfo = new inventoryInfo (newInventoryInfo); if (inventoryObjectManager.inventoryObjectInfo.storeTotalAmountPerUnit) { inventoryObjectManager.inventoryObjectInfo.amountPerUnit = 1; } pickUpObject currentPickupObject = inventoryObjectClone.GetComponent (); currentPickupObject.amountPerUnit = newInventoryInfo.amountPerUnit; if (currentPickupObject.amountPerUnit > 0 && !inventoryObjectManager.inventoryObjectInfo.storeTotalAmountPerUnit) { currentPickupObject.useAmountPerUnit = true; } currentPickupObject.setPickUpAmount (amount); inventoryObjectClone.name = inventoryObjectManager.inventoryObjectInfo.Name + " (inventory)"; inventoryObjectClone.GetComponentInChildren ().setNewDeviceName (inventoryObjectManager.inventoryObjectInfo.Name); return inventoryObjectClone; } return null; } public GameObject spawnInventoryObjectByName (string objectName, int amount, Vector3 newPosition, Quaternion newRotation) { int objectIndex = inventoryList.FindIndex (s => s.Name.Equals (objectName)); if (objectIndex != -1) { GameObject inventoryObjectClone = Instantiate (inventoryList [objectIndex].inventoryObjectPrefab, newPosition, newRotation); inventoryObject inventoryObjectManager = inventoryObjectClone.GetComponentInChildren (); if (inventoryObjectManager != null) { pickUpObject currentPickupObject = inventoryObjectClone.GetComponent (); // currentPickupObject.amountPerUnit = inventoryObjectManager.inventoryObjectInfo.amountPerUnit; // // if (currentPickupObject.amountPerUnit > 0 && !inventoryObjectManager.inventoryObjectInfo.storeTotalAmountPerUnit) { // currentPickupObject.useAmountPerUnit = true; // } currentPickupObject.setPickUpAmount (amount); inventoryObjectClone.name = objectName + " (inventory)"; inventoryObjectClone.GetComponentInChildren ().setNewDeviceName (objectName); return inventoryObjectClone; } } return null; } public void renameInventoryObject (string currentObjectName, string newObjectName) { inventoryInfo currentInventoryInfo = getInventoryInfoFromName (currentObjectName); if (currentInventoryInfo != null) { int categoryIndex = currentInventoryInfo.categoryIndex; int elementIndex = currentInventoryInfo.elementIndex; currentInventoryInfo = inventoryCategoryInfoList [categoryIndex].inventoryList [elementIndex]; currentInventoryInfo.setNewName (newObjectName); updateInventoryPrefab (categoryIndex, elementIndex); } } public void renameCraftingObjectByName (string currentObjectName, string newObjectName) { if (mainFullCraftingBlueprintInfoTemplateData == null) { return; } bool objectsFound = false; int dataListCount = mainFullCraftingBlueprintInfoTemplateData.craftingBlueprintInfoTemplateDataList.Count; for (int i = 0; i < dataListCount; i++) { List craftingBlueprintInfoTemplateDataList = mainFullCraftingBlueprintInfoTemplateData.craftingBlueprintInfoTemplateDataList; int craftingBlueprintInfoTemplateDataListCount = craftingBlueprintInfoTemplateDataList.Count; for (int j = 0; j < craftingBlueprintInfoTemplateDataListCount; j++) { int currentIndex = craftingBlueprintInfoTemplateDataList [j].craftingBlueprintInfoTemplateList.FindIndex (s => s.Name.Equals (currentObjectName)); if (currentIndex > -1) { craftingBlueprintInfoTemplate currentBlueprint = craftingBlueprintInfoTemplateDataList [j].craftingBlueprintInfoTemplateList [currentIndex]; currentBlueprint.Name = newObjectName; print ("trying to convert to object " + currentBlueprint.Name); UnityEngine.Object currentBlueprintObject = currentBlueprint as UnityEngine.Object; if (currentBlueprintObject != null) { print ("converted, renaming prefab"); GKC_Utils.renameAsset (currentBlueprintObject, newObjectName); } objectsFound = true; } } } if (objectsFound) { GKC_Utils.refreshAssetDatabase (); } } public void saveInventoryListToFile () { if (mainInventoryListManagerData != null) { mainInventoryListManagerData.inventoryList = new List (inventoryCategoryInfoList); print ("Inventory list saved to file"); updateComponent (); } } public void loadInventoryListFromFile () { if (mainInventoryListManagerData != null) { inventoryCategoryInfoList = new List (mainInventoryListManagerData.inventoryList); updateSingleInventoryList (); updateComponent (); print ("Inventory list loaded from file"); } } public void checkIfUpdateInventoryListScriptableObjectOnCreateEditRemove () { if (updateInventoryListScriptableObjectOnCreateEditRemove) { saveInventoryListToFile (); } } public void setInventoryCaptureToolOpenState (bool state) { inventoryCaptureToolOpen = state; Debug.Log ("Inventory Object Capture Icon window opened: " + inventoryCaptureToolOpen); updateComponent (); } public void addObjectInfoIntoInventoryList () { if (addNewInventoryObjectToAddThroughEditorList) { for (int i = 0; i < newInventoryObjectToAddThroughEditorList.Count; i++) { if (newInventoryObjectToAddThroughEditorList [i] != null) { addObjectInfoIntoInventoryListThroughEditor (newInventoryObjectToAddThroughEditorList [i]); } else { print ("WARNING: No Inventory Object Prefab was found, make sure to assign it properly"); } } newInventoryObjectToAddThroughEditorList.Clear (); } else { if (newInventoryObjectToAddThroughEditor == null) { print ("WARNING: No Inventory Object Prefab was found, make sure to assign it properly"); return; } addObjectInfoIntoInventoryListThroughEditor (newInventoryObjectToAddThroughEditor); newInventoryObjectToAddThroughEditor = null; } } void addObjectInfoIntoInventoryListThroughEditor (GameObject newObject) { inventoryObject currentInventoryObject = newObject.GetComponentInChildren (); if (currentInventoryObject != null) { inventoryInfo newInventoryObjectInfo = new inventoryInfo (currentInventoryObject.inventoryObjectInfo); int categoryIndex = -1; string categoryName = newInventoryObjectInfo.categoryName; if (useNewCategoryToAddObject && newCategoryToAddObject != "") { categoryName = newCategoryToAddObject; } categoryIndex = inventoryCategoryInfoList.FindIndex (s => s.Name.Equals (categoryName)); if (categoryIndex > -1) { inventoryCategoryInfo currentInventoryCategoryInfo = inventoryCategoryInfoList [categoryIndex]; int objectIndex = currentInventoryCategoryInfo.inventoryList.FindIndex (s => s.Name.Equals (newInventoryObjectInfo.Name)); if (objectIndex == -1) { newInventoryObjectInfo.categoryName = categoryName; newInventoryObjectInfo.elementIndex = currentInventoryCategoryInfo.inventoryList.Count + 1; newInventoryObjectInfo.inventoryObjectPrefab = newObject; currentInventoryCategoryInfo.inventoryList.Add (newInventoryObjectInfo); newObject = null; GKC_Utils.updateComponent (currentInventoryObject); updateComponent (); updateInventoryList (); checkIfUpdateInventoryListScriptableObjectOnCreateEditRemove (); print ("Adding inventory info " + newInventoryObjectInfo.Name + " from inventory object prefab to the main inventory list manager, " + "on the category " + categoryName); } else { print ("Object " + newInventoryObjectInfo.Name + " already exists on the main inventory list manager, on the category " + categoryName); } } else { print ("Category " + categoryName + " wasn't found, make sure to configure a new category with that name or use an already existing" + " category for it"); } } } public void updateComponent () { GKC_Utils.updateComponent (this); GKC_Utils.updateDirtyScene ("Updating main inventory list manager ", gameObject); } }