Ejemplo n.º 1
0
        public GameObject Reconstruct(ImportLayerData root, ReconstructData data, GameObject selection)
        {
            GameObject rootObject = new GameObject(root.name);

            if (selection != null)
            {
                rootObject.transform.SetParent(selection.transform);
            }

            // Create a stack that represents the current parent
            // as the hierarchy is being traversed
            Stack <Transform> hierarchy = new Stack <Transform>();

            // Add the root object as the first parent
            hierarchy.Push(rootObject.transform);

            // Calculate the document pivot position
            Vector2 docRoot = data.documentSize;

            docRoot.x *= data.documentPivot.x;
            docRoot.y *= data.documentPivot.y;

            // Sorting index accumulator, so sprites draw in correct order
            int sortIdx = 0;

            root.Iterate(
                layer =>
            {
                // Only process non group layers and layers marked for import
                if (layer.Childs.Count > 0 || layer.import == false)
                {
                    return;
                }

                // Create an object
                GameObject layerObject = new GameObject(layer.name);
                Transform layerT       = layerObject.transform;
                // And attach it to the last
                layerT.SetParent(hierarchy.Peek());
                layerT.SetAsLastSibling();

                // Find the sprite for this layer in the data sprite index
                Sprite layerSprite;
                if (data.spriteIndex.TryGetValue(layer.indexId, out layerSprite))
                {
                    // Attach a sprite renderer and set the sorting order
                    SpriteRenderer layerRender = layerObject.AddComponent <SpriteRenderer>();
                    layerRender.sprite         = layerSprite;
                    layerRender.sortingOrder   = sortIdx;
                    sortIdx--;
                }

                // Get the layer position
                Vector2 layerPos = GetLayerPosition(data, layer.indexId);
                // Express it as a vector
                Vector2 layerVector = layerPos - docRoot;
                // This is in pixel units, convert to unity world units
                layerVector    /= data.documentPPU;
                layerT.position = layerVector;
            },
                checkGroup => checkGroup.import,                 // Only enter groups if part of the import
                enterGroupCallback: layer =>
            {
                // Enter a group, create an object for it
                GameObject groupObject = new GameObject(layer.name);
                Transform groupT       = groupObject.transform;
                // Parent to the last hierarchy parent
                groupT.SetParent(hierarchy.Peek());
                // Look at me, I'm the hierarchy parent now
                hierarchy.Push(groupT);
            },
                exitGroupCallback: layer =>
            {
                // Go back to the last parent
                hierarchy.Pop();
            });

            // Unity 5.6 introduces the sorting group component
#if UNITY_5_6_OR_NEWER
            rootObject.AddComponent <SortingGroup>();
#endif
            return(rootObject);
        }
Ejemplo n.º 2
0
        public GameObject Reconstruct(ImportLayerData root, ReconstructData data, GameObject selection)
        {
            if (selection == null)
            {
                return(null);
            }
            if (CanReconstruct(selection) == false)
            {
                return(null);
            }

            var rootT = CreateObject(root.name);

            rootT.SetParent(selection.transform);

            rootT.sizeDelta     = data.documentSize;
            rootT.pivot         = data.documentPivot;
            rootT.localPosition = Vector3.zero;

            // Create a stack that represents the current parent
            // as the hierarchy is being traversed
            Stack <RectTransform> hierarchy = new Stack <RectTransform>();

            // Add the root object as the first parent
            hierarchy.Push(rootT);

            // Calculate the document pivot position
            Vector2 docRoot = data.documentSize;

            docRoot.x *= data.documentPivot.x;
            docRoot.y *= data.documentPivot.y;

            root.Iterate(
                layer =>
            {
                // Only process non group layers or layers marked for import
                if (layer.Childs.Count > 0 || layer.import == false)
                {
                    return;
                }

                // Create an object
                RectTransform layerT = CreateObject(layer.name);

                // And attach it to the last parent
                layerT.SetParent(hierarchy.Peek());
                // Order correctly for UI
                layerT.SetAsFirstSibling();

                // Find the sprite for this layer in the data sprite index
                Sprite layerSprite;
                if (data.spriteIndex.TryGetValue(layer.indexId, out layerSprite))
                {
                    var layerImg    = layerT.gameObject.AddComponent <Image>();
                    layerImg.sprite = layerSprite;
                }

                // Get the layer position

                Rect layerRect;
                if (data.layerBoundsIndex.TryGetValue(layer.indexId, out layerRect) == false)
                {
                    layerRect = Rect.zero;
                }

                Vector2 layerAnchor;
                if (data.spriteAnchors.TryGetValue(layer.indexId, out layerAnchor) == false)
                {
                    layerAnchor = Vector2.zero;
                }

                Vector2 layerPos = GetLayerPosition(layerRect, layerAnchor);
                // Express it as a vector
                Vector2 layerVector = layerPos - docRoot;
                // Position using the rootT as reference
                layerT.position = rootT.TransformPoint(layerVector.x, layerVector.y, 0);

                layerT.pivot     = layerAnchor;
                layerT.sizeDelta = new Vector2(layerRect.width, layerRect.height);
            },
                checkGroup => checkGroup.import,                 // Only enter groups if part of the import
                enterGroupCallback: layer =>
            {
                // Enter a group, create an object for it
                RectTransform groupT = CreateObject(layer.name);
                // Parent to the last hierarchy parent
                groupT.SetParent(hierarchy.Peek());
                groupT.SetAsFirstSibling();

                groupT.anchorMin = Vector2.zero;
                groupT.anchorMax = Vector2.one;
                groupT.offsetMin = Vector2.zero;
                groupT.offsetMax = Vector2.zero;

                // Look at me, I'm the hierarchy parent now
                hierarchy.Push(groupT);
            },
                exitGroupCallback: layer =>
            {
                // Go back to the last parent
                hierarchy.Pop();
            });

            return(rootT.gameObject);
        }