public static bool LoadWindowLayout(string path, bool newProjectLayoutWasCreated)
{
Rect mainWindowPosition = new Rect();
UnityObject[] containers = Resources.FindObjectsOfTypeAll(typeof(ContainerWindow));
foreach (ContainerWindow window in containers)
{
if (window.showMode == ShowMode.MainWindow)
{
mainWindowPosition = window.position;
}
}
bool layoutLoadingIssue = false;
// Load new windows and show them
try
{
ContainerWindow.SetFreezeDisplay(true);
CloseWindows();
// Load data
UnityObject[] loadedWindows = InternalEditorUtility.LoadSerializedFileAndForget(path);
if (loadedWindows == null || loadedWindows.Length == 0)
{
throw new ArgumentException("Window layout at '" + path + "' could not be loaded.");
}
List <UnityObject> newWindows = new List <UnityObject>();
// At this point, unparented editor windows are neither desired nor desirable.
// This can be caused by (legacy) serialization of FallbackEditorWindows or
// other serialization hiccups (note that unparented editor windows should not exist in theory).
// Same goes for empty DockAreas (no panes). Leave them behind.
for (int i = 0; i < loadedWindows.Length; i++)
{
UnityObject o = loadedWindows[i];
EditorWindow editorWin = o as EditorWindow;
if (editorWin != null)
{
if (editorWin.m_Parent == null)
{
UnityObject.DestroyImmediate(editorWin, true);
Console.WriteLine("LoadWindowLayout: Removed unparented EditorWindow while reading window layout: window #" + i + ", type=" +
o.GetType().ToString() + ", instanceID=" + o.GetInstanceID());
layoutLoadingIssue = true;
continue;
}
}
else
{
DockArea dockArea = o as DockArea;
if (dockArea != null && dockArea.m_Panes.Count == 0)
{
dockArea.Close(null);
Console.WriteLine("LoadWindowLayout: Removed empty DockArea while reading window layout: window #" + i + ", instanceID=" +
o.GetInstanceID());
layoutLoadingIssue = true;
continue;
}
}
newWindows.Add(o);
}
ContainerWindow mainWindowToSetSize = null;
ContainerWindow mainWindow = null;
for (int i = 0; i < newWindows.Count; i++)
{
ContainerWindow cur = newWindows[i] as ContainerWindow;
if (cur != null && cur.showMode == ShowMode.MainWindow)
{
mainWindow = cur;
if (mainWindowPosition.width != 0.0)
{
mainWindowToSetSize = cur;
mainWindowToSetSize.position = mainWindowPosition;
}
}
}
for (int i = 0; i < newWindows.Count; i++)
{
UnityObject o = newWindows[i];
if (o == null)
{
Console.WriteLine("LoadWindowLayout: Error while reading window layout: window #" + i + " is null");
layoutLoadingIssue = true;
// Keep going
}
else if (o.GetType() == null)
{
Console.WriteLine("LoadWindowLayout: Error while reading window layout: window #" + i + " type is null, instanceID=" + o.GetInstanceID());
layoutLoadingIssue = true;
// Keep going
}
else
{
if (newProjectLayoutWasCreated)
{
MethodInfo method = o.GetType().GetMethod("OnNewProjectLayoutWasCreated", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (method != null)
{
method.Invoke(o, null);
}
}
}
}
if (mainWindowToSetSize)
{
mainWindowToSetSize.position = mainWindowPosition;
mainWindowToSetSize.OnResize();
}
// Always show main window before other windows. So that other windows can
// get their parent/owner.
if (mainWindow == null)
{
Debug.LogError("Error while reading window layout: no main window found");
throw new System.Exception();
}
mainWindow.Show(mainWindow.showMode, true, true);
// Show other windows
for (int i = 0; i < newWindows.Count; i++)
{
EditorWindow win = newWindows[i] as EditorWindow;
if (win)
{
win.minSize = win.minSize; // Causes minSize to be propagated upwards to parents!
}
ContainerWindow containerWindow = newWindows[i] as ContainerWindow;
if (containerWindow && containerWindow != mainWindow)
{
containerWindow.Show(containerWindow.showMode, true, true);
}
}
// Unmaximize maximized GameView if maximize on play is enabled
GameView gameView = GetMaximizedWindow() as GameView;
if (gameView != null && gameView.maximizeOnPlay)
{
Unmaximize(gameView);
}
// For new projects, show services window if and only if online and logged in
if (newProjectLayoutWasCreated)
{
if (UnityConnect.instance.online && UnityConnect.instance.loggedIn && UnityConnect.instance.shouldShowServicesWindow)
{
UnityConnectServiceCollection.instance.ShowService(HubAccess.kServiceName, true, "new_project_created");
}
else
{
UnityConnectServiceCollection.instance.CloseServices();
}
}
}
catch (System.Exception ex)
{
Debug.LogError("Failed to load window layout: " + ex);
int option = 0;
UnityObject[] containerWindows = Resources.FindObjectsOfTypeAll(typeof(ContainerWindow));
// Only show dialog if an actual window is present. If not, revert to default immediately
if (!Application.isTestRun && containerWindows.Length > 0)
{
option = EditorUtility.DisplayDialogComplex("Failed to load window layout", "This can happen if layout contains custom windows and there are compile errors in the project.", "Load Default Layout", "Quit", "Revert Factory Settings");
}
switch (option)
{
case 0:
LoadDefaultLayout();
break;
case 1:
EditorApplication.Exit(0);
break;
case 2:
RevertFactorySettings();
break;
}
return(false);
}
finally
{
ContainerWindow.SetFreezeDisplay(false);
if (Path.GetExtension(path) == ".wlt")
{
Toolbar.lastLoadedLayoutName = Path.GetFileNameWithoutExtension(path);
}
else
{
Toolbar.lastLoadedLayoutName = null;
}
}
if (layoutLoadingIssue)
{
Debug.Log("The editor layout could not be fully loaded, this can happen when the layout contains EditorWindows not available in this project");
}
return(true);
}