Пример #1
0
        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);
        }