private void DrawTemplates(ME.ECS.DataConfigs.DataConfig dataConfig) { GUILayoutExt.Separator(6f); GUILayoutExt.DrawHeader("Used Templates:"); GUILayoutExt.Separator(); var usedComponents = new System.Collections.Generic.HashSet <ME.ECS.DataConfigs.DataConfigTemplate>(); if (dataConfig.templates != null) { var rect = new Rect(0f, 0f, EditorGUIUtility.currentViewWidth, 1000f); var style = new GUIStyle("AssetLabel Partial"); var buttonRects = EditorGUIUtility.GetFlowLayoutedRects(rect, style, 4f, 4f, dataConfig.templates.Select(x => { var guid = x; if (string.IsNullOrEmpty(guid) == true) { return(string.Empty); } var path = AssetDatabase.GUIDToAssetPath(guid); if (string.IsNullOrEmpty(path) == true) { return(string.Empty); } var template = AssetDatabase.LoadAssetAtPath <ME.ECS.DataConfigs.DataConfigTemplate>(path); if (template == null) { return(string.Empty); } return(template.name); }).ToList()); GUILayout.BeginHorizontal(); GUILayout.EndHorizontal(); var areaRect = GUILayoutUtility.GetLastRect(); for (int i = 0; i < buttonRects.Count; ++i) { areaRect.height = Mathf.Max(0f, buttonRects[i].yMax); } GUILayoutUtility.GetRect(areaRect.width, areaRect.height); GUI.BeginGroup(areaRect); for (int i = 0; i < dataConfig.templates.Length; ++i) { var guid = dataConfig.templates[i]; if (string.IsNullOrEmpty(guid) == true) { continue; } var path = AssetDatabase.GUIDToAssetPath(guid); if (string.IsNullOrEmpty(path) == true) { continue; } var template = AssetDatabase.LoadAssetAtPath <ME.ECS.DataConfigs.DataConfigTemplate>(path); if (template == null) { continue; } if (usedComponents.Contains(template) == false) { usedComponents.Add(template); } } for (int i = 0; i < dataConfig.templates.Length; ++i) { var guid = dataConfig.templates[i]; if (string.IsNullOrEmpty(guid) == true) { continue; } var path = AssetDatabase.GUIDToAssetPath(guid); if (string.IsNullOrEmpty(path) == true) { continue; } var template = AssetDatabase.LoadAssetAtPath <ME.ECS.DataConfigs.DataConfigTemplate>(path); if (template == null) { continue; } if (GUI.Button(buttonRects[i], template.name, style) == true) { EditorGUIUtility.PingObject(template); //this.RemoveTemplate(dataConfig, template, usedComponents); } } GUI.EndGroup(); } GUILayoutExt.DrawManageDataConfigTemplateMenu(usedComponents, (template, isUsed) => { var path = AssetDatabase.GetAssetPath(template); var guid = AssetDatabase.AssetPathToGUID(path); if (string.IsNullOrEmpty(guid) == true) { return; } if (isUsed == true) { usedComponents.Remove(template); for (int i = 0; i < dataConfig.templates.Length; ++i) { if (dataConfig.templates[i] == guid) { this.RemoveTemplate(dataConfig, template, usedComponents); break; } } } else { usedComponents.Add(template); if (dataConfig.templates == null) { dataConfig.templates = new string[0]; } System.Array.Resize(ref dataConfig.templates, dataConfig.templates.Length + 1); dataConfig.templates[dataConfig.templates.Length - 1] = guid; dataConfig.AddTemplate(template); dataConfig.OnScriptLoad(); this.Save(dataConfig); AssetDatabase.ForceReserializeAssets(new [] { AssetDatabase.GetAssetPath(dataConfig) }, ForceReserializeAssetsOptions.ReserializeAssetsAndMetadata); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(dataConfig), ImportAssetOptions.ForceUpdate); AssetDatabase.SaveAssets(); } }); }
private static IMGUIContainer CreateTemplatesButton(DataConfigEditor editor, System.Collections.Generic.HashSet <ME.ECS.DataConfigs.DataConfigTemplate> usedComponents, VisualElement rootElement, VisualElement templatesContainer, SerializedProperty source, SerializedObject so, System.Action <SerializedObject, ME.ECS.DataConfigs.DataConfigTemplate> onAddTemplate, System.Action <SerializedObject, ME.ECS.DataConfigs.DataConfigTemplate> onRemoveTemplate) { var container = new IMGUIContainer(() => { GUILayoutExt.DrawManageDataConfigTemplateMenu(usedComponents, (template, isUsed) => { var path = AssetDatabase.GetAssetPath(template); var guid = AssetDatabase.AssetPathToGUID(path); if (string.IsNullOrEmpty(guid) == true) { return; } if (isUsed == true) { var copy = source.Copy(); var i = 0; var enterChildren = true; while (copy.NextVisible(enterChildren) == true) { enterChildren = false; if (copy.propertyType != SerializedPropertyType.String) { continue; } if (copy.stringValue == guid) { usedComponents.Remove(template); source.DeleteArrayElementAtIndex(i); so.ApplyModifiedProperties(); onRemoveTemplate.Invoke(so, template); break; } ++i; } } else { usedComponents.Add(template); onAddTemplate.Invoke(so, template); ++source.arraySize; var elem = source.GetArrayElementAtIndex(source.arraySize - 1); elem.stringValue = guid; so.ApplyModifiedProperties(); } editor.Save(); BuildContainer(editor, rootElement, so); }); }); container.AddToClassList("add-template-menu-button-imgui"); return(container); }