Beispiel #1
0
        private static UICondition[] ParseConditions(List <ConditionElement> conditionList, GameObject go,
                                                     IReadOnlyLayoutContext context)
        {
            var conditions = new UICondition[conditionList.Count];

            for (int i = 0; i < conditionList.Count; ++i)
            {
                UICondition uiCondition = null;
                var         condition   = conditionList[i];

                if (condition is ConditionPointerOverElement)
                {
                    uiCondition = go.AddComponent <PointerIsOverCondition>();
                }
                else if (condition is ConditionNotElement notElement)
                {
                    var not = go.AddComponent <InverseCondition>();
                    not.originals = ParseConditions(notElement.Conditions, go, context);
                    Debug.Assert(not.originals.Length == 1);
                    uiCondition = not;
                }

                conditions[i] = uiCondition;
            }

            return(conditions);
        }
Beispiel #2
0
 protected override void PostInstall(GameObject go, ScrollElement element, IReadOnlyLayoutContext context)
 {
     if (go.transform.childCount != 1 || go.transform.GetChild(0).childCount != 1)
     {
         Debug.LogError("Scroll element should have exactly one child");
     }
     else
     {
         go.GetComponent <ScrollRect>().content = go.transform.GetChild(0).GetChild(0).GetComponent <RectTransform>();
     }
 }
Beispiel #3
0
        protected override GameObject Install(GameObject go, ImageElement element, IReadOnlyLayoutContext context)
        {
            var img = go.AddComponent <Image>();

            img.color          = context.ParseColor(element.Color);
            img.sprite         = context.GetAsset <Sprite>(element.Image);
            img.preserveAspect = context.ParseBool(element.PreserveAspect);
            img.material       = context.GetAsset <Material>(element.Material);

            return(go);
        }
Beispiel #4
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);
        }
Beispiel #5
0
        protected override GameObject Install(GameObject go, ScrollElement element, IReadOnlyLayoutContext context)
        {
            var scroll   = go.AddComponent <ScrollRect>();
            var viewPort = new GameObject($"Viewport ({go.name})");

            viewPort.transform.parent = scroll.transform;

            var viewPortRect = viewPort.AddComponent <RectTransform>();

            viewPortRect.localScale = Vector3.one;

            viewPortRect.anchorMin = Vector2.zero;
            viewPortRect.anchorMax = Vector2.one;

            viewPortRect.offsetMax     =
                viewPortRect.offsetMin = Vector2.zero;

            scroll.viewport = viewPortRect;

            var mask = viewPort.gameObject.AddComponent <RectMask2D>();

            return(viewPort);
        }
        protected override GameObject Install(GameObject go, HorizontalOrVerticalLayoutElement horzOrVert, IReadOnlyLayoutContext context)
        {
            ContentSizeFitter fitter = null;

            if (context.ParseBool(horzOrVert.FitSize))
            {
                fitter = go.AddComponent <ContentSizeFitter>();
            }

            HorizontalOrVerticalLayoutGroup group = null;

            if (horzOrVert is VerticalLayoutElement vert)
            {
                group = go.AddComponent <VerticalLayoutGroup>();
                group.childControlHeight = group.childControlWidth = context.ParseBool(vert.Flex);
                if (fitter != null)
                {
                    fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
                }
            }
            else if (horzOrVert is HorizontalLayoutElement hor)
            {
                group = go.AddComponent <HorizontalLayoutGroup>();
                group.childControlHeight = group.childControlWidth = context.ParseBool(hor.Flex);

                if (fitter != null)
                {
                    fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
                }
            }

            var padding = ParseUtils.ParsePadding(horzOrVert.Padding);

            group.padding                = new RectOffset((int)padding.w, (int)padding.y, (int)padding.x, (int)padding.z);
            group.spacing                = context.ParseFloat(horzOrVert.Spacing);
            group.childControlWidth     |= context.ParseBool(horzOrVert.ExpandWidth);
            group.childForceExpandWidth  = context.ParseBool(horzOrVert.ExpandWidth);
            group.childForceExpandHeight = context.ParseBool(horzOrVert.ExpandHeight);
            group.childControlHeight    |= context.ParseBool(horzOrVert.ExpandHeight);

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

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

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

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

            return(go);
        }
Beispiel #7
0
 protected override GameObject Install(GameObject go, ListViewElement element, IReadOnlyLayoutContext context)
 {
     xLayouter.BuildLayout(go, element.ChildItem.Elements, context);
     return(go);
 }
