public ILevelEditorObject CreateObject(ILevelEditorResource resource, Vector3 position, Quaternion rotation, Transform parent, uint instanceID, bool registerUndo = true)
        {
            LevelEditorLogger.Log($"Create object | Resource: {resource} | Position: {position} | Rotation: {rotation} | Parent: {parent} | Instance ID: {instanceID} | Register undo: {registerUndo}");

            if (resource.Asset is GameObject go)
            {
                return(CreateLevelEditorObject((p, r, par) =>
                {
                    GameObject objGo = Instantiate(go, p, r, par);
                    return objGo.GetOrAddComponent <LevelEditorObject>();
                }, resource, instanceID, registerUndo, position, rotation, parent));
            }

            if (resource.Asset is Component comp)
            {
                return(CreateLevelEditorObject((p, r, par) =>
                {
                    Component objGo = Instantiate(comp, p, r, par);
                    return objGo.gameObject.GetOrAddComponent <LevelEditorObject>();
                }, resource, instanceID, registerUndo, position, rotation, parent));
            }

            Debug.LogError($"Tried to create {resource.Name} ({resource.ID}) but the asset is not a prefab.");
            return(null);
        }
        public virtual void SaveLevel(LevelEditorSaveData saveData, string path)
        {
            string saveFolder = Path.GetDirectoryName(path);

            if (!Directory.Exists(saveFolder))
            {
                Directory.CreateDirectory(saveFolder);
            }

            LevelSavingLoadingArgs args = new LevelSavingLoadingArgs(saveData, path);

            OnLevelSaving?.Invoke(this, args);
            if (args.Cancel)
            {
                LevelEditorLogger.Log("LevelEditorSaveManager saving was canceled.");
                return;
            }

            saveData.customData = args.GetAllCustomData();

            switch (levelFormat)
            {
            case FormatType.JSON:
                string json = LevelEditorSerializer.SerializeJson(saveData);
                File.WriteAllText(path, json);
                break;

            case FormatType.Binary:
                byte[] bytes = LevelEditorSerializer.SerializeBinary(saveData, compress);
                File.WriteAllBytes(path, bytes);
                break;
            }

            OnLevelSaved?.Invoke(this, new LevelEventArgs(saveData));
        }
        public static void AddObject(ILevelEditorObject value)
        {
            LevelEditorLogger.Log($"Add object to world: {value}");
            Assert.IsFalse(objects.Contains(value), "This object has already been added to the world.");

            objects.Add(value);
        }
        public void Undo()
        {
            LevelEditorLogger.Log("Undo");

            if (counter > 0)
            {
                counter--;
                IUndoAction action = actionHistory[counter];
                action.Undo(this);

                OnUndo?.Invoke(action);
            }
        }
        public void Redo()
        {
            LevelEditorLogger.Log("Redo");

            if (counter < actionHistory.Count)
            {
                IUndoAction action = actionHistory[counter];
                action.Redo(this);
                counter++;

                OnRedo?.Invoke(action);
            }
        }
        public void AddAction(IUndoAction action)
        {
            LevelEditorLogger.Log($"Add undo action | Action: {action}");

            // Remove the actions above if we cut into the history.
            while (actionHistory.Count > counter)
            {
                actionHistory.RemoveAt(counter);
            }

            actionHistory.Add(action);
            counter++;
        }
        public virtual LevelEditorSaveData LoadLevel(LevelEditorSaveData data)
        {
            LevelSavingLoadingArgs args = new LevelSavingLoadingArgs(data, loadLocation);

            loadLocation = null;
            OnLevelLoading?.Invoke(this, args);
            if (args.Cancel)
            {
                LevelEditorLogger.Log("LevelEditorSaveManager loading was canceled.");
                return(new LevelEditorSaveData(null));
            }

            realObjectManager.DeleteAllObjects();
            realObjectManager.CreateObjectsFromSaveData(data);

            OnLevelLoaded?.Invoke(this, new LevelEventArgs(data));

            return(data);
        }
