Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Fixing playmode switching errors and attempting to make scene switchi…
Browse files Browse the repository at this point in the history
…ng smooth, too. Right now, an error will still occur if you force OnGUI to be called on the window before OnHierarchyChange (by moving the cursor over the window while loading scene is performed). Also, runtime loading of the editor state is now officially not supported (previously it would throw an error) because of missing support to load subAssets at runtime. Previous implementation was faulty and did not work in the end either.

In the end, fixes were done by shifting playmode code from runtime usage to editor usage (making use of editor classes) because the runtime part is what was causing the errors in the first place.
  • Loading branch information
Seneral committed Apr 29, 2016
1 parent 29b3f39 commit cc44390
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
18 changes: 16 additions & 2 deletions Editor/Node_Editor/NodeEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,25 @@ private void OnEnable ()

NodeEditor.ClientRepaints -= Repaint;
NodeEditor.ClientRepaints += Repaint;

EditorLoadingControl.justLeftPlayMode -= NormalReInit;
EditorLoadingControl.justLeftPlayMode += NormalReInit;
// Here, both justLeftPlayMode and justOpenedNewScene have to act because of timing
EditorLoadingControl.justOpenedNewScene -= NormalReInit;
EditorLoadingControl.justOpenedNewScene += NormalReInit;


// Setup Cache
tempSessionPath = Path.GetDirectoryName (AssetDatabase.GetAssetPath (MonoScript.FromScriptableObject (this)));
LoadCache ();
SetupCacheEvents ();
}

private void NormalReInit ()
{
NodeEditor.ReInit (false);
}

private void OnDestroy ()
{
NodeEditor.ClientRepaints -= Repaint;
Expand Down Expand Up @@ -181,8 +194,9 @@ private void SetupCacheEvents ()
EditorLoadingControl.lateEnteredPlayMode -= LoadCache;
EditorLoadingControl.lateEnteredPlayMode += LoadCache;

EditorLoadingControl.justLeftPlayMode -= LoadCache;
EditorLoadingControl.justLeftPlayMode += LoadCache;
// included in justOpenedNewScene as playmode scene is a new, temporary scene
//EditorLoadingControl.justLeftPlayMode -= LoadCache;
//EditorLoadingControl.justLeftPlayMode += LoadCache;

EditorLoadingControl.justOpenedNewScene -= LoadCache;
EditorLoadingControl.justOpenedNewScene += LoadCache;
Expand Down
10 changes: 3 additions & 7 deletions Node_Editor/Framework/NodeEditorSaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,12 @@ public static List<NodeEditorState> LoadEditorStates (string path, bool createWo
if (string.IsNullOrEmpty (path))
throw new UnityException ("Cannot load NodeEditorStates: No path specified to load the EditorStates from!");

// Fetch all objects in the save file
ScriptableObject[] objects = ResourceManager.LoadResources<ScriptableObject> (path);
if (objects == null || objects.Length == 0)
throw new UnityException ("Cannot load NodeEditorStates: The specified path '" + path + "' does not point to a save file!");
// Fetch all editorStates in the save file
List<NodeEditorState> editorStates = ResourceManager.LoadResources<NodeEditorState> (path).ToList ();

// Obtain the editorStates in that asset file and create a working copy of them
List<NodeEditorState> editorStates = objects.OfType<NodeEditorState> ().ToList ();
#if UNITY_EDITOR
if (createWorkingCopy)
{
{ // Create a working copy of the editorStates
for (int cnt = 0; cnt < editorStates.Count; cnt++)
{
NodeEditorState state = editorStates[cnt];
Expand Down
2 changes: 1 addition & 1 deletion Node_Editor/Utilities/EditorLoadingControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static EditorLoadingControl ()
}

private static void OnHierarchyChange ()
{
{ // TODO: OnGUI might be called before this function and migth cause problems. Find a better way to detect scene change!
#if UNITY_5_3_OR_NEWER || UNITY_5_3
Scene currentScene = EditorSceneManager.GetActiveScene ();
#else
Expand Down
31 changes: 20 additions & 11 deletions Node_Editor/Utilities/ResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,24 @@ public static void SetDefaultResourcePath (string defaultResourcePath)
public static string PreparePath (string path)
{
path = path.Replace (Application.dataPath, "Assets");
if (Application.isPlaying)
{ // At runtime
if (path.Contains ("Resources"))
path = path.Substring (path.LastIndexOf ("Resources") + 10);
path = path.Substring (0, path.LastIndexOf ('.'));
return path;
}
// In the editor
#if UNITY_EDITOR
if (!path.StartsWith ("Assets/"))
path = _ResourcePath + path;
#else
if (path.Contains ("Resources"))
path = path.Substring (path.LastIndexOf ("Resources") + 10);
path = path.Substring (0, path.LastIndexOf ('.'));
#endif
// if (Application.isPlaying)
// { // At runtime
// if (path.Contains ("Resources"))
// path = path.Substring (path.LastIndexOf ("Resources") + 10);
// path = path.Substring (0, path.LastIndexOf ('.'));
// return path;
// }
// // In the editor
// if (!path.StartsWith ("Assets/"))
// path = _ResourcePath + path;
return path;
}

Expand All @@ -46,13 +54,14 @@ public static string PreparePath (string path)
public static T[] LoadResources<T> (string path) where T : UnityEngine.Object
{
path = PreparePath (path);
if (Application.isPlaying) // At runtime
return UnityEngine.Resources.LoadAll<T> (path);
//if (Application.isPlaying) // At runtime
//return UnityEngine.Resources.LoadAll<T> (path);
#if UNITY_EDITOR
// In the editor
return UnityEditor.AssetDatabase.LoadAllAssetsAtPath (path).OfType<T> ().ToArray ();
#else
return null;
throw new NotImplementedException ("Currently it is not possible to load subAssets at runtime!");
return UnityEngine.Resources.LoadAll<T> (path);
#endif
}

Expand Down

0 comments on commit cc44390

Please sign in to comment.