Beispiel #8
0
        protected override GameObject Install(GameObject go, RawImageElement element, IReadOnlyLayoutContext context)
        {
            var img = go.AddComponent <RawImage>();

            img.color    = context.ParseColor(element.Color);
            img.texture  = context.GetAsset <Texture2D>(element.Image);
            img.material = context.GetAsset <Material>(element.Material);

            return(go);
        }
Beispiel #9
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;
                }
            }
        }
Beispiel #10
0
        private static GameObject CreateElement(RectTransformElement element, Transform parent, IReadOnlyLayoutContext context, out GameObject newParent)
        {
            GameObject go = null;
            List <RectTransformElement> originalElements = null;

            while (element is PrefabElement prefab)
            {
                var prefabElement = context.GetPrefab(prefab.Prefab);
                if (prefabElement != null)
                {
                    if (originalElements == null)
                    {
                        originalElements = new List <RectTransformElement>();
                    }

                    originalElements.Add(element);

                    element = prefabElement.Content.Elements[0] as RectTransformElement;
                }
            }

            if (element is GameObjectElement gameobject)
            {
                go      = PrefabUtility.InstantiatePrefab(context.GetAsset <GameObject>(gameobject.Path)) as GameObject;
                go.name = element.Name;
            }
            else
            {
                go = new GameObject(element.Name);
            }
            go.transform.parent     = parent;
            go.transform.localScale = Vector3.one;

            go.AddComponent <ExternalLayoutWarning>();

            newParent = go;

            if (element.Active == "false")
            {
                go.SetActive(false);
            }

            var rect = go.GetComponent <RectTransform>();

            if (rect == null)
            {
                rect = go.AddComponent <RectTransform>();
            }

            if (!string.IsNullOrEmpty(element.Padding))
            {
                var        padding = context.ParsePadding(element.Padding);
                GameObject padder  = new GameObject();
                padder.name                 = $"Padding ({go.name})";
                padder.transform.parent     = rect;
                padder.transform.localScale = Vector3.one;
                rect = padder.AddComponent <RectTransform>();

                rect.anchorMin = Vector2.zero;
                rect.anchorMax = Vector2.one;

                rect.offsetMin = new Vector2(padding.w, padding.z);
                rect.offsetMax = new Vector2(-padding.y, -padding.x);

                newParent = padder;
            }

            ApplyTransformSettings(element, parent, context, go);

            if (originalElements != null)
            {
                for (int i = originalElements.Count - 1; i >= 0; --i)
                {
                    var originalElement = originalElements[i];
                    ApplyTransformSettings(originalElement, parent, context, go);
                    if (!string.IsNullOrEmpty(originalElement.Name))
                    {
                        go.name = originalElement.Name;
                    }
                }
            }
            return(go);
        }
Beispiel #11
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}");
            }
        }
Beispiel #12
0
 public abstract UITrigger Install(GameObject gameObject, TriggerElement element, IReadOnlyLayoutContext context, IReadOnlyAnimationContext animationContext);
Beispiel #13
0
 protected override void PostInstall(GameObject go, GridLayoutElement element, IReadOnlyLayoutContext context)
 {
     go.GetComponent <GridLayoutGroup>().CalculateLayoutInputVertical();
     go.GetComponent <GridLayoutGroup>().CalculateLayoutInputHorizontal();
 }
Beispiel #14
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);
            }
        }
        protected override UITrigger Install(GameObject go, OnAnimationFinishedTriggerElement element, IReadOnlyLayoutContext context, IReadOnlyAnimationContext animationContext)
        {
            var finishTriggerComponent = go.AddComponent <OnAnimationFinishedPlayAnimation>();

            var otherAnimation = animationContext.FindAnimation(element.Animation);

            if (otherAnimation == null)
            {
                Debug.LogError("Cannot find animation with key " + element.Animation);
            }

            finishTriggerComponent.otherAnimation = otherAnimation;

            return(finishTriggerComponent);
        }
Beispiel #16
0
        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);
        }