示例#8
0
        private static void RegisterResolversInternal()
        {
            if (serializerRegistered)
            {
                return;
            }

            LevelEditorLogger.Log("Registering LevelEditorResolver.");

            customResolvers.Add(instance);
            customResolvers.Add(UnityResolver.Instance);
            customResolvers.Add(UnityBlitResolver.Instance);
            customResolvers.Add(StandardResolver.Instance);

            StaticCompositeResolver.Instance.Register(customResolvers.ToArray());
            LevelEditorSerializer.Options = (LevelEditorSerializerOptions)LevelEditorSerializer.Options.WithResolver(StaticCompositeResolver.Instance);

            serializerRegistered = true;
        }
        public bool DeleteObject(ILevelEditorObject target, bool registerUndo = true)
        {
            if (target == null)
            {
                return(false);
            }

            LevelEditorObjectEventDeletingEvent args = new LevelEditorObjectEventDeletingEvent(target);

            OnDeletingObject?.Invoke(this, args);

            if (args.Cancel)
            {
                LevelEditorLogger.Log("DeleteObject canceled.");
                return(false);
            }

            DeleteObjectInternal(target, registerUndo);

            allObjects.Remove(target);

            return(true);
        }
        private ILevelEditorObject CreateLevelEditorObject(Func <Vector3, Quaternion, Transform, ILevelEditorObject> createObject, ILevelEditorResource resource, uint instanceID, bool registerUndo, Vector3 position, Quaternion rotation, Transform parent)
        {
            if (objectsWithId.ContainsKey(instanceID))
            {
                throw new DuplicateIDException($"There already is an object with the instance ID {instanceID}.");
            }

            LevelEditorObjectEventSpawningEvent args = new LevelEditorObjectEventSpawningEvent(resource);

            OnCreatingObject?.Invoke(this, args);

            if (args.Cancel)
            {
                LevelEditorLogger.Log("CreateObject canceled. Returning null.");
                return(null);
            }

            ILevelEditorObject obj;

            if (poolObjects && pooledObjects.ContainsKey(resource.ID) && pooledObjects[resource.ID].Count > 0)
            {
                obj = pooledObjects[resource.ID].Pop();
                obj.MyGameObject.transform.SetPositionAndRotation(position, rotation);
                obj.MyGameObject.transform.SetParent(parent);
            }
            else
            {
                obj    = createObject.Invoke(position, rotation, parent);
                obj.ID = resource.ID;

                LevelEditorComponentWrapper.AddWrappers(obj.MyGameObject);
                obj.GetExposedComponents();
            }

            obj.MyGameObject.name = resource.Name;
            obj.InstanceID        = instanceID;
            obj.MyGameObject.SetActive(true);

            if (instanceID >= nextInstanceID)
            {
                nextInstanceID = instanceID;
            }

            if (!activeObjects.ContainsKey(resource.ID))
            {
                activeObjects.Add(resource.ID, new List <ILevelEditorObject>());
            }

            if (!objectCount.ContainsKey(resource.ID))
            {
                objectCount.Add(resource.ID, 0);
            }

            activeObjects[resource.ID].Add(obj);
            objectCount[resource.ID]++;
            allObjects.Add(obj);

            obj.OnUnPooled();

            objectsWithId[obj.InstanceID] = obj;

            OnCreatedObject?.Invoke(this, new LevelEditorObjectEvent(obj));

            if (registerUndo && Undo != null)
            {
                undoComp.AddAction(new CreateObjectUndoAction(resource, position, rotation, parent, instanceID, obj));
            }

            return(obj);
        }
 public static bool RemoveObject(uint instanceId)
 {
     LevelEditorLogger.Log($"Remove object from world using instance ID: {instanceId}");
     return(TryGetObject(instanceId, out ILevelEditorObject value) && RemoveObject(value));
 }
 public static bool RemoveObject(ILevelEditorObject value)
 {
     LevelEditorLogger.Log($"Remove objects from world: {value}");
     return(objects.Remove(value));
 }