Skip to content

Commit

Permalink
Allow adding screens straight from the hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
neilsarkar committed Jul 23, 2020
1 parent 14eca79 commit ddd73f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
23 changes: 9 additions & 14 deletions Editor/ScreenEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ static ScreenEditor() {
saturatedViolet = Color.HSVToRGB(h, 1f, 1f);
}

[MenuItem("GameObject/Create Screen", false, 0)]
static void CreateScreen() {
var gameObject = UnityEditor.Selection.activeGameObject;
if (gameObject.GetComponent<Navigator>() == null) {
throw new VioletException($"Unable to create screen child of {gameObject.name} - try adding a Navigator component to {gameObject.name} first.");
}
Debug.Log("nice");
}

static void DrawHierarchyItem(int instanceID, Rect rect) {
var gameObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (gameObject == null) { return; }
Expand All @@ -53,10 +44,14 @@ static void DrawHierarchyItem(int instanceID, Rect rect) {
}

static void DrawNavigator(Navigator navigator, Rect rect) {
if (navigator.EditingScreen == null) {return;}

if (Button(rect, "Save", true)) {
navigator.FinishEditing();
if (navigator.EditingScreen == null) {
if (Button(rect, "Add")) {
navigator.AddScreen();
}
} else if (navigator.transform.childCount > 1) {
if (Button(rect, "Save", true)) {
navigator.FinishEditing();
}
}
}

Expand All @@ -69,7 +64,7 @@ static void DrawScreen(Screen screen, Rect rect) {
throw new VioletException($"Tried to edit {screen.name} without a Navigator. Try adding a Navigator component to {screen.transform.parent.name}");
}
if (screen.isActiveAndEnabled) {
navigator.FinishEditing();
navigator.FinishEditing(screen);
} else {
navigator.Edit(screen);
}
Expand Down
22 changes: 17 additions & 5 deletions Runtime/Navigation/Navigator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,25 @@ public void Edit(Screen screen) {
}
}

public void FinishEditing() {
PrefabUtility.SaveAsPrefabAssetAndConnect(EditingScreen.gameObject, $"Assets/Menus/{EditingScreen.name}.prefab", InteractionMode.AutomatedAction);

EditingScreen.gameObject.SetActive(false);
EditingScreen = null;
public void FinishEditing(Screen screen = null) {
if (EditingScreen == null) { EditingScreen = gameObject.GetComponentInChildren<Screen>(); }
if (screen == null) { screen = EditingScreen; }
PrefabUtility.SaveAsPrefabAssetAndConnect(screen.gameObject, $"Assets/Menus/{screen.name}.prefab", InteractionMode.AutomatedAction);
screen.gameObject.SetActive(false);
if (screen == EditingScreen) { EditingScreen = null; } ;
homeScreen = originalHomeScreen;
}

public void AddScreen() {
var gameObject = new GameObject("Rename Me");
var screen = gameObject.AddComponent<Screen>();
var canvas = gameObject.AddComponent<Canvas>();

gameObject.transform.parent = transform;
gameObject.transform.position = new Vector3(0,0,0);
canvas.renderMode = worldCamera == null ? RenderMode.ScreenSpaceOverlay : RenderMode.ScreenSpaceCamera;
EditingScreen = screen;
}
#endif
}
}

0 comments on commit ddd73f4

Please sign in to comment.