internal static void Gather() { if (inspectorEditorByTypes != null) { return; } HashSet <InspectorEditor> editors = new HashSet <InspectorEditor>(); inspectorEditors = new Dictionary <Type, InspectorEditor>(); inspectorEditorByTypes = new Dictionary <Type, InspectorEditor>(); inspectedType = typeof(CustomEditor).GetField("m_InspectedType", BindingFlags.NonPublic | BindingFlags.Instance); foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type type in assembly.GetTypes()) { if (type.IsAbstract || type.IsGenericType || !typeof(InspectorEditor).IsAssignableFrom(type)) { continue; } if (typeof(ExternalEditor) == type || typeof(BehaviourEditor) == type || typeof(ScriptableEditor) == type) { continue; } InspectorEditor editor = ScriptableObject.CreateInstance(type) as InspectorEditor; editor.hideFlags = HideFlags.HideAndDontSave; editors.Add(editor); object[] attribute = type.GetCustomAttributes(typeof(CustomEditor), true); if (attribute.Length == 0) { continue; } foreach (CustomEditor customEditor in attribute) { Type inspectorType = (Type)inspectedType.GetValue(customEditor); if (!inspectorEditors.ContainsKey(inspectorType)) { inspectorEditors.Add(inspectorType, editor); } } } } foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type type in assembly.GetTypes()) { InspectorEditor editor = GetEditor(type); if (editor != null) { inspectorEditorByTypes.Add(type, editor); } } } foreach (InspectorEditor editor in editors) { DestroyImmediate(editor); } }
public override void Draw(InspectorField field, GUIStyle style) { IPicker picker = field.GetAttribute <IPicker>(); if (picker != null && !picker.IsPickingAvailable(field.Instances, field.GetValues())) { object obj = field.GetValue(); if (field.Mixed) { GUILayout.Label("---"); } else if (obj == null) { GUILayout.Label("None"); } else if (field.OverloadToString) { GUILayout.Label(obj.ToString()); } else if (field.Type != null) { GUILayout.Label(field.Type.Name); } return; } if (validator == null) { validator = typeof(EditorGUI).GetNestedType("ObjectFieldValidator", BindingFlags.NonPublic); } if (doObjectField == null) { doObjectField = typeof(EditorGUI).GetMethod("DoObjectField", BindingFlags.NonPublic | BindingFlags.Static, null, new Type[] { typeof(Rect), typeof(Rect), typeof(int), typeof(UnityEngine.Object), typeof(Type), typeof(SerializedProperty), validator, typeof(bool), typeof(GUIStyle) }, null); } DontAllowSceneObjectAttribute dontAllow = field.GetAttribute <DontAllowSceneObjectAttribute>();; UnityEngine.Object value = (UnityEngine.Object)GetValue(field); EditorGUILayout.BeginHorizontal(); EditorGUI.BeginChangeCheck(); Type type = field.Type; if (value != null && AdvancedInspectorControl.ShowIconPreview) { if (previewIconStyle == null) { previewIconStyle = new GUIStyle(); previewIconStyle.margin = new RectOffset(4, 2, 2, 2); previewIconStyle.padding = new RectOffset(0, 0, 0, 0); } Texture2D preview = AssetPreview.GetAssetPreview(value); if (preview != null) { int previewSize; switch (AdvancedInspectorControl.IconPreviewSize) { case IconPreviewSize.Smallest: previewSize = 16; break; case IconPreviewSize.Small: previewSize = 24; break; case IconPreviewSize.Normal: previewSize = 32; break; case IconPreviewSize.Large: previewSize = 48; break; case IconPreviewSize.Largest: previewSize = 64; break; default: previewSize = 16; break; } GUILayout.Label(preview, previewIconStyle, GUILayout.Width(previewSize), GUILayout.Height(previewSize)); } } UnityEngine.Object result = null; if (type.IsInterface) { Rect position = EditorGUILayout.GetControlRect(false, 16f); int id = GUIUtility.GetControlID(s_ObjectFieldHash, EditorGUIUtility.native, position); Delegate validation = Delegate.CreateDelegate(validator, typeof(ObjectEditor).GetMethod("ValidateObjectFieldAssignment", BindingFlags.NonPublic | BindingFlags.Static)); result = doObjectField.Invoke(null, new object[] { position, position, id, value, type, null, validation, dontAllow == null, EditorStyles.objectField }) as UnityEngine.Object; } else { result = EditorGUILayout.ObjectField(value, type, dontAllow == null); } if (EditorGUI.EndChangeCheck()) { field.SetValue(result); } if (dontAllow == null && (field.Type == typeof(GameObject) || typeof(Component).IsAssignableFrom(field.Type) || field.Type.IsAssignableFrom(typeof(Component)))) { if (GUILayout.Button(Picker, GUIStyle.none, GUILayout.Width(18), GUILayout.Height(18))) { InspectorEditor.StartPicking(Picked, field); } } EditorGUILayout.EndHorizontal(); DrawObjectSelector(field); }
/// <summary> /// Default Inspector entry point. /// </summary> public override void OnInspectorGUI() { if (Instances == null) { return; } current = this; if (advanced) { DrawAdvancedInspector(); } else { DrawDefaultInspector(); } if (picking) { Rect window = EditorWindow.focusedWindow.position; EditorGUIUtility.AddCursorRect(new Rect(0, 0, window.width, window.height), MouseCursor.CustomCursor); if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape) { StopPicking(); Event.current.Use(); } } current = null; if (Instances.Length != 1) { return; } IInspect inspect = Instances[0] as IInspect; if (inspect == null) { return; } object[] inspectInstances = inspect.Inspect; if (inspectInstances == null || inspectInstances.Length == 0) { ClearExtraEditors(); } else { bool refreshEditors = inspectInstances.Length != extraEditors.Count; if (!refreshEditors) { for (int i = 0; i < inspectInstances.Length; i++) { if (inspectInstances[i] is UnityEngine.Object) { if ((UnityEngine.Object)inspectInstances[i] != extraEditors[i].target) { refreshEditors = true; break; } } else { InspectorWrapper wrapper = extraEditors[i].target as InspectorWrapper; if (wrapper == null || wrapper.Tag != inspectInstances[i]) { refreshEditors = true; break; } } } } if (refreshEditors) { ClearExtraEditors(); foreach (object inspectInstance in inspectInstances) { if (inspectInstance == null || inspectInstance.GetType().Namespace == "System") { continue; } UnityEngine.Object unityObject = inspectInstance as UnityEngine.Object; if (unityObject == null) { InspectorWrapper wrapper = CreateInstance <InspectorWrapper>(); wrapper.Tag = inspectInstance; unityObject = wrapper; } extraEditors.Add(CreateEditor(unityObject)); } } foreach (Editor editor in extraEditors) { editor.DrawHeader(); editor.OnInspectorGUI(); } } }