Exemple #1
0
        public static void ReflectionSetComponentField(IReadOnlyLayoutContext context, string fieldName, Type fieldType, Action <object> setter, Component component,
                                                       string value)
        {
            if (fieldType == typeof(string))
            {
                setter(context.ParseString(value));
            }
            else if (fieldType == typeof(int))
            {
                setter(context.ParseInt(value));
            }
            else if (fieldType == typeof(float))
            {
                setter(context.ParseFloat(value));
            }
            else if (fieldType == typeof(Vector2))
            {
                setter(context.ParseVector2(value));
            }
            else if (fieldType == typeof(Vector3))
            {
                setter(context.ParseVector3(value));
            }
            else if (fieldType == typeof(Vector4))
            {
                setter(context.ParseVector4(value));
            }
            else if (fieldType == typeof(Color))
            {
                setter(context.ParseColor(value));
            }
            else if (fieldType.IsEnum)
            {
                var reValue = context.ParseString(value);
                if (reValue.Length == 0)
                {
                    return;
                }

                if (char.IsDigit(reValue[0]))
                {
                    if (!int.TryParse(reValue, out var integer))
                    {
                        Debug.LogError(
                            $"Trying to set field {fieldName} of enum type {fieldType}, but `{reValue}` is not a number");
                    }
                    else
                    {
                        setter(Enum.ToObject(fieldType, integer));
                    }
                }
                else
                {
                    setter(Enum.Parse(fieldType, reValue));
                }
            }
            else if (fieldType.IsSubclassOf(typeof(Object)))
            {
                var asset = context.GetAsset <Object>(value);
                setter(asset);
            }
            else
            {
                Debug.LogError($"Don't know how to set value of field type {fieldType} in {component}/{fieldName}");
            }
        }
        protected override GameObject Install(GameObject go, GridLayoutElement element, IReadOnlyLayoutContext context)
        {
            var gridLayout = go.AddComponent <GridLayoutGroup>();

            if (!string.IsNullOrEmpty(element.Spacing))
            {
                gridLayout.spacing = context.ParseVector2(element.Spacing);
            }

            if (!string.IsNullOrEmpty(element.CellSize))
            {
                gridLayout.cellSize = context.ParseVector2(element.CellSize);
            }
            else
            {
                gridLayout.cellSize = Vector2.one * 250;
            }

            if (!string.IsNullOrEmpty(element.Padding))
            {
                var padding = context.ParsePadding(element.Padding);

                gridLayout.padding.top    = (int)padding.x;
                gridLayout.padding.right  = (int)padding.y;
                gridLayout.padding.bottom = (int)padding.z;
                gridLayout.padding.left   = (int)padding.w;
            }

            if (string.IsNullOrEmpty(element.Align))
            {
                element.Align = "left";
            }

            if (string.IsNullOrEmpty(element.VertAlign))
            {
                element.VertAlign = "top";
            }

            var alignment = ParseUtils.ParseAlignment(element.Align, element.VertAlign);

            if (alignment.HasValue)
            {
                gridLayout.childAlignment = alignment.Value;
            }

            if (!string.IsNullOrEmpty(element.Rows) &&
                !string.IsNullOrEmpty(element.Columns))
            {
                Debug.LogError($"You cannot set both ROWS and COLS in GridLayout!");
            }
            else if (!string.IsNullOrEmpty(element.Rows))
            {
                gridLayout.constraint      = GridLayoutGroup.Constraint.FixedRowCount;
                gridLayout.constraintCount = context.ParseInt(element.Rows);
            }
            else if (!string.IsNullOrEmpty(element.Columns))
            {
                gridLayout.constraint      = GridLayoutGroup.Constraint.FixedColumnCount;
                gridLayout.constraintCount = context.ParseInt(element.Columns);
            }
            else
            {
                gridLayout.constraint = GridLayoutGroup.Constraint.Flexible;
            }

            if (!string.IsNullOrEmpty(element.Axis))
            {
                gridLayout.startAxis = (element.Axis == "vertical")
                    ? GridLayoutGroup.Axis.Vertical
                    : GridLayoutGroup.Axis.Horizontal;
            }

            if (!string.IsNullOrEmpty(element.FitSize))
            {
                var fitsize = context.ParseString(element.FitSize);
                var fitter  = go.AddComponent <ContentSizeFitter>();
                fitter.verticalFit   = fitsize == "both" || fitsize == "vertical" ? ContentSizeFitter.FitMode.PreferredSize : ContentSizeFitter.FitMode.Unconstrained;
                fitter.horizontalFit = fitsize == "both" || fitsize == "horizontal" ? ContentSizeFitter.FitMode.PreferredSize : ContentSizeFitter.FitMode.Unconstrained;
            }

            return(go);
        }