/// <summary> /// Draws the main window with (if selected) the editor for each viewable scriptable object /// </summary> void DrawMainWindow() { // Begin drawing BeginWindows(); // Size of each window with padding is.. float windowWidth = 100f; // No need to do anything here if there's nothing to draw, this must be checked within BeginWindows // and not before if (_scriptableObjectWindows.Count() == 0) { return; } windowWidth = _scriptableObjectWindows [0].WindowRect.width + XPADDING; // Absolute number we can fit into this editor window width int remainder = 0; float windowsWidthCount = System.Math.DivRem((int)(this.position.width - SIDEWINDOWWIDTH - STARTX), (int)windowWidth, out remainder); int currentXCount = 0; float yPosition = STARTY; float maxPreviousRowHeight = 0; for (int i = 0; i < _scriptableObjectWindows.Count(); i++) { DefaultItemWindow scriptableObjectWindow = _scriptableObjectWindows[i]; if (scriptableObjectWindow == null || scriptableObjectWindow.Data == null) { continue; } scriptableObjectWindow.WindowRect = GUILayout.Window(i, scriptableObjectWindow.WindowRect, scriptableObjectWindow.DrawWindow, scriptableObjectWindow.Data.name, _options); // Current Window Rect Rect windowPositionRect = scriptableObjectWindow.WindowRect; windowPositionRect.x = SIDEWINDOWWIDTH + STARTX; windowPositionRect.x += (windowWidth * currentXCount); windowPositionRect.y = yPosition; scriptableObjectWindow.WindowRect = windowPositionRect; if (windowPositionRect.height > maxPreviousRowHeight) { maxPreviousRowHeight = windowPositionRect.height; } // If we've exceeded the max column count then increment the row count and reset the column's currentXCount++; if (currentXCount >= (int)windowsWidthCount) { currentXCount = 0; yPosition += maxPreviousRowHeight + YPADDING; maxPreviousRowHeight = 0f; } } EndWindows(); }
/// <summary> /// Handle the mouse pointer right click. /// </summary> /// <param name="e">E.</param> void HandleMousePointerRightClick(Event e) { if (e.button == 1 && e.type == EventType.MouseUp && _selectedType != null) { var menu = new GenericMenu(); menu.AddItem(new GUIContent("Add New " + _selectedType.Name), false, delegate { string folderPath = ""; // Get the path of an existing scriptable object for this type, if applicable // Find a window of the same type DefaultItemWindow window = _scriptableObjectWindows.Where(a => a.Data.GetType() == _selectedType).FirstOrDefault(); if (window != null) { string fullPath = AssetDatabase.GetAssetPath(window.Data); folderPath = System.IO.Path.GetDirectoryName(fullPath); } if (folderPath == string.Empty) { folderPath = "Assets/"; } string path = EditorUtility.SaveFilePanel("Create Scriptable Object", folderPath, String.Format("{0}.asset", _selectedType.Name), "asset"); if (path == "") { return; } path = FileUtil.GetProjectRelativePath(path); var asset = CreateInstance(_selectedType.ToString()); AssetDatabase.CreateAsset(asset, path); AssetDatabase.SaveAssets(); ReloadWindows(); }); menu.ShowAsContext(); e.Use(); } }