Exemple #1
0
        private static void InstallAnimations(RectTransformElement rte, GameObject gameObject, IReadOnlyLayoutContext context)
        {
            AnimationContext animationContext = new AnimationContext();

            List <(UIAnimation, AnimationElement)> listOfAnimations = new List <(UIAnimation, AnimationElement)>();

            foreach (var anim in rte.Animations)
            {
                var         constructor = Constructors.GetAnimationConstructor(anim);
                UIAnimation animation   = constructor.Install(gameObject, anim, context);

                if (!string.IsNullOrEmpty(anim.Key))
                {
                    animationContext.AddAnimation(context.ParseString(anim.Key), animation);
                }

                listOfAnimations.Add((animation, anim));
            }

            foreach (var pair in listOfAnimations)
            {
                var anim      = pair.Item2;
                var animation = pair.Item1;
                foreach (var trigger in anim.Triggers)
                {
                    var       constructor      = Constructors.GetTriggerConstructor(trigger);
                    UITrigger triggerComponent = constructor.Install(gameObject, trigger, context, animationContext);

                    triggerComponent.conditions = ParseConditions(trigger.Conditions, gameObject, context);
                    triggerComponent.instant    = context.ParseBool(trigger.Instant);
                    triggerComponent.animation  = animation;
                }
            }
        }
Exemple #2
0
        protected override GameObject Install(GameObject go, LabelElement element, IReadOnlyLayoutContext context)
        {
            var rect         = go.GetComponent <RectTransform>();
            var oldOffsetMin = rect.offsetMin;
            var oldOffsetMax = rect.offsetMax;

            var textMesh = go.AddComponent <TextMeshProUGUI>();

            textMesh.text     = context.ParseString(element.Text);
            textMesh.fontSize = context.ParseFloat(element.FontSize);

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

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

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

            if (alignment.HasValue)
            {
                textMesh.alignment = alignment.Value;
            }

            textMesh.color = context.ParseColor(element.Color);

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

            rect.offsetMin = oldOffsetMin;
            rect.offsetMax = oldOffsetMax;

            return(go);
        }
Exemple #3
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);
        }
Exemple #5
0
        private static void ApplyTransformSettings(RectTransformElement element, Transform parent,
                                                   IReadOnlyLayoutContext context, GameObject go)
        {
            var rect = go.GetComponent <RectTransform>();

            var dock = context.ParseString(element.Dock);

            if (dock == "fill")
            {
                element.AnchorX = element.AnchorY = "(0, 1)";
            }
            else if (dock == "left")
            {
                element.AnchorX = "(0, 0)";
                element.AnchorY = "(0, 1)";
                element.Pivot   = "(0, 0.5)";
            }
            else if (dock == "right")
            {
                element.AnchorX = "(1, 1)";
                element.AnchorY = "(0, 1)";
                element.Pivot   = "(1, 0.5)";
            }
            else if (dock == "top")
            {
                element.AnchorX = "(0, 1)";
                element.AnchorY = "(1, 1)";
                element.Pivot   = "(0.5, 1)";
            }
            else if (dock == "bottom")
            {
                element.AnchorX = "(0, 1)";
                element.AnchorY = "(0, 0)";
                element.Pivot   = "(0.5, 0)";
            }

            if (!string.IsNullOrEmpty(element.AnchorX))
            {
                rect.anchorMin = new Vector2(context.ParseVector2(element.AnchorX).x, rect.anchorMin.y);
                rect.anchorMax = new Vector2(context.ParseVector2(element.AnchorX).y, rect.anchorMax.y);
            }

            if (!string.IsNullOrEmpty(element.AnchorY))
            {
                rect.anchorMin = new Vector2(rect.anchorMin.x, context.ParseVector2(element.AnchorY).x);
                rect.anchorMax = new Vector2(rect.anchorMax.x, context.ParseVector2(element.AnchorY).y);
            }

            if (!string.IsNullOrEmpty(element.Pivot))
            {
                rect.pivot = context.ParseVector2(element.Pivot);
            }

            rect.offsetMax = new Vector2(0, 0);
            rect.offsetMin = Vector2.zero;

            if (!string.IsNullOrEmpty(element.Top))
            {
                if (Mathf.Abs(rect.anchorMin.y - rect.anchorMax.y) > 0.01f)
                {
                    rect.offsetMax = new Vector2(rect.offsetMax.x, -context.ParseFloat(element.Top));
                }
                else
                {
                    Debug.LogError("Property Top cannot work if AnchorY is single value");
                }
            }

            if (!string.IsNullOrEmpty(element.Bottom))
            {
                if (Mathf.Abs(rect.anchorMin.y - rect.anchorMax.y) > 0.01f)
                {
                    rect.offsetMin = new Vector2(rect.offsetMin.x, context.ParseFloat(element.Bottom));
                }
                else
                {
                    Debug.LogError("Property Bottom cannot work if AnchorY is single value");
                }
            }


            if (!string.IsNullOrEmpty(element.Left))
            {
                if (Mathf.Abs(rect.anchorMin.x - rect.anchorMax.x) > 0.01f)
                {
                    rect.offsetMin = new Vector2(context.ParseFloat(element.Left), rect.offsetMin.y);
                }
                else
                {
                    Debug.LogError("Property Left cannot work if AnchorX is single value");
                }
            }

            if (!string.IsNullOrEmpty(element.Right))
            {
                if (Mathf.Abs(rect.anchorMin.x - rect.anchorMax.x) > 0.01f)
                {
                    rect.offsetMax = new Vector2(-context.ParseFloat(element.Right), rect.offsetMax.y);
                }
                else
                {
                    Debug.LogError("Property Right cannot work if AnchorX is single value");
                }
            }

            if (!string.IsNullOrEmpty(element.Width))
            {
                if (parent.gameObject.GetComponent <HorizontalOrVerticalLayoutGroup>()?.childControlWidth ?? false)
                {
                    var layoutElement = go.AddComponent <LayoutElement>();
                    if (element.Width == "fill")
                    {
                        layoutElement.flexibleWidth = 1;
                    }
                    else
                    {
                        layoutElement.minWidth = context.ParseFloat(element.Width);
                    }
                }
                else
                {
                    rect.sizeDelta = new Vector2(context.ParseFloat(element.Width), rect.sizeDelta.y);
                }
            }

            if (!string.IsNullOrEmpty(element.Height))
            {
                if (parent.gameObject.GetComponent <HorizontalOrVerticalLayoutGroup>()?.childControlHeight ?? false)
                {
                    var layoutElement = go.AddComponent <LayoutElement>();
                    if (element.Height == "fill")
                    {
                        layoutElement.flexibleHeight = 1;
                    }
                    else
                    {
                        layoutElement.minHeight = context.ParseFloat(element.Height);
                    }
                }
                else
                {
                    rect.sizeDelta = new Vector2(rect.sizeDelta.x, context.ParseFloat(element.Height));
                }
            }


            if (!string.IsNullOrEmpty(element.Offset))
            {
                var offset = context.ParseVector2(element.Offset);
                rect.anchoredPosition += new Vector2(offset.x, -offset.y);
            }
        }