private void CreateSprites() { int zOrder = settings.Psd.Layers.Count; // Find scaling factor float posScale = 1f; switch (settings.ScaleBy) { case 1: posScale = 0.5f; break; case 2: posScale = 0.25f; break; } GameObject root = new GameObject(settings.Filename); // Create the offset vector Vector3 createOffset = Vector3.zero; if (createPivot != PivotPos.TopLeft) { Vector2 docSize = new Vector2(settings.Psd.ColumnCount, settings.Psd.RowCount); docSize *= posScale; if (createPivot == PivotPos.Center || createPivot == PivotPos.Left || createPivot == PivotPos.Right) { createOffset.y = (docSize.y / 2) / settings.PixelsToUnitSize; } if (createPivot == PivotPos.Bottom || createPivot == PivotPos.BottomLeft || createPivot == PivotPos.BottomRight) { createOffset.y = docSize.y / settings.PixelsToUnitSize; } if (createPivot == PivotPos.Center || createPivot == PivotPos.Top || createPivot == PivotPos.Bottom) { createOffset.x = -(docSize.x / 2) / settings.PixelsToUnitSize; } if (createPivot == PivotPos.Right || createPivot == PivotPos.TopRight || createPivot == PivotPos.BottomRight) { createOffset.x = -(docSize.x) / settings.PixelsToUnitSize; } } // Loop through the layers Dictionary <LayerGroupInfo, GameObject> groupHeaders = new Dictionary <LayerGroupInfo, GameObject>(); GameObject lastParent = root; for (int i = settings.Psd.Layers.Count - 1; i >= 0; i--) { var groupInfo = fileInfo.GetGroupByLayerIndex(i); if (groupInfo != null && !groupInfo.visible) { continue; } if (!fileInfo.LayerVisibility[i]) { continue; } Layer layer = settings.Psd.Layers[i]; bool inGroup = groupInfo != null; if (inGroup) { bool startGroup = groupInfo.end == i; bool closeGroup = groupInfo.start == i; if (startGroup) { GameObject groupRoot = new GameObject(layer.Name); groupRoot.transform.parent = lastParent.transform; groupRoot.transform.localPosition = Vector3.zero; groupRoot.transform.localScale = Vector3.one; lastParent = groupRoot; groupHeaders.Add(groupInfo, groupRoot); continue; } if (closeGroup) { lastParent = groupHeaders[groupInfo].transform.parent.gameObject; continue; } } // Try to get the sprite from the asset database first string assetPath = AssetDatabase.GetAssetPath(image); string path = Path.Combine(Path.GetDirectoryName(assetPath), Path.GetFileNameWithoutExtension(assetPath) + "_" + layer.Name + ".png"); // Sprites doesn't exist, create it Sprite spr = (Sprite)AssetDatabase.LoadAssetAtPath(path, typeof(Sprite)); if (spr == null) { spr = PSDExporter.CreateSprite(settings, layer); } // Get the pivot settings for the sprite TextureImporter spriteSettings = (TextureImporter)AssetImporter.GetAtPath(path); GameObject go = new GameObject(layer.Name); SpriteRenderer sr = go.AddComponent <SpriteRenderer>(); sr.sprite = spr; sr.sortingOrder = zOrder--; if (_sortingLayerNames != null) { sr.sortingLayerName = _sortingLayerNames[createSortLayer]; } Vector3 goPos = Vector3.zero; goPos.x = ((layer.Rect.width * spriteSettings.spritePivot.x) + layer.Rect.x) / settings.PixelsToUnitSize; goPos.y = (-(layer.Rect.height * (1 - spriteSettings.spritePivot.y)) - layer.Rect.y) / settings.PixelsToUnitSize; goPos.x *= posScale; goPos.y *= posScale; goPos += createOffset; go.transform.parent = lastParent.transform; go.transform.localScale = Vector3.one; go.transform.localPosition = goPos; if (createAtSelection && Selection.activeGameObject != null) { go.layer = Selection.activeGameObject.layer; } } if (createAtSelection && Selection.activeGameObject != null) { root.transform.parent = Selection.activeGameObject.transform; root.transform.localScale = Vector3.one; root.transform.localPosition = Vector3.zero; root.layer = Selection.activeGameObject.layer; } }
private void ExportLayers() { PSDExporter.Export(settings, fileInfo); }