Beispiel #17
0
        private static void InstallBindings(Dictionary <string, GameObject> byKeys, ComponentElement element, System.Type monoBehaviourType, Component component, IReadOnlyLayoutContext context)
        {
            foreach (var componentBinding in element.Bindings)
            {
                PropertyInfo property = null;
                var          field    = monoBehaviourType.GetField(componentBinding.Field,
                                                                   BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

                Action <object> setValue = v => field.SetValue(component, v);

                if (field == null)
                {
                    property = monoBehaviourType.GetProperty(componentBinding.Field,
                                                             BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

                    if (property == null)
                    {
                        Debug.LogError(
                            $"Component {monoBehaviourType.FullName} doesn't have field {componentBinding.Field}");
                        continue;
                    }

                    setValue = v => property.SetValue(component, v);
                }

                string fieldName = field?.Name ?? property.Name;
                Type   fieldType = field?.FieldType ?? property.PropertyType;

                if (componentBinding is BindingElement binding)
                {
                    if (!byKeys.TryGetValue(binding.Source, out var source))
                    {
                        Debug.LogError($"Trying to bind object of key {binding.Source} but not found");
                        continue;
                    }

                    if (field.FieldType.IsSubclassOf(typeof(Component)))
                    {
                        var sourceComponent = source.GetComponent((System.Type)field.FieldType);

                        if (sourceComponent == null)
                        {
                            Debug.LogError($"Component {field.FieldType} not found on {source}", source);
                        }
                        else
                        {
                            setValue(sourceComponent);
                        }
                    }
                    else if (field.FieldType == typeof(GameObject))
                    {
                        setValue(source);
                    }
                    else
                    {
                        Debug.LogError($"Cannot bind type {field.FieldType}");
                    }
                }
                else if (componentBinding is ComponentSetterElement setter)
                {
                    ReflectionUtils.ReflectionSetComponentField(context, fieldName, fieldType, setValue, component, setter.Value);
                }
            }
        }
Beispiel #18
0
        private static void InstallComponents(RectTransformElement element, GameObject gameObject, Dictionary <string, GameObject> byKeys, IReadOnlyLayoutContext context)
        {
            if (!string.IsNullOrEmpty(element.Key))
            {
                if (byKeys.ContainsKey(element.Key))
                {
                    Debug.LogError($"Duplicate key: {element.Key}. Key is supposed to be unique in whole XML, thus it is different than name");
                }
                byKeys[element.Key] = gameObject;
            }
            byKeys["{this}"] = gameObject;

            if (element.Components != null)
            {
                foreach (var componentElement in element.Components)
                {
                    var type = ComponentInstaller.GetMonoBehaviourTypeOrLogError(componentElement.ComponentType);
                    if (type == null)
                    {
                        continue;
                    }

                    var component = gameObject.AddComponent(type);

                    if (componentElement.Bindings != null)
                    {
                        InstallBindings(byKeys, componentElement, type, component, context);
                    }
                }
            }
        }
Beispiel #19
0
 public static void BuildLayout(GameObject parent, IEnumerable <BaseElement> children, IReadOnlyLayoutContext context)
 {
     foreach (var child in children)
     {
         Deserialize(child, parent, context);
     }
 }
 protected override void PostInstall(GameObject go, HorizontalOrVerticalLayoutElement element, IReadOnlyLayoutContext context)
 {
     LayoutRebuilder.ForceRebuildLayoutImmediate(go.GetComponent <RectTransform>());
 }
Beispiel #21
0
 public abstract UIAnimation Install(GameObject gameObject, AnimationElement element, IReadOnlyLayoutContext context);
Beispiel #22
0
 protected override GameObject Install(GameObject go, EmptyElement element, IReadOnlyLayoutContext context)
 {
     return(go);
 }
Beispiel #23
0
        protected override GameObject Install(GameObject go, GameObjectElement element, IReadOnlyLayoutContext context)
        {
            foreach (var setter in element.Setters)
            {
                var obj = DecodePath(go, setter.Path);

                if (obj == null)
                {
                    Debug.LogError($"Cannot decode path {setter.Path} for gameobject {go}", go);
                    continue;
                }

                var component = obj.GetComponent(setter.Component);

                if (component == null)
                {
                    Debug.LogError($"Cannot find component `{setter.Component}` on {obj}", obj);
                    continue;
                }

                var field = component.GetType().GetField(setter.Field,
                                                         BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

                if (field == null)
                {
                    Debug.LogError($"Cannot find field `{setter.Field}` on component {component}", obj);
                    continue;
                }
                ReflectionUtils.ReflectionSetComponentField(context, field.Name, field.FieldType, v => field.SetValue(component, v), component, setter.Value);
            }
            return(go);
        }
Beispiel #24
0
 public abstract void PostInstall(GameObject gameObject, BaseElement element, IReadOnlyLayoutContext context);
Beispiel #25
0
        protected override UIAnimation Install(GameObject go, CanvasAlphaAnimationElement element, IReadOnlyLayoutContext context)
        {
            CanvasGroup cg             = go.EnsureComponent <CanvasGroup>();
            var         alphaAnimation = go.AddComponent <CanvasAlphaAnimation>();

            alphaAnimation.Setup(cg, context.ParseFloat(element.DestValue));

            return(alphaAnimation);
        }
Beispiel #26
0
 protected override UITrigger Install(GameObject go, OnPointerUpTriggerElement element, IReadOnlyLayoutContext context, IReadOnlyAnimationContext animationContext)
 {
     return(go.AddComponent <OnPointerUpPlayAnimation>());
 }
Beispiel #27
0
        private static Dictionary <string, GameObject> Deserialize(BaseElement element, GameObject gameObject, IReadOnlyLayoutContext context)
        {
            GameObject parent = gameObject;
            Dictionary <string, GameObject> byKeys = new Dictionary <string, GameObject>();

            if (element is RectTransformElement rte)
            {
                gameObject = CreateElement(rte, gameObject.transform, context, out parent);
            }
            while (element is PrefabElement prefab)
            {
                var prefabElement = context.GetPrefab(prefab.Prefab);
                if (prefabElement != null)
                {
                    if (!string.IsNullOrEmpty(prefab.Key))
                    {
                        byKeys[prefab.Key] = gameObject;
                    }

                    if ((prefabElement.Properties?.Count ?? 0) > 0 ||
                        (prefab.Properties?.Count ?? 0) > 0 ||
                        !string.IsNullOrEmpty(prefab.Property))
                    {
                        var newContext = new LayoutContext();
                        newContext.MergeResource((LayoutContext)context); // haaack :S

                        if (prefabElement.Properties != null)
                        {
                            foreach (var property in prefabElement.Properties)
                            {
                                if (property.HasDefault)
                                {
                                    newContext.AddProperty(property.Name, property.Default);
                                }
                            }
                        }

                        if (prefab.Properties != null)
                        {
                            foreach (var property in prefab.Properties)
                            {
                                newContext.AddProperty(property.Name, property.Value);
                            }
                        }

                        if (!string.IsNullOrEmpty(prefab.Property))
                        {
                            var colon = prefab.Property.IndexOf(":");
                            if (colon == -1)
                            {
                                Debug.LogError("Attribute Property in Prefab element is expected to match [a-zA-Z0-9_]+:.*");
                            }
                            else
                            {
                                var prop  = prefab.Property.Substring(0, colon);
                                var value = prefab.Property.Substring(colon + 1);
                                newContext.AddProperty(prop, value);
                            }
                        }

                        context = newContext;
                    }

                    element = prefabElement.Content.Elements[0];
                }
            }

            var oldParent   = parent;
            var constructor = Constructors.GetConstructor(element.GetType());

            parent = constructor.Install(gameObject, element, context);

            if (parent != gameObject)
            {
                oldParent = parent;
            }

            foreach (var subElement in element.Elements)
            {
                var keys = Deserialize(subElement, oldParent, context);
                if (!(element is PrefabElement))
                {
                    foreach (var key in keys)
                    {
                        byKeys[key.Key] = key.Value;
                    }
                }
            }

            if (element is RectTransformElement rte_)
            {
                InstallComponents(rte_, gameObject, byKeys, context);
                InstallAnimations(rte_, gameObject, context);
            }

            constructor.PostInstall(gameObject, element, context);

            return(byKeys);
        }
        protected override UIAnimation Install(GameObject go, PositionAnimationElement element, IReadOnlyLayoutContext context)
        {
            var positionAnimation = go.AddComponent <UIPositionAnimation>();

            positionAnimation.Setup(go.GetComponent <RectTransform>(),
                                    context.ParseVector2(element.Offset), context.ParseFloat(element.Speed, 1));
            return(positionAnimation);
        }
Beispiel #29
0
        protected override UIAnimation Install(GameObject go, ScaleAnimationElement element, IReadOnlyLayoutContext context)
        {
            var scaleAnimation = go.AddComponent <UIScaleAnimation>();

            scaleAnimation.Setup(go.GetComponent <RectTransform>(),
                                 context.ParseVector3(element.DestValue), context.ParseFloat(element.Speed, 1));

            return(scaleAnimation);
        }