private void OnGUI() { EditorGUILayout.HelpBox("The following field has been drawn with MuffinDevGUI.ExtendedObjectField().\nUse the added controls to, from left to right:\n\t- Show/hide informations about the selected asset\n\t- Lock/unlock the selected asset\n\t- Focus the asset in the project view\n\t- Create a new asset if applicable", MessageType.Info); EditorGUILayout.Space(); bool locked = m_Object != null && m_Locked; bool canChangeFoldState = m_Object != null; bool canFocus = m_Object != null; bool canCreate = m_Object != null ? m_Object is ScriptableObject : false; GUI.enabled = !locked; m_Object = MuffinDevGUI.ExtendedObjectField("Selected Asset", m_Object, typeof(Object), true, new ExtendedObjectFieldButton[] { // Add "Fold/Unfold" button new ExtendedObjectFieldButton(m_Folded ? EEditorIcon.Unfold : EEditorIcon.Fold, ExtendedObjectFieldButton.EPosition.BeforeLabel, m_Folded ? "Hide informations" : "Show informations", () => { m_Folded = !m_Folded; }, canChangeFoldState), // Add "Focus" button new ExtendedObjectFieldButton(locked ? EEditorIcon.Lock : EEditorIcon.Unlock, ExtendedObjectFieldButton.EPosition.BeforeField, locked ? "Locked" : "Unlocked", locked ? "Unlock selected asset" : "Lock selected asset", () => { m_Locked = !m_Locked; }, m_Object != null), // Add "Focus" button new ExtendedObjectFieldButton(EEditorIcon.Focus, ExtendedObjectFieldButton.EPosition.AfterField, "Focus asset", () => { EditorHelpers.FocusAsset(m_Object, true, true); }, canFocus), // Add "Create" button new ExtendedObjectFieldButton(EEditorIcon.Add, ExtendedObjectFieldButton.EPosition.AfterField, $"Create new {(m_Object != null ? m_Object.GetType().Name : "Object")} asset", () => { if (m_Object != null) { EditorHelpers.CreateAssetPanel(m_Object.GetType(), out Object newAsset, $"Create new {m_Object.GetType().Name} asset", $"New{m_Object.GetType().Name}", "", "asset", false); if (newAsset != null) { m_Object = newAsset; } } }, canCreate)
/// <summary> /// Draws the Assets List GUI, using Layout methods. /// </summary> public void DrawLayout() { if (!string.IsNullOrEmpty(InfoMessage)) { EditorGUILayout.HelpBox(InfoMessage, InfoMessageVerbosity); } // Draw title and "Refresh" button EditorGUILayout.Space(); using (new GUILayout.HorizontalScope()) { EditorGUILayout.LabelField(Title, EditorStyles.largeLabel); if (GUILayout.Button(EditorGUIUtility.IconContent("Refresh"), GUILayout.Width(EditorGUIUtility.singleLineHeight * 2))) { Refresh(); } } // Draw the assets list T assetToOpen = null; using (new GUILayout.VerticalScope(EditorStyles.helpBox)) { // Draw table headers using (new GUILayout.HorizontalScope()) { EditorGUILayout.GetControlRect(GUILayout.Width(BUTTONS_WIDTH)); EditorGUILayout.LabelField("Name", GUILayout.MaxWidth(ASSET_NAME_MAX_WIDTH)); EditorGUILayout.LabelField("Path"); } MuffinDevGUI.HorizontalLine(); // For each asset of the target type in the project... foreach (T asset in Assets) { // Draw the "Open Asset" button, the name and the project path of the current asset using (new GUILayout.HorizontalScope()) { if (GUILayout.Button(OpenAssetButtonLabel, GUILayout.Width(BUTTONS_WIDTH))) { assetToOpen = asset; } EditorGUILayout.LabelField(asset.name, EditorStyles.boldLabel, GUILayout.MaxWidth(ASSET_NAME_MAX_WIDTH)); EditorGUILayout.LabelField(AssetDatabase.GetAssetPath(asset)); } } // If "Allow Create" option is enabled, draw "Create New Asset" button if (m_AllowCreate && GUILayout.Button(CreateAssetButtonLabel, GUILayout.Width(BUTTONS_WIDTH))) { AssetCreationResult result = EditorHelpers.CreateAssetPanel <T>($"Create new {DisplayableTypeName} asset", $"New{TypeName}", EditorHelpers.ASSETS_FOLDER, EditorHelpers.DEFAULT_ASSET_EXTENSION, false); if (result && m_AutoOpenCreatedAsset) { assetToOpen = result.GetAsset <T>(); Refresh(); } } } // Open the selected asset if required if (assetToOpen != null) { if (m_AutoSelectOpenedAsset) { EditorUtility.FocusProjectWindow(); EditorHelpers.FocusAsset(assetToOpen); } if (OnOpenAsset != null) { OnOpenAsset.Invoke(assetToOpen); } } }