public static void VariableField(DashVariables p_variables, string p_name, GameObject p_boundObject, float p_maxWidth) { var variable = p_variables.GetVariable(p_name); GUILayout.BeginHorizontal(); string newName = EditorGUILayout.TextField(p_name, GUILayout.Width(120)); GUILayout.Space(2); if (newName != p_name) { p_variables.RenameVariable(p_name, newName); } variable.ValueField(p_maxWidth - 150, p_boundObject); var oldColor = GUI.color; GUI.color = variable.IsBound || variable.IsLookup ? Color.yellow : Color.gray; GUILayout.FlexibleSpace(); GUILayout.BeginVertical(GUILayout.Width(16)); GUILayout.Space(2); if (GUILayout.Button(IconManager.GetIcon("bind_icon"), GUIStyle.none, GUILayout.Height(16), GUILayout.Width(16))) { var menu = GetVariableMenu(p_variables, p_name, p_boundObject); GenericMenuPopup.Show(menu, "", Event.current.mousePosition, 240, 300, false, false); } GUILayout.EndVertical(); GUI.color = oldColor; GUILayout.EndHorizontal(); }
static Dictionary <Component, List <FieldInfo> > GetBindableFields(DashVariables p_variables, string p_name, GameObject p_boundObject) { Dictionary <Component, List <FieldInfo> > bindableFields = new Dictionary <Component, List <FieldInfo> >(); var variable = p_variables.GetVariable(p_name); if (p_boundObject != null) { Component[] components = p_boundObject.GetComponents <Component>(); foreach (Component component in components) { Type componentType = component.GetType(); FieldInfo[] fields = componentType.GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (FieldInfo field in fields) { if (IsFieldBindable(variable, field)) { if (!bindableFields.ContainsKey(component)) { bindableFields.Add(component, new List <FieldInfo>()); } bindableFields[component].Add(field); } } } } return(bindableFields); }
static Dictionary <Component, List <PropertyInfo> > GetBindableProperties(DashVariables p_variables, string p_name, GameObject p_boundObject) { Dictionary <Component, List <PropertyInfo> > bindableProperties = new Dictionary <Component, List <PropertyInfo> >(); var variable = p_variables.GetVariable(p_name); if (p_boundObject != null) { Component[] components = p_boundObject.GetComponents <Component>(); foreach (Component component in components) { Type componentType = component.GetType(); PropertyInfo[] properties = componentType.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo property in properties) { if (IsPropertyBindable(variable, property)) { if (!bindableProperties.ContainsKey(component)) { bindableProperties.Add(component, new List <PropertyInfo>()); } bindableProperties[component].Add(property); } } } } return(bindableProperties); }
public static void DrawVariablesInspector(string p_title, DashVariables p_variables, GameObject p_boundObject) { var style = new GUIStyle(); style.normal.textColor = new Color(1, 0.7f, 0); style.alignment = TextAnchor.MiddleCenter; style.fontStyle = FontStyle.Bold; style.normal.background = Texture2D.whiteTexture; style.fontSize = 16; GUI.backgroundColor = new Color(0, 0, 0, .5f); GUILayout.Label(p_title, style, GUILayout.Height(28)); GUI.backgroundColor = Color.white; int index = 0; p_variables.variables?.ForEach(variable => { VariableField(p_variables, variable.Name, p_boundObject, EditorGUIUtility.currentViewWidth - 20); GUILayout.Space(4); index++; }); if (GUILayout.Button("Add Variable")) { TypesMenu.Show((type) => OnAddNewVariable(p_variables, type)); } }
static RuntimeGenericMenu GetVariableMenu(DashVariables p_variables, string p_name, GameObject p_boundObject) { RuntimeGenericMenu menu = new RuntimeGenericMenu(); var variable = p_variables.GetVariable(p_name); if (variable.IsBound) { menu.AddItem(new GUIContent("Unbind"), false, () => variable.UnbindProperty()); } else { if (p_boundObject != null && !variable.IsLookup) { Dictionary <Component, List <PropertyInfo> > bindableProperties = GetBindableProperties(p_variables, p_name, p_boundObject); foreach (var infoKeys in bindableProperties) { foreach (PropertyInfo property in infoKeys.Value) { menu.AddItem(new GUIContent("Bind (Controller)/" + infoKeys.Key.name + "/" + property.Name), false, () => OnBindVariable(variable, property, infoKeys.Key)); } } Dictionary <Component, List <FieldInfo> > bindableFields = GetBindableFields(p_variables, p_name, p_boundObject); foreach (var infoKeys in bindableFields) { foreach (FieldInfo field in infoKeys.Value) { menu.AddItem(new GUIContent("Bind (Controller)/" + infoKeys.Key.name + "/" + field.Name), false, () => OnBindVariable(variable, field, infoKeys.Key)); } } } if (variable.IsLookup) { menu.AddItem(new GUIContent("Unset as Lookup"), false, () => OnLookupVariable(p_variables, p_name)); } else { menu.AddItem(new GUIContent("Set as Lookup"), false, () => OnLookupVariable(p_variables, p_name)); } } menu.AddItem(new GUIContent("Delete Variable"), false, () => OnDeleteVariable(p_variables, p_name, p_boundObject)); menu.AddItem(new GUIContent("Copy Variable"), false, () => OnCopyVariable(p_variables, p_name)); return(menu); }
void OnAddVariable(DashVariables p_variables, Type p_type) { string name = "new" + p_type.ToString().Substring(p_type.ToString().LastIndexOf(".") + 1); int index = 0; while (p_variables.HasVariable(name + index)) { index++; } p_variables.AddVariableByType((Type)p_type, name + index, null); DashEditorCore.SetDirty(); }
static void OnAddNewVariable(DashVariables p_variables, Type p_type) { p_variables.AddNewVariable(p_type); // EditorUtility.SetDirty(target); // PrefabUtility.RecordPrefabInstancePropertyModifications(target); }
static void OnCopyVariable(DashVariables p_variables, string p_name) { VariableUtils.CopyVariable(p_variables.GetVariable(p_name)); }
static void OnLookupVariable(DashVariables p_variables, string p_name) { var variable = p_variables.GetVariable(p_name); variable.SetAsLookup(!variable.IsLookup); }
static void OnDeleteVariable(DashVariables p_variables, string p_name, GameObject p_boundObject) { p_variables.RemoveVariable(p_name); EditorUtility.SetDirty(p_boundObject); }
protected void DrawVariablesGUI(Vector2 p_position, bool p_global, Color p_color, DashVariables p_variables, ref bool p_minimized, GameObject p_boundObject) { Rect rect = new Rect(p_position.x, p_position.y, 380, p_minimized ? 32 : 200); DrawBoxGUI(rect, p_global ? "Global Variables" : "Graph Variables", TextAnchor.UpperCenter, p_color); var minStyle = new GUIStyle(); minStyle.normal.textColor = Color.white; minStyle.fontStyle = FontStyle.Bold; minStyle.fontSize = 20; if (GUI.Button(new Rect(rect.x + rect.width - 20 + (p_minimized ? 0 : 2), rect.y + 2, 20, 20), p_minimized ? "+" : "-", minStyle)) { p_minimized = !p_minimized; GUI.FocusControl(""); } if (p_minimized) { return; } if (p_global && PrefabUtility.GetPrefabInstanceStatus(p_boundObject) != PrefabInstanceStatus.NotAPrefab) { var style = new GUIStyle(); style.alignment = TextAnchor.MiddleCenter; style.normal.textColor = Color.white; style.fontSize = 20; style.wordWrap = true; EditorGUI.TextArea(new Rect(rect.x + 5, rect.y + 30, rect.width - 10, rect.height - 30), "Global variables on prefab instances are not supported!", style); return; } GUILayout.BeginArea(new Rect(rect.x + 5, rect.y + 30, rect.width - 10, rect.height - 79)); scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, false); EditorGUI.BeginChangeCheck(); if (p_variables != null) { int index = 0; foreach (var variable in p_variables) { GUIVariableUtils.VariableField(p_variables, variable.Name, p_boundObject, rect.width - 10); EditorGUILayout.Space(4); index++; } } GUILayout.EndScrollView(); GUILayout.EndArea(); if (GUI.Button(new Rect(rect.x + 4, rect.y + rect.height - 48, rect.width - 8, 20), "Add Variable")) { TypesMenu.Show((type) => OnAddVariable(p_variables, type)); } if (GUI.Button(new Rect(rect.x + 4, rect.y + rect.height - 24, rect.width / 2 - 6, 20), "Copy Variables")) { VariableUtils.CopyVariables(p_variables); } if (GUI.Button(new Rect(rect.x + rect.width / 2 + 2, rect.y + rect.height - 24, rect.width / 2 - 6, 20), "Paste Variables")) { VariableUtils.PasteVariables(p_variables, p_boundObject); } if (EditorGUI.EndChangeCheck()) { DashEditorCore.SetDirty(); } UseEvent(new Rect(rect.x, rect.y, rect.width, rect.height)); }