Exemple #1
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);
        }
Exemple #2
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);
        }