/// <summary> /// Loads the utmodule at the given file path /// </summary> /// <param name="moduleFile">Relative path to the .utmodule file</param> public static void LoadModule(string moduleFile) { Assert.IsFalse(EditorApplication.isPlayingOrWillChangePlaymode); var context = new UTinyContext(); var registry = context.Registry; UTinyPersistence.LoadModule(moduleFile, registry); var module = registry.FindAllBySource(UTinyRegistry.DefaultSourceIdentifier).OfType <UTinyModule>().First(); Assert.IsNotNull(module); module.Name = Path.GetFileNameWithoutExtension(moduleFile); SetupModule(registry, module); var project = registry.CreateProject(UTinyId.Generate(KWorkspaceProjectName), KWorkspaceProjectName); project.Module = (UTinyModule.Reference)module; var editorContext = new UTinyEditorContext((UTinyProject.Reference)project, EditorContextType.Module, context, UTinyEditorPrefs.LoadWorkspace(project.PersistenceId)); LoadContext(editorContext, isChanged: false); }
/// <summary> /// Creates and loads a new .utmodule /// @NOTE The module only exists in memory until Save() is called /// </summary> public static void NewModule() { var context = new UTinyContext(); var registry = context.Registry; UTinyPersistence.LoadAllModules(registry); // Create a `workspace` project to host the module for editing purposes var project = registry.CreateProject(UTinyId.Generate(KWorkspaceProjectName), KWorkspaceProjectName); // Create objects for the new module var module = registry.CreateModule(UTinyId.New(), "NewModule"); // Setup initial state for the module module.Namespace = "module"; SetupModule(registry, module); project.Module = (UTinyModule.Reference)module; var workspace = new UTinyEditorWorkspace(); UTinyEditorPrefs.SaveWorkspace(workspace); var editorContext = new UTinyEditorContext((UTinyProject.Reference)project, EditorContextType.Module, context, workspace); LoadContext(editorContext, isChanged: true); }
/// <summary> /// Creates and loads a new .utproject /// @NOTE The project only exists in memory until Save() is called /// </summary> public static void NewProject() { Assert.IsFalse(EditorApplication.isPlayingOrWillChangePlaymode); var context = new UTinyContext(); var registry = context.Registry; UTinyPersistence.LoadAllModules(registry); // Create new objects for the project var project = registry.CreateProject(UTinyId.New(), "NewProject"); var module = registry.CreateModule(UTinyId.New(), "Main"); // Setup the start scene var entityGroup = registry.CreateEntityGroup(UTinyId.New(), "NewEntityGroup"); var entityGroupRef = (UTinyEntityGroup.Reference)entityGroup; var cameraEntity = registry.CreateEntity(UTinyId.New(), "Camera"); var transform = cameraEntity.AddComponent(registry.GetTransformType()); transform.Refresh(); var camera = cameraEntity.AddComponent(registry.GetCamera2DType()); camera.Refresh(); camera["clearFlags"] = new UTinyEnum.Reference(registry.GetCameraClearFlagsType().Dereference(registry), 1); camera.AssignPropertyFrom("backgroundColor", Color.black); camera["depth"] = -1.0f; entityGroup.AddEntityReference((UTinyEntity.Reference)cameraEntity); // Setup initial state for the project module.Options |= UTinyModuleOptions.ProjectModule; module.Namespace = "game"; module.StartupEntityGroup = (UTinyEntityGroup.Reference)entityGroup; module.AddEntityGroupReference(entityGroupRef); project.Module = (UTinyModule.Reference)module; project.Settings.EmbedAssets = true; project.Settings.CanvasWidth = 1920; project.Settings.CanvasHeight = 1080; SetupProject(registry, project); module.AddExplicitModuleDependency((UTinyModule.Reference)registry.FindByName <UTinyModule>("UTiny.Core2D")); // Always include a dependency on core, math, core2d by default // And HTML for now, since it is the only renderer we have right now. module.AddExplicitModuleDependency((UTinyModule.Reference)registry.FindByName <UTinyModule>("UTiny.HTML")); var workspace = new UTinyEditorWorkspace { OpenedEntityGroups = { entityGroupRef }, ActiveEntityGroup = entityGroupRef }; UTinyEditorPrefs.SaveWorkspace(workspace); var editorContext = new UTinyEditorContext((UTinyProject.Reference)project, EditorContextType.Project, context, workspace); LoadContext(editorContext, isChanged: true); }
public static Scene GetScratchPad(UTinyEditorContext context) { Assert.IsNotNull(context, $"{UTinyConstants.ApplicationName}: Trying to get the scratch pad of a null editor context."); Assert.IsNotNull(context.Project, $"{UTinyConstants.ApplicationName}: Trying to get the scratch pad of an editor context without any project."); var scenePath = GetScratchPadPath(GetOrCreateContextDirectory(context)); return(SceneManager.GetSceneByPath(scenePath)); }
/// <summary> /// Closes the current Tiny project /// </summary> public static void Close() { if (null == EditorContext) { return; } OnCloseProject?.Invoke(EditorContext.Project); EditorContext?.Unload(); EditorContext = null; UTinyTemp.Delete(); UTinyModificationProcessor.ClearChanged(); }
private static DirectoryInfo GetOrCreateContextDirectory(UTinyEditorContext context) { string directory; // [MP] @TODO: Get the actual folder where the project/solution is stored. if (context.ContextType == EditorContextType.Project) { directory = UTinyPersistence.GetLocation(context.Project); } else { directory = UTinyPersistence.GetLocation(context.Module); } return(Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(directory ?? "Assets/"), k_UTinyTempFolderName))); }
/// <summary> /// Trys to loads the last saved temp file /// </summary> public static void LoadTemp() { Assert.IsFalse(EditorApplication.isPlayingOrWillChangePlaymode); if (!UTinyTemp.Exists()) { return; } var context = new UTinyContext(); var registry = context.Registry; string persistenceId; if (!UTinyTemp.Accept(registry, out persistenceId)) { LoadPersistenceId(persistenceId); return; } var project = registry.FindAllByType <UTinyProject>().FirstOrDefault(); UTinyEditorContext editorContext = null; if (project != null) { SetupProject(registry, project); editorContext = new UTinyEditorContext((UTinyProject.Reference)project, EditorContextType.Project, context, UTinyEditorPrefs.LoadLastWorkspace()); } else { var module = registry.FindAllBySource(UTinyRegistry.DefaultSourceIdentifier).OfType <UTinyModule>().First(); SetupModule(registry, module); if (null != module) { project = registry.CreateProject(UTinyId.Generate(KWorkspaceProjectName), KWorkspaceProjectName); project.Module = (UTinyModule.Reference)module; editorContext = new UTinyEditorContext((UTinyProject.Reference)project, EditorContextType.Module, context, UTinyEditorPrefs.LoadLastWorkspace()); } } Assert.IsNotNull(project); LoadContext(editorContext, true); }
public static bool GetOrGenerateScratchPad(UTinyEditorContext context) { Assert.IsNotNull(context, $"{UTinyConstants.ApplicationName}: Trying to generate a scratch pad from a null editor context."); Assert.IsNotNull(context.Project, $"{UTinyConstants.ApplicationName}: Trying to generate a scratch pad from an editor context without any project."); var directory = GetOrCreateContextDirectory(context); var path = GetScratchPadPath(directory); var scene = SceneManager.GetSceneByPath(path); if (scene.isLoaded && scene.IsValid()) { return(true); } AssetDatabase.ImportAsset(Path.GetDirectoryName(path)); scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); return(EditorSceneManager.SaveScene(scene, path)); }
public static bool ReleaseScratchPad(UTinyEditorContext context) { Assert.IsNotNull(context, $"{UTinyConstants.ApplicationName}: Trying to release a null editor context."); Assert.IsNotNull(context.Project, $"{UTinyConstants.ApplicationName}: Trying to release an editor context without any project."); var directory = GetOrCreateContextDirectory(context); var scenePath = SceneManager.GetSceneByPath(GetScratchPadPath(directory)); if (!scenePath.IsValid()) { return(false); } if (!DestroyScratchPad(directory)) { return(false); } DestroyDirectory(directory); return(true); }
/// <summary> /// Loads the utproject at the given file path /// </summary> /// <param name="projectFile">Relative path to the .utproject file</param> public static void LoadProject(string projectFile) { Assert.IsFalse(EditorApplication.isPlayingOrWillChangePlaymode); var context = new UTinyContext(); var registry = context.Registry; UTinyPersistence.LoadProject(projectFile, registry); var project = registry.FindAllByType <UTinyProject>().First(); Assert.IsNotNull(project); project.Name = Path.GetFileNameWithoutExtension(projectFile); SetupProject(registry, project); var editorContext = new UTinyEditorContext((UTinyProject.Reference)project, EditorContextType.Project, context, UTinyEditorPrefs.LoadWorkspace(project.PersistenceId)); LoadContext(editorContext, isChanged: false); }
private static void LoadContext(UTinyEditorContext context, bool isChanged) { Assert.IsNotNull(context); UTinyModificationProcessor.ClearChanged(); // @NOTE Loading a project can cause a Unity scene to change or be loaded during this operation we dont want to trigger a save using (new UTinyModificationProcessor.DontSaveScope()) { // Cleanup the previous context if (context != EditorContext) { EditorContext?.Unload(); } // Load the new context EditorContext = context; EditorContext.Load(); // Setup the initial state s_WorkspaceVersion = EditorContext.Workspace.Version; OnLoadProject?.Invoke(EditorContext.Project); // Flush the Undo stack EditorContext.Undo.Update(); // Listen for ANY changes and flag the project as changed (*) EditorContext.Caretaker.OnObjectChanged += (originator, memento) => { UTinyModificationProcessor.MarkChanged(); }; EditorContext.Undo.OnUndoPerformed += UTinyModificationProcessor.MarkChanged; EditorContext.Undo.OnRedoPerformed += UTinyModificationProcessor.MarkChanged; } if (isChanged) { UTinyModificationProcessor.MarkChanged(); } }
public HierarchyTree(UTinyEditorContext context, TreeViewState treeViewState) : base(treeViewState) { m_Context = context; m_EntityGroups = new List <UTinyEntityGroup.Reference>(); DroppedOnMethod = new Dictionary <System.Type, DropOnItemAction> { { typeof(HierarchyEntityGroupGraph), DropUponSceneItem }, { typeof(HierarchyEntity), DropUponEntityItem }, { typeof(HierarchyStaticEntity), DropUponStaticEntityItem }, }; DroppedBetweenMethod = new Dictionary <System.Type, DropBetweenAction> { { typeof(HierarchyTreeItemBase), DropBetweenEntityGroupItems }, { typeof(HierarchyEntityGroupGraph), DropBetweenRootEntities }, { typeof(HierarchyEntity), DropBetweenChildrenEntities }, { typeof(HierarchyStaticEntity), DropBetweenStaticEntities }, }; Invalidate(); Reload(); }
public UTinyEntityGroupManager(UTinyEditorContext context) { Assert.IsNotNull(context); m_Context = context; }