コード例 #1
0
        /// <summary>
        /// Creating a new Object Pool from script. Returns null, if no pool was created.
        /// </summary>
        /// <param name="PrefabToSpawn">The Prefab to spawn. Must have a JustSaveRuntimeId-component</param>
        /// <param name="PrefabId">A prefab id. Should be unique among your other object-pooling-ids</param>
        /// <param name="BasePoolSize">The base size to which the pool should be filled</param>
        /// <param name="Mode">How the pool should operate if you want to spawn something and the pool is empty.</param>
        /// <param name="NotifyToDespawn">How many objects ahead a pool object will be notified to despawn itself (JSOnNeeded is called)</param>
        /// <returns></returns>
        public ObjectPool CreateObjectPool(GameObject PrefabToSpawn, string PrefabId, int BasePoolSize, PoolingMode Mode, int NotifyToDespawn)
        {
            if (BasePoolSize < 0 || PrefabId == null || PrefabId == "")
            {
                return(null);
            }
            GameObject newPool          = new GameObject("ObjectPoolFor" + PrefabToSpawn, typeof(ObjectPool));
            ObjectPool newPoolComponent = newPool.GetComponent <ObjectPool>();

            newPoolComponent.SpawnPrefabId = PrefabId;
            JustSaveRuntimeId SavablePrefabToSpawn = PrefabToSpawn.GetComponent <JustSaveRuntimeId>();

            if (SavablePrefabToSpawn == null)
            {
                return(null);
            }
            newPoolComponent.SpawnPrefab  = SavablePrefabToSpawn;
            newPoolComponent.BasePoolSize = BasePoolSize;
            newPoolComponent.SetPoolingMode(Mode);
            newPoolComponent.NotifyToDespawn = NotifyToDespawn <= BasePoolSize ? NotifyToDespawn : BasePoolSize;
            myObjectPoolingManager.RegisterObjectPool(PrefabId, newPoolComponent);
            return(newPoolComponent);
        }
コード例 #2
0
        /// <summary>
        /// Interprets a given save and applies the data to the Application
        /// </summary>
        /// <param name="source">The save to interpret</param>
        /// <returns>True if the method reached the end of the calculation without fatal errors</returns>
        public bool InterpretSave(Save source)
        {
            List <int> arrayList = new List <int>();
            //getting references to spawning and autosaves
            JSDictionary <JSSerializable> Runtime = source.Runtime;
            JSDictionary <JSSerializable> Static  = source.Static;

            //use this dictionary to remember once scanned classes for later use
            Dictionary <Type, FieldInfo[]> RememberedFields = new Dictionary <Type, FieldInfo[]>();

            //preparing references and despawning runtime objects
            JustSaveId[]           JSManagedObjects      = UnityEngine.Object.FindObjectsOfType <JustSaveId>();
            List <JustSaveSceneId> JSManagedSceneObjects = new List <JustSaveSceneId>();

            foreach (JustSaveId JustSaveManagedObject in JSManagedObjects)
            {
                if (JustSaveManagedObject is JustSaveRuntimeId)
                {
                    ((JustSaveRuntimeId)JustSaveManagedObject).Despawn(); //despawns every runtime object
                }
                else
                {
                    JSManagedSceneObjects.Add((JustSaveSceneId)JustSaveManagedObject); //saves every scene object reference
                }
            }
            if (Dbug.Is(DebugMode.DEBUG))
            {
                Debug.Log("Save Interpreter despawned old runtime objects");
            }

            //loading scene objects
            foreach (JustSaveSceneId IdObj in JSManagedSceneObjects)
            {
                SyncObject(source, false, IdObj, RememberedFields);
            }

            if (Dbug.Is(DebugMode.DEBUG))
            {
                Debug.Log("Save Interpreter synced scene objects");
            }


            //iterating through spawned objects
            string[] idInfo = new string[2];
            foreach (Tuple <String, JSSerializable> RuntimeObjectInfo in Runtime.GetValuePairs())
            {
                idInfo = RuntimeObjectInfo.Item1.Split('_');
                GameObject        spawnedPrefab = JustSaveManager.Instance.Spawn(idInfo[0], Vector3.zero);
                JustSaveRuntimeId spawnedIdObj  = spawnedPrefab.GetComponent <JustSaveRuntimeId>();
                spawnedIdObj.SetId(Guid.Parse(idInfo[1]));
                SyncObject(source, true, spawnedPrefab.GetComponent <JustSaveRuntimeId>(), RememberedFields);
            }


            if (Dbug.Is(DebugMode.DEBUG))
            {
                Debug.Log("Save Interpreter spawned and synced runtime objects");
                Debug.Log("Save Interpreter finished loading Save");
            }

            return(true);
        }