Esempio n. 1
0
        public override object Read <T>(ES3Reader reader)
        {
            var prefabId = reader.ReadRefProperty();

            if (ES3ReferenceMgrBase.Current == null)
            {
                return(null);
            }

            var es3Prefab = ES3ReferenceMgrBase.Current.GetPrefab(prefabId);

            if (es3Prefab == null)
            {
                throw new MissingReferenceException("Prefab with ID " + prefabId + " could not be found.\nPress the 'Refresh References' button on the ES3ReferenceMgr Component of the Easy Save 3 Manager in the scene to refresh prefabs.");
            }


#if UNITY_EDITOR
            // Use PrefabUtility.InstantiatePrefab if we're saving in the Editor and the application isn't playing.
            // This keeps the connection to the prefab, which is useful for Editor scripts saving data outside of runtime.
            var instance = Application.isPlaying ? GameObject.Instantiate(es3Prefab.gameObject) : PrefabUtility.InstantiatePrefab(es3Prefab.gameObject);
#else
            var instance = GameObject.Instantiate(es3Prefab.gameObject);
#endif
            var instanceES3Prefab = ((GameObject)instance).GetComponent <ES3Prefab>();
            if (instanceES3Prefab == null)
            {
                throw new MissingReferenceException("Prefab with ID " + prefabId + " was found, but it does not have an ES3Prefab component attached.");
            }

            ReadInto <T>(reader, instance);

            return(instanceES3Prefab.gameObject);
        }
Esempio n. 2
0
        public override object Read <T>(ES3Reader reader)
        {
            var prefabId = reader.ReadRefProperty();
            // Load as ES3Refs and convert to longs.
            var localToGlobal_refs = reader.ReadProperty <Dictionary <ES3Ref, ES3Ref> >();
            var localToGlobal      = new Dictionary <long, long>();

            foreach (var kvp in localToGlobal_refs)
            {
                localToGlobal.Add(kvp.Key.id, kvp.Value.id);
            }

            if (ES3ReferenceMgrBase.Current == null)
            {
                return(null);
            }

            var es3Prefab = ES3ReferenceMgrBase.Current.GetPrefab(prefabId);

            if (es3Prefab == null)
            {
                throw new MissingReferenceException("Prefab with ID " + prefabId + " could not be found.");
            }
            var instance          = GameObject.Instantiate(es3Prefab.gameObject);
            var instanceES3Prefab = ((GameObject)instance).GetComponent <ES3Prefab>();

            if (instanceES3Prefab == null)
            {
                throw new MissingReferenceException("Prefab with ID " + prefabId + " was found, but it does not have an ES3Prefab component attached.");
            }

            instanceES3Prefab.ApplyReferences(localToGlobal);

            return(instanceES3Prefab.gameObject);
        }
Esempio n. 3
0
        public override object Read <T>(ES3Reader reader)
        {
            var prefabId = reader.ReadRefProperty();
            // Load as ES3Refs and convert to longs.
            var localToGlobal_refs = reader.ReadProperty <Dictionary <ES3Ref, ES3Ref> >();
            var localToGlobal      = new Dictionary <long, long>();

            foreach (var kvp in localToGlobal_refs)
            {
                localToGlobal.Add(kvp.Key.id, kvp.Value.id);
            }

            if (ES3ReferenceMgrBase.Current == null)
            {
                return(null);
            }

            var es3Prefab = ES3ReferenceMgrBase.Current.GetPrefab(prefabId);

            if (es3Prefab == null)
            {
                throw new MissingReferenceException("Prefab with ID " + prefabId + " could not be found.\nPress the 'Refresh References' button on the ES3ReferenceMgr Component of the Easy Save 3 Manager in the scene to refresh prefabs.");
            }


#if UNITY_EDITOR
            // Use PrefabUtility.InstantiatePrefab if we're saving in the Editor and the application isn't playing.
            // This keeps the connection to the prefab, which is useful for Editor scripts saving data outside of runtime.
            var instance = Application.isPlaying ? GameObject.Instantiate(es3Prefab.gameObject) : PrefabUtility.InstantiatePrefab(es3Prefab.gameObject);
#else
            var instance = GameObject.Instantiate(es3Prefab.gameObject);
#endif
            var instanceES3Prefab = ((GameObject)instance).GetComponent <ES3Prefab>();
            if (instanceES3Prefab == null)
            {
                throw new MissingReferenceException("Prefab with ID " + prefabId + " was found, but it does not have an ES3Prefab component attached.");
            }

            instanceES3Prefab.ApplyReferences(localToGlobal);

            return(instanceES3Prefab.gameObject);
        }
        protected override object ReadObject <T>(ES3Reader reader)
        {
            UnityEngine.Object obj = null;
            var  refMgr            = ES3ReferenceMgrBase.Current;
            long id = 0;

            // Read the intial properties regarding the instance we're loading.
            while (true)
            {
                if (refMgr == null)
                {
                    throw new InvalidOperationException("An Easy Save 3 Manager is required to load references. To add one to your scene, exit playmode and go to Assets > Easy Save 3 > Add Manager to Scene");
                }

                var propertyName = ReadPropertyName(reader);

                if (propertyName == ES3Type.typeFieldName)
                {
                    return(ES3TypeMgr.GetOrCreateES3Type(reader.ReadType()).Read <T>(reader));
                }
                else if (propertyName == ES3ReferenceMgrBase.referencePropertyName)
                {
                    id  = reader.Read_ref();
                    obj = refMgr.Get(id, true);
                }
                else if (propertyName == transformPropertyName)
                {
                    // Now load the Transform's ID and assign it to the Transform of our object.
                    long transformID = reader.Read_ref();
                    if (obj == null)
                    {
                        obj = CreateNewGameObject(refMgr, id);
                    }
                    refMgr.Add(((GameObject)obj).transform, transformID);
                }
                else if (propertyName == prefabPropertyName)
                {
                    if (obj != null || ES3ReferenceMgrBase.Current == null)
                    {
                        reader.ReadRefProperty();          // Skip the ref as we don't need it.
                        reader.ReadInto <GameObject>(obj); // ReadInto to apply the prefab references.
                    }
                    else
                    {
                        obj = reader.Read <GameObject>(ES3Type_ES3PrefabInternal.Instance);
                        ES3ReferenceMgrBase.Current.Add(obj, id);
                    }
                }
                else if (propertyName == null)
                {
                    if (obj == null)
                    {
                        return(CreateNewGameObject(refMgr, id));
                    }
                    return(obj);
                }
                else
                {
                    reader.overridePropertiesName = propertyName;
                    break;
                }
            }

            if (obj == null)
            {
                obj = CreateNewGameObject(refMgr, id);
            }

            ReadInto <T>(reader, obj);
            return(obj);
        }