Пример #1
0
        void OnGUI()
        {
            GUILayout.Label("Search entity by id");
            GUILayout.BeginHorizontal();
            id_search = EditorGUILayout.IntField(id_search);
            if (GUILayout.Button("Paste"))
            {
                id_search = Convert.ToInt16(GUIUtility.systemCopyBuffer);
            }
            if (GUILayout.Button("Find"))
            {
                var pref = SaveEntityDatabase.GetPrefab(id_search);
                Selection.activeObject = pref;
            }
            GUILayout.EndHorizontal();


            GUILayout.Label("Search entities in scene by id");
            GUILayout.BeginHorizontal();
            scene_search = EditorGUILayout.IntField(scene_search);
            if (GUILayout.Button("Paste"))
            {
                scene_search = Convert.ToInt16(GUIUtility.systemCopyBuffer);
            }
            if (GUILayout.Button("Find"))
            {
                List <GameObject> selection = new List <GameObject>();
                var entities = GameObject.FindObjectsOfType <SaveEntity>();
                foreach (var e in entities)
                {
                    if (e.entityID == scene_search)
                    {
                        selection.Add(e.gameObject);
                    }
                }
                Selection.objects = selection.ToArray();
            }
            GUILayout.EndHorizontal();


            GUILayout.Label("Regex search by name");
            GUILayout.BeginHorizontal();
            name_search = GUILayout.TextArea(name_search);
            if (GUILayout.Button("Paste"))
            {
                name_search = GUIUtility.systemCopyBuffer;
            }
            if (GUILayout.Button("Find"))
            {
                List <GameObject> selection = new List <GameObject>();
                var entities = Resources.LoadAll <SaveEntity>(SaveSystemSettings.Current.EntitiesFolder + "/");

                var names = entities.Select(x => x.name).ToArray();

                for (int i = 0; i < names.Length; i++)
                {
                    if (System.Text.RegularExpressions.Regex.IsMatch(names[i], name_search, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                    {
                        selection.Add(entities[i].gameObject);
                    }
                }
                Selection.objects = selection.ToArray();
            }
            GUILayout.EndHorizontal();
        }
Пример #2
0
        public static void UnboxSaveObject(SaveObject save, Transform root)
        {
            if (save == null)
            {
                Debug.LogError("Save object is null");
                return;
            }

            var initializedfield = typeof(SavedComponent).GetField("m_initialized", BindingFlags.NonPublic | BindingFlags.Instance);

            Dictionary <int, SaveEntity> bp_entity = null;
            Dictionary <int, Dictionary <int, SavedComponent> > bp_parent_component = null;
            Dictionary <int, Dictionary <int, SavedComponent> > bp_all_comp         = new Dictionary <int, Dictionary <int, SavedComponent> >();

            if (save.isBlueprint)
            {
                bp_entity           = new Dictionary <int, SaveEntity>();
                bp_parent_component = new Dictionary <int, Dictionary <int, SavedComponent> >();
                bp_all_comp         = new Dictionary <int, Dictionary <int, SavedComponent> >();
            }

            bool blueprintEditorMode = save.isBlueprint && !Application.isPlaying;

            Dictionary <int, ComponentObject> cobjects = null;
            Dictionary <int, Dictionary <int, SavedComponent> > allComps = new Dictionary <int, Dictionary <int, SavedComponent> >();
            Dictionary <int, SaveEntity>          allEntities            = new Dictionary <int, SaveEntity>();
            Dictionary <EntityObject, SaveEntity> toParent = new Dictionary <EntityObject, SaveEntity>();
            List <SavedComponent> allComponents            = new List <SavedComponent>();

            foreach (var eobj in save.entities)
            {
                var prefab = SaveEntityDatabase.GetPrefab(eobj.database_ID);

                if (!prefab)
                {
                    Debug.LogError("When loading, database entity: " + eobj.database_ID + " was not found, this probably means you saved an entity that is not registered in database. Make it a prefab in Entity folder and run database scan.");
                    continue;
                }
                bool prefabState = prefab.activeSelf;
                prefab.SetActive(false);
                GameObject gameobj = null;
#if UNITY_EDITOR
                if (blueprintEditorMode)
                {
                    gameobj = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
                }
                else
                {
                    gameobj = GameObject.Instantiate(prefab);
                }
#else
                gameobj = GameObject.Instantiate(prefab);
#endif
                gameobj.name = eobj.gameObjectName;
                var tr = gameobj.transform;

                GameObject parentGo = null;
                if (eobj.parentName != "null")
                {
                    parentGo = GameObject.Find(eobj.parentName);
                }

                if (save.isBlueprint)
                {
                    tr.parent        = root;
                    tr.localPosition = eobj.position;
                    tr.localRotation = Quaternion.Euler(eobj.rotation);
                }
                else
                {
                    tr.position = eobj.position;
                    tr.rotation = Quaternion.Euler(eobj.rotation);
                    tr.parent   = eobj.parentName == "null" ? root : parentGo == null ? root : parentGo.transform;
                }

                var entity = gameobj.GetComponent <SaveEntity>();
                Dictionary <int, SavedComponent> ecomps  = null;
                Dictionary <int, SavedComponent> bpcomps = null;

                if (save.isBlueprint)
                {
                    bp_entity.Add(eobj.blueprint_ID, entity);
                    bp_all_comp.TryGetValue(eobj.blueprint_ID, out bpcomps);
                    if (bpcomps == null)
                    {
                        bpcomps = new Dictionary <int, SavedComponent>();
                        bp_parent_component.Add(eobj.blueprint_ID, bpcomps);
                    }
                    bp_parent_component.TryGetValue(eobj.blueprint_ID, out ecomps);
                    if (ecomps == null)
                    {
                        ecomps = new Dictionary <int, SavedComponent>();
                        bp_parent_component.Add(eobj.blueprint_ID, ecomps);
                    }
                }
                else
                {
                    allEntities.Add(eobj.instance_ID, entity);
                }

                entity.instanceID  = eobj.instance_ID;
                entity.blueprintID = eobj.blueprint_ID;

                if (eobj.parentIsComponent || eobj.parentIsEntity)
                {
                    toParent.Add(eobj, entity);
                }

                var comps = gameobj.GetComponentsInChildren <SavedComponent>(true);
                if (comps.Length != 0)
                {
                    if (save.isBlueprint)
                    {
                        save.components.TryGetValue(entity.blueprintID, out cobjects);
                    }
                    else
                    {
                        save.components.TryGetValue(entity.instanceID, out cobjects);
                    }

                    if (cobjects != null)
                    {
                        foreach (var component in comps)
                        {
                            if (save.isBlueprint)
                            {
                                ecomps.Add(component.componentID, component);
                            }

                            ComponentObject cobj = null;
                            cobjects.TryGetValue(component.componentID, out cobj);
                            if (cobj != null)
                            {
                                SetDataForComponent(component, cobj.data);
                                initializedfield.SetValue(component, cobj.initialized);
                                component.enabled = cobj.enabled;
                            }

                            //Storing for later reference injection
                            Dictionary <int, SavedComponent> injectionDict = null;
                            allComps.TryGetValue(entity.ID, out injectionDict);
                            if (injectionDict == null)
                            {
                                injectionDict = new Dictionary <int, SavedComponent>();
                                allComps.Add(entity.ID, injectionDict);
                            }
                            injectionDict.Add(component.componentID, component);

                            allComponents.Add(component);
                        }
                    }
                }

                if (save.isBlueprint)
                {
                    entity.instanceID = 0;
                }



                prefab.SetActive(prefabState);

                if (eobj.Enabled)
                {
                    entity.gameObject.SetActive(true);
                }
                else
                {
                    entity.InitializeDisabled();
                }
                //HACK change this to something like interface.
                //entity.gameObject.BroadcastMessage("OnAfterLoad", SendMessageOptions.DontRequireReceiver);
            }

            if (CompRefSerializationProcessor.refs != null && CompRefSerializationProcessor.refs.Count > 0)
            {
                foreach (var compref in CompRefSerializationProcessor.refs)
                {
                    if (!compref.isNull)
                    {
                        Dictionary <int, SavedComponent> comps = null;
                        SavedComponent cbase = null;

                        if (save.isBlueprint)
                        {
                            bp_parent_component.TryGetValue(compref.entity_ID, out comps);
                        }
                        else
                        {
                            allComps.TryGetValue(compref.entity_ID, out comps);
                        }

                        if (comps != null)
                        {
                            if (save.isBlueprint)
                            {
                                bp_parent_component[compref.entity_ID].TryGetValue(compref.component_ID, out cbase);
                            }
                            else
                            {
                                comps.TryGetValue(compref.component_ID, out cbase);
                            }
                            if (cbase != null)
                            {
                                compref.component = cbase;
                            }
                            else
                            {
                                Debug.LogError("CompRef linker could not find component with id: " + compref.component_ID + " on entity: " + compref.entityName);
                            }
                        }
                        else
                        {
                            Debug.LogError("CompRef linker could not find entity with id: " + compref.entity_ID + " on entity: " + compref.entityName);
                        }
                    }
                }
            }
#if UNITY_EDITOR
            if (blueprintEditorMode)
            {
                foreach (var e in bp_entity)
                {
                    var go = PrefabUtility.FindPrefabRoot(e.Value.gameObject);
                    PrefabUtility.DisconnectPrefabInstance(go);
                    PrefabUtility.ReconnectToLastPrefab(go);
                }
            }
#endif
            if (save.isBlueprint)
            {
                if (blueprintEditorMode)
                {
#if UNITY_EDITOR
                    foreach (var pair in toParent)
                    {
                        SaveEntity e  = pair.Value;
                        var        go = PrefabUtility.FindPrefabRoot(e.gameObject);
                        PrefabUtility.DisconnectPrefabInstance(go);

                        EntityObject eobj   = pair.Key;
                        Transform    parent = null;
                        if (eobj.parentIsComponent)
                        {
                            parent = bp_parent_component[eobj.parent_entity_ID][eobj.parent_component_ID].transform;
                        }
                        else if (eobj.parentIsEntity)
                        {
                            parent = bp_entity[eobj.parent_entity_ID].transform;
                        }
                        e.transform.SetParent(parent);
                        PrefabUtility.ReconnectToLastPrefab(go);
                    }
#endif
                }
                else
                {
                    foreach (var pair in toParent)
                    {
                        SaveEntity   e      = pair.Value;
                        EntityObject eobj   = pair.Key;
                        Transform    parent = null;
                        if (eobj.parentIsComponent)
                        {
                            parent = bp_parent_component[eobj.parent_entity_ID][eobj.parent_component_ID].transform;
                        }
                        else if (eobj.parentIsEntity)
                        {
                            parent = bp_entity[eobj.parent_entity_ID].transform;
                        }
                        e.transform.SetParent(parent);
                    }
                }
            }
            else
            {
                foreach (var pair in toParent)
                {
                    SaveEntity   e      = pair.Value;
                    EntityObject eobj   = pair.Key;
                    Transform    parent = null;
                    if (eobj.parentIsComponent)
                    {
                        parent = allComps[eobj.parent_entity_ID][eobj.parent_component_ID].transform;
                    }
                    else if (eobj.parentIsEntity)
                    {
                        parent = allEntities[eobj.parent_entity_ID].transform;
                    }
                    e.transform.SetParent(parent);
                }
            }

            foreach (var comp in allComponents)
            {
                comp.SendMessage("OnAfterLoad", SendMessageOptions.DontRequireReceiver);
            }
        }