public void DrawFields(Type componentType, object component, FieldInfo[] fields) { OldSettings settings = OldInspectorSidekick.Current.Settings; // Grab the active window's settings for (int j = 0; j < fields.Length; j++) { string fieldName = fields[j].Name; if (!string.IsNullOrEmpty(settings.SearchTerm) && !fieldName.Contains(settings.SearchTerm, StringComparison.InvariantCultureIgnoreCase)) { // Does not match search term, skip it continue; } string metaSuffix = ""; object[] customAttributes = fields[j].GetCustomAttributes(false); if (fields[j].IsPublic || AttributeHelper.IsSerializable(customAttributes)) { metaSuffix += "(SF)"; } if (fields[j].IsStatic) { metaSuffix += "(Static)"; } EditorGUI.BeginChangeCheck(); object newValue = DrawVariable(fields[j].FieldType, fieldName, component != null ? fields[j].GetValue(component) : null, metaSuffix, true, componentType); if (EditorGUI.EndChangeCheck()) { fields[j].SetValue(component, newValue); } } }
public void DrawProperties(Type componentType, object component, PropertyInfo[] properties) { OldSettings settings = OldInspectorSidekick.Current.Settings; // Grab the active window's settings for (int j = 0; j < properties.Length; j++) { if (properties[j].DeclaringType == typeof(Component) || properties[j].DeclaringType == typeof(UnityEngine.Object)) { continue; } if (!string.IsNullOrEmpty(settings.SearchTerm) && !properties[j].Name.Contains(settings.SearchTerm, StringComparison.InvariantCultureIgnoreCase)) { // Does not match search term, skip it continue; } MethodInfo getMethod = properties[j].GetGetMethod(true); MethodInfo setMethod = properties[j].GetSetMethod(true); string metaSuffix = ""; if (setMethod == null) { GUI.enabled = false; } object[] attributes = properties[j].GetCustomAttributes(false); // Don't try to get the value of properties that error on access bool isObsoleteWithError = AttributeHelper.IsObsoleteWithError(attributes); if (getMethod != null && !isObsoleteWithError && !(componentType == typeof(MeshFilter) && properties[j].Name == "mesh")) { object oldValue = getMethod.Invoke(component, null); EditorGUI.BeginChangeCheck(); object newValue = DrawVariable(properties[j].PropertyType, properties[j].Name, oldValue, metaSuffix, true, componentType); if (EditorGUI.EndChangeCheck() && setMethod != null) { setMethod.Invoke(component, new object[] { newValue }); } } else { GUILayout.Label(properties[j].PropertyType + " " + properties[j].Name); } if (setMethod == null) { GUI.enabled = true; } } }
public void Draw(Type componentType, object component) { OldSettings settings = OldInspectorSidekick.Current.Settings; // Grab the active window's settings if (component is MonoScript) { MonoScript monoScript = (MonoScript)component; Type classType = monoScript.GetClass(); if (classType != null) { if (classType.IsSubclassOf(typeof(ScriptableObject))) { if (GUILayout.Button("Instantiate Asset")) { ScriptableObject asset = ScriptableObject.CreateInstance(classType); string fullPath = AssetDatabase.GenerateUniqueAssetPath("Assets/" + classType + ".asset"); AssetDatabase.CreateAsset(asset, fullPath); AssetDatabase.SaveAssets(); } } } } else if (component is GameObject) { GameObject gameObject = (GameObject)component; if (GUILayout.Button("Set Name From First Script")) { MonoBehaviour[] behaviours = gameObject.GetComponents <MonoBehaviour>(); // Attempt to name it after the first script if (behaviours.Length >= 1) { gameObject.name = behaviours[0].GetType().Name; } else { // No scripts found, so name after the first optional component Component[] components = gameObject.GetComponents <Component>(); if (components.Length >= 2) // Ignore Transform { gameObject.name = components[1].GetType().Name; } } } } if (GUILayout.Button("Copy As JSON")) { string json = JsonUtility.ToJson(component, true); EditorGUIUtility.systemCopyBuffer = json; } }
public void DrawMethods(Type componentType, object component, MethodInfo[] methods) { OldSettings settings = OldInspectorSidekick.Current.Settings; // Grab the active window's settings GUIStyle labelStyle = new GUIStyle(GUI.skin.label); labelStyle.alignment = TextAnchor.MiddleRight; GUIStyle normalButtonStyle = new GUIStyle(GUI.skin.button); normalButtonStyle.padding = normalButtonStyle.padding.SetLeft(100); normalButtonStyle.alignment = TextAnchor.MiddleLeft; List <MethodSetup> expandedMethods = OldInspectorSidekick.Current.PersistentData.ExpandedMethods; GUIStyle expandButtonStyle = new GUIStyle(GUI.skin.button); RectOffset padding = expandButtonStyle.padding; padding.left = 0; padding.right = 1; expandButtonStyle.padding = padding; for (int j = 0; j < methods.Length; j++) { MethodInfo method = methods[j]; if (!string.IsNullOrEmpty(settings.SearchTerm) && !method.Name.Contains(settings.SearchTerm, StringComparison.InvariantCultureIgnoreCase)) { // Does not match search term, skip it continue; } // object[] customAttributes = method.GetCustomAttributes(false); EditorGUILayout.BeginHorizontal(); ParameterInfo[] parameters = method.GetParameters(); if (method.ReturnType == typeof(void)) { labelStyle.normal.textColor = Color.grey; } else if (method.ReturnType.IsValueType) { labelStyle.normal.textColor = new Color(0, 0, 1); } else { labelStyle.normal.textColor = new Color32(255, 130, 0, 255); } bool buttonClicked = GUILayout.Button(method.Name + " " + parameters.Length, normalButtonStyle); Rect lastRect = GUILayoutUtility.GetLastRect(); lastRect.xMax = normalButtonStyle.padding.left; GUI.Label(lastRect, TypeUtility.NameForType(method.ReturnType), labelStyle); if (buttonClicked) { object[] arguments = null; if (parameters.Length > 0) { arguments = new object[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { arguments[i] = TypeUtility.GetDefaultValue(parameters[i].ParameterType); } } methodOutput = FireMethod(method, component, arguments); opacity = 1f; } if (parameters.Length > 0) { string methodIdentifier = componentType.FullName + "." + method.Name; bool wasExpanded = expandedMethods.Any(item => item.MethodName == methodIdentifier); bool expanded = GUILayout.Toggle(wasExpanded, "▼", expandButtonStyle, GUILayout.Width(20)); if (expanded != wasExpanded) { if (expanded) { MethodSetup methodSetup = new MethodSetup() { MethodName = methodIdentifier, Values = new object[parameters.Length], }; expandedMethods.Add(methodSetup); } else { expandedMethods.RemoveAll(item => item.MethodName == methodIdentifier); } } EditorGUILayout.EndHorizontal(); if (expanded) { MethodSetup methodSetup = expandedMethods.FirstOrDefault(item => item.MethodName == methodIdentifier); if (methodSetup.Values.Length != parameters.Length) { methodSetup.Values = new object[parameters.Length]; } EditorGUI.indentLevel++; for (int i = 0; i < parameters.Length; i++) { // VariablePane.DrawVariable(parameters[i].ParameterType, parameters[i].Name, GetDefaultValue(parameters[i].ParameterType), "", false); EditorGUI.BeginChangeCheck(); object newValue = VariablePane.DrawVariable(parameters[i].ParameterType, parameters[i].Name, methodSetup.Values[i], "", false, null); if (EditorGUI.EndChangeCheck()) { methodSetup.Values[i] = newValue; } } EditorGUI.indentLevel--; EditorGUILayout.BeginHorizontal(); GUILayout.Space(30); if (GUILayout.Button("Fire")) { methodOutput = FireMethod(method, component, methodSetup.Values); opacity = 1f; } EditorGUILayout.EndHorizontal(); GUILayout.Space(20); } } else { EditorGUILayout.EndHorizontal(); } } }