Skip to content

Commit

Permalink
Revert screen by reading prefab from filesystem, closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
neilsarkar committed Feb 22, 2021
1 parent 54e1068 commit c1754f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Runtime/Navigation/ScreenId.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Names are automatically added through ScreenIdGenerator.cs, deletions are done manually :)
public enum ScreenId {
None = 0,
}
}
38 changes: 16 additions & 22 deletions Runtime/Navigation/VioletScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void Update() {
EditorSceneManager.sceneSaved += EditorSceneManager_sceneSaved;
}

[SerializeField, HideInInspector]
string prefabPath = "";
UnityEngine.Object prefab;
public bool isEditing;
Expand All @@ -119,31 +120,24 @@ public void PackPrefab() {

public void RevertPrefab() {
Violet.Log($"Reverting. You will lose work!");

isEditing = false;
if (prefab != null) {
var revertedScreen = PrefabUtility.InstantiatePrefab(prefab, transform.parent) as GameObject;
revertedScreen.transform.SetSiblingIndex(gameObject.transform.GetSiblingIndex());
revertedScreen.SetActive(false);
DestroyImmediate(gameObject);
} else {
PackPrefab();
var path = string.IsNullOrEmpty(prefabPath) ? $"Assets/Menus/{name}.prefab" : prefabPath;
UnityEngine.Debug.LogWarning($"@null - reverting prefab shouldn't rely on git in case projects aren't using git");
var proc = new System.Diagnostics.Process() {
StartInfo = new System.Diagnostics.ProcessStartInfo() {
FileName = "git",
Arguments = $"checkout \"{path}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
while (!proc.StandardOutput.EndOfStream) {
var line = proc.StandardOutput.ReadLine();
print($"line={line}");
if (string.IsNullOrEmpty(prefabPath)) {
throw new Exception($"Can't revert prefab for {gameObject.name} because prefabPath is null. Try saving and discarding changes in git.");
}

if (prefab == null) {
prefab = AssetDatabase.LoadAssetAtPath<VioletScreen>(prefabPath)?.gameObject;

if (prefab == null) {
throw new Exception($"Couldn't find prefab path={prefabPath}");
}
}

var revertedScreen = PrefabUtility.InstantiatePrefab(prefab, transform.parent) as GameObject;
revertedScreen.transform.SetSiblingIndex(gameObject.transform.GetSiblingIndex());
revertedScreen.SetActive(false);
DestroyImmediate(gameObject);
}

public void UnpackPrefab() {
Expand Down

0 comments on commit c1754f7

Please sign in to comment.