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); } } } }
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; } } }
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); } }
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); }