コード例 #1
0
        public override object OnGUI(Rect _position, GUIContent _label)
        {
            Shape shape = (Shape)Target;

            EditorGUILayout.LabelField(_label);

            shape.ShapeType = (ShapeType)EditorGUILayout.EnumPopup("形状:", shape.ShapeType);
            switch (shape.ShapeType)
            {
            case ShapeType.AABB:
                shape.AABBMin = EditorGUILayout.Vector2Field(GUIHelper.TextContent("Min:"), shape.AABBMin);
                shape.AABBMax = EditorGUILayout.Vector2Field(GUIHelper.TextContent("Max:"), shape.AABBMax);
                break;

            case ShapeType.Circle:
                shape.Center       = EditorGUILayout.Vector2Field(GUIHelper.TextContent("Center:"), shape.Center);
                shape.CircleRadius = EditorGUILayout.FloatField(GUIHelper.TextContent("CircleRadius:"), shape.CircleRadius);
                break;

            case ShapeType.Polygon:
                if (shape.PolygonVertices == null)
                {
                    shape.PolygonVertices = new List <Vector2>();
                }
                int count    = shape.PolygonVertices.Count;
                int newCount = EditorGUILayout.IntField(GUIHelper.TextContent("Size"), count);
                if (count > newCount)
                {
                    for (int i = newCount - 1; i < count - 1; i++)
                    {
                        shape.PolygonVertices.RemoveAt(i);
                    }
                }
                else if (count < newCount)
                {
                    for (int i = count - 1; i < newCount - 1; i++)
                    {
                        shape.PolygonVertices.Add(new Vector2());
                    }
                }
                if (count == newCount)
                {
                    for (int i = 0; i < shape.PolygonVertices.Count; i++)
                    {
                        shape.PolygonVertices[i] = EditorGUILayout.Vector2Field(GUIHelper.TextContent($"Index:{i + 1}"), shape.PolygonVertices[i]);
                    }
                }
                break;

            default:
                break;
            }

            EditorGUILayout.Space(2);
            return(shape);
        }
コード例 #2
0
        /// <summary>
        /// 绘制字段
        /// </summary>
        /// <param name="fieldInfo">字段</param>
        /// <param name="context">字段所属环境</param>
        public static void DrawField(FieldInfo fieldInfo, object context)
        {
            GUIContent label = null;

            if (AttributeHelper.TryGetFieldAttribute(fieldInfo, out TooltipAttribute tooltipAtt))
            {
                label = GUIHelper.TextContent(ObjectNames.NicifyVariableName(fieldInfo.Name), tooltipAtt.tooltip);
            }
            else
            {
                label = GUIHelper.TextContent(ObjectNames.NicifyVariableName(fieldInfo.Name));
            }
            DrawField(fieldInfo, context, label);
        }
コード例 #3
0
 /// <summary>
 /// 绘制字段
 /// </summary>
 /// <param name="fieldInfo">字段</param>
 /// <param name="context">字段所属环境</param>
 public static void DrawField(FieldInfo fieldInfo, object context, string label)
 {
     DrawField(fieldInfo, context, GUIHelper.TextContent(label));
 }
コード例 #4
0
        static object DrawArrayField(Type objType, object value, GUIContent label)
        {
            Type fieldType = objType;

            Type elementType;

            if (fieldType.IsArray)
            {
                elementType = fieldType.GetElementType();
            }
            else
            {
                Type type2 = fieldType;
                while (!type2.IsGenericType)
                {
                    type2 = type2.BaseType;
                }
                elementType = type2.GetGenericArguments()[0];
            }

            IList list;

            if (value == null)
            {
                if (fieldType.IsGenericType || fieldType.IsArray)
                {
                    value = list = (Activator.CreateInstance(typeof(List <>).MakeGenericType(new Type[]
                    {
                        elementType
                    }), true) as IList);
                }
                else
                {
                    value = list = (Activator.CreateInstance(fieldType, true) as IList);
                }
                if (fieldType.IsArray)
                {
                    Array array = Array.CreateInstance(elementType, list.Count);
                    list.CopyTo(array, 0);
                    value = list = array;
                }
                GUI.changed = true;
            }
            else
            {
                list = (IList)value;
            }
            if (DrawFoldout(value.GetHashCode(), label))
            {
                EditorGUI.indentLevel++;
                bool flag     = value.GetHashCode() == editingFieldHash;
                int  count    = (!flag) ? list.Count : savedArraySize;
                int  newCount = EditorGUILayout.IntField(GUIHelper.TextContent("Size"), count);
                if (flag && editingArray && (GUIUtility.keyboardControl != currentKeyboardControl || (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return)))
                {
                    if (newCount != list.Count)
                    {
                        int currentCount = list.Count;
                        if (newCount > currentCount)
                        {
                            if (fieldType.IsArray)
                            {
                                Array array2 = Array.CreateInstance(elementType, newCount);
                                int   num3   = -1;
                                for (int i = 0; i < newCount; i++)
                                {
                                    if (i < list.Count)
                                    {
                                        num3 = i;
                                    }
                                    if (num3 == -1)
                                    {
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                Type type = list.Count > 0 ? list[list.Count - 1].GetType() : elementType;
                                if (!typeof(UnityObject).IsAssignableFrom(type))
                                {
                                    for (int i = currentCount; i < newCount; i++)
                                    {
                                        list.Add(ReflectionHelper.CreateInstance(type));
                                    }
                                }
                                else
                                {
                                    for (int i = currentCount; i < newCount; i++)
                                    {
                                        list.Add(null);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (!fieldType.IsArray)
                            {
                                while (list.Count > newCount)
                                {
                                    list.RemoveAt(list.Count - 1);
                                }
                            }
                        }
                        Event.current.Use();
                    }
                    editingArray     = false;
                    savedArraySize   = -1;
                    editingFieldHash = -1;
                    GUI.changed      = true;
                }
                else if (newCount != count)
                {
                    if (!editingArray)
                    {
                        currentKeyboardControl = GUIUtility.keyboardControl;
                        editingArray           = true;
                        editingFieldHash       = value.GetHashCode();
                    }
                    savedArraySize = newCount;
                }

                for (int k = 0; k < list.Count; k++)
                {
                    object obj = list[k];
                    label.text = $"Element {obj.GetType().Name}" + k;
                    list[k]    = DrawField(obj.GetType(), obj);
                }

                //for (int k = 0; k < list.Count; k++)
                //{
                //    label.text = "Element " + k;
                //    object obj = list[k];
                //    list[k] = DrawField(obj.GetType(), obj, "Element " + k);
                //}

                EditorGUI.indentLevel--;
            }
            return(list);
        }
コード例 #5
0
        public static object DrawField(Type type, object value, string label, string tooltip)
        {
            GUIContent lable = GUIHelper.TextContent(label, tooltip);

            return(DrawField(type, value, lable));
        }
コード例 #6
0
 /// <summary>
 /// 绘制字段
 /// </summary>
 public static object DrawField(Rect rect, Type type, object value, string label, string tooltip)
 {
     return(DrawField(rect, type, value, GUIHelper.TextContent(label, tooltip)));
 }