Ejemplo n.º 1
0
        private static void LoadUI(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return;
            }

            List <object> list = MiniJSON.Deserialize(json) as List <object>;

            for (int i = 0; i < list.Count; i++)
            {
                Dictionary <string, object> mData = list[i] as Dictionary <string, object>;
                string        prefab       = (string)mData["Prefab"];
                List <object> positionData = mData["Position"] as List <object>;
                List <object> rotationData = mData["Rotation"] as List <object>;
                string        type         = (string)mData["Type"];

                Vector3        position       = new Vector3(System.Convert.ToSingle(positionData[0]), System.Convert.ToSingle(positionData[1]), System.Convert.ToSingle(positionData[2]));
                Quaternion     rotation       = Quaternion.Euler(new Vector3(System.Convert.ToSingle(rotationData[0]), System.Convert.ToSingle(rotationData[1]), System.Convert.ToSingle(rotationData[2])));
                ItemCollection itemCollection = null;
                if (type == "UI")
                {
                    UIWidget container = WidgetUtility.Find <UIWidget>(prefab);
                    if (container != null)
                    {
                        itemCollection = container.GetComponent <ItemCollection>();
                    }
                }
                if (itemCollection != null)
                {
                    itemCollection.SetObjectData(mData);
                }
            }
            if (InventoryManager.DefaultSettings.debugMessages)
            {
                Debug.Log("[Inventory System] UI Loaded: " + json);
            }
        }
Ejemplo n.º 2
0
        private static void LoadScene(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return;
            }

            ItemCollection[] itemCollections = FindObjectsOfType <ItemCollection>().Where(x => x.saveable).ToArray();
            for (int i = 0; i < itemCollections.Length; i++)
            {
                ItemCollection collection = itemCollections[i];

                //Dont destroy ui game objects
                if (collection.GetComponent <ItemContainer>() != null)
                {
                    continue;
                }

                GameObject prefabForCollection = InventoryManager.GetPrefab(collection.name);

                //Store real prefab to cache
                if (prefabForCollection == null)
                {
                    collection.transform.parent = InventoryManager.current.transform;
                    InventoryManager.m_PrefabCache.Add(collection.name, collection.gameObject);
                    collection.gameObject.SetActive(false);
                    continue;
                }

                Destroy(collection.gameObject);
            }

            List <object> list = MiniJSON.Deserialize(json) as List <object>;

            for (int i = 0; i < list.Count; i++)
            {
                Dictionary <string, object> mData = list[i] as Dictionary <string, object>;
                string        prefab       = (string)mData["Prefab"];
                List <object> positionData = mData["Position"] as List <object>;
                List <object> rotationData = mData["Rotation"] as List <object>;

                Vector3    position = new Vector3(System.Convert.ToSingle(positionData[0]), System.Convert.ToSingle(positionData[1]), System.Convert.ToSingle(positionData[2]));
                Quaternion rotation = Quaternion.Euler(new Vector3(System.Convert.ToSingle(rotationData[0]), System.Convert.ToSingle(rotationData[1]), System.Convert.ToSingle(rotationData[2])));

                GameObject collectionGameObject = CreateCollection(prefab, position, rotation);
                if (collectionGameObject != null)
                {
                    IGenerator[] generators = collectionGameObject.GetComponents <IGenerator>();
                    for (int j = 0; j < generators.Length; j++)
                    {
                        generators[j].enabled = false;
                    }
                    ItemCollection itemCollection = collectionGameObject.GetComponent <ItemCollection>();
                    itemCollection.SetObjectData(mData);
                }
            }

            if (InventoryManager.DefaultSettings.debugMessages)
            {
                Debug.Log("[Inventory System] Scene Loaded: " + json);
            }
        }
Ejemplo n.º 3
0
        public static void Load(string key, bool includePersistent = true)
        {
            key += "InventorySystem";
            key += " [" + UnityEngine.SceneManagement.SceneManager.GetActiveScene().name + "]";
            string data = PlayerPrefs.GetString(key);

            if (string.IsNullOrEmpty(data))
            {
                return;
            }

            ItemCollection[] itemCollections = FindObjectsOfType <ItemCollection>();
            for (int i = 0; i < itemCollections.Length; i++)
            {
                if (InventoryManager.GetPrefab(itemCollections[i].name.Replace("(Clone)", "")) == null)
                {
                    continue;
                }
                Destroy(itemCollections[i].gameObject);
            }

            List <object> list = MiniJSON.Deserialize(data) as List <object>;

            for (int i = 0; i < list.Count; i++)
            {
                Dictionary <string, object> mData = list[i] as Dictionary <string, object>;
                string        prefab       = (string)mData["Prefab"];
                List <object> positionData = mData["Position"] as List <object>;
                List <object> rotationData = mData["Rotation"] as List <object>;
                string        type         = (string)mData["Type"];

                Vector3        position       = new Vector3(System.Convert.ToSingle(positionData[0]), System.Convert.ToSingle(positionData[1]), System.Convert.ToSingle(positionData[2]));
                Quaternion     rotation       = Quaternion.Euler(new Vector3(System.Convert.ToSingle(rotationData[0]), System.Convert.ToSingle(rotationData[1]), System.Convert.ToSingle(rotationData[2])));
                ItemCollection itemCollection = null;
                if (type == "UI")
                {
                    UIWidget container = WidgetUtility.Find <UIWidget>(prefab);
                    if (container != null && (includePersistent || container.gameObject.scene == UnityEngine.SceneManagement.SceneManager.GetActiveScene()))
                    {
                        itemCollection = container.GetComponent <ItemCollection>();
                    }
                }
                else
                {
                    GameObject collectionGameObject = CreateCollection(prefab, position, rotation);
                    if (collectionGameObject != null)
                    {
                        IGenerator[] generators = collectionGameObject.GetComponents <IGenerator>();
                        for (int j = 0; j < generators.Length; j++)
                        {
                            generators[j].enabled = false;
                        }
                        itemCollection = collectionGameObject.GetComponent <ItemCollection>();
                    }
                }

                if (itemCollection != null)
                {
                    itemCollection.SetObjectData(mData);
                }
            }

            if (InventoryManager.current != null && InventoryManager.current.onSceneLoaded != null)
            {
                InventoryManager.current.onSceneLoaded.Invoke();
            }

            if (InventoryManager.DefaultSettings.debugMessages)
            {
                Debug.Log("[Inventory System] Data loaded: " + data);
            }
        }