Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
## [8.0.1] - 2022-10-11
### Fixed
- Improved import speed and memory allocation for psd/psb files by reducing the intermediate texture buffers.
- Fixed an editor freeze caused by over allocating intermediate texture buffers. (Case DANB-140)
- Fixed an issue where some layers would become invisible when merging multiple layers together. (Case DANB-131)
  • Loading branch information
Unity Technologies committed Oct 11, 2022
1 parent e32d4cb commit cb142bb
Show file tree
Hide file tree
Showing 27 changed files with 1,234 additions and 1,190 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [8.0.1] - 2022-10-11
### Fixed
- Improved import speed and memory allocation for psd/psb files by reducing the intermediate texture buffers.
- Fixed an editor freeze caused by over allocating intermediate texture buffers. (Case DANB-140)
- Fixed an issue where some layers would become invisible when merging multiple layers together. (Case DANB-131)

## [8.0.0] - 2022-08-03
### Changed
- Package release version.
Expand Down
179 changes: 178 additions & 1 deletion Editor/ImportUtilites.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using PDNWrapper;
using Unity.Collections;
using UnityEditor.U2D.Animation;
using UnityEngine;

namespace UnityEditor.U2D.PSD
Expand Down Expand Up @@ -95,4 +98,178 @@ public GameObject CreateGameObject(string name, params System.Type[] components)
}
}

internal static class ImportUtilities
{
public static string SaveToPng(NativeArray<Color32> buffer, int width, int height)
{
if (!buffer.IsCreated ||
buffer.Length == 0 ||
width == 0 ||
height == 0)
return "No .png generated.";

var texture2D = new Texture2D(width, height);
texture2D.SetPixels32(buffer.ToArray());
var png = texture2D.EncodeToPNG();
var path = Application.dataPath + $"/tex_{System.Guid.NewGuid().ToString()}.png";
var fileStream = System.IO.File.Create(path);
fileStream.Write(png);
fileStream.Close();

UnityEngine.Object.DestroyImmediate(texture2D);

return path;
}

public static void ValidatePSDLayerId(IEnumerable<PSDLayer> oldPsdLayer, IEnumerable<BitmapLayer> layers, UniqueNameGenerator uniqueNameGenerator)
{
// first check if all layers are unique. If not, we use back the previous layer id based on name match
var uniqueIdSet = new HashSet<int>();
var useOldID = false;
foreach(var layer in layers)
{
if (uniqueIdSet.Contains(layer.LayerID))
{
useOldID = true;
break;
}
uniqueIdSet.Add(layer.LayerID);
}

for (var i = 0; i < layers.Count(); ++i)
{
var childBitmapLayer = layers.ElementAt(i);
// fix case 1291323
if (useOldID)
{
var oldLayers = oldPsdLayer.Where(x => x.name == childBitmapLayer.Name);
if (oldLayers.Count() == 0)
oldLayers = oldPsdLayer.Where(x => x.layerID == childBitmapLayer.Name.GetHashCode());
// pick one that is not already on the list
foreach (var ol in oldLayers)
{
if (!uniqueNameGenerator.ContainHash(ol.layerID))
{
childBitmapLayer.LayerID = ol.layerID;
break;
}
}
}

if (uniqueNameGenerator.ContainHash(childBitmapLayer.LayerID))
{
var importWarning = string.Format("Layer {0}: LayerId is not unique. Mapping will be done by Layer's name.", childBitmapLayer.Name);
var layerName = uniqueNameGenerator.GetUniqueName(childBitmapLayer.Name);
if (layerName != childBitmapLayer.Name)
importWarning += "\nLayer names are not unique. Please ensure they are unique to for SpriteRect to be mapped back correctly.";
childBitmapLayer.LayerID = layerName.GetHashCode();
Debug.LogWarning(importWarning);
}
else
uniqueNameGenerator.AddHash(childBitmapLayer.LayerID);
if (childBitmapLayer.ChildLayer != null)
{
ValidatePSDLayerId(oldPsdLayer, childBitmapLayer.ChildLayer, uniqueNameGenerator);
}
}
}

public static void TranslatePivotPoint(Vector2 pivot, Rect rect, out SpriteAlignment alignment, out Vector2 customPivot)
{
customPivot = pivot;
if (new Vector2(rect.xMin, rect.yMax) == pivot)
alignment = SpriteAlignment.TopLeft;
else if(new Vector2(rect.center.x, rect.yMax) == pivot)
alignment = SpriteAlignment.TopCenter;
else if(new Vector2(rect.xMax, rect.yMax) == pivot)
alignment = SpriteAlignment.TopRight;
else if(new Vector2(rect.xMin, rect.center.y) == pivot)
alignment = SpriteAlignment.LeftCenter;
else if(new Vector2(rect.center.x, rect.center.y) == pivot)
alignment = SpriteAlignment.Center;
else if(new Vector2(rect.xMax, rect.center.y) == pivot)
alignment = SpriteAlignment.RightCenter;
else if(new Vector2(rect.xMin, rect.yMin) == pivot)
alignment = SpriteAlignment.BottomLeft;
else if(new Vector2(rect.center.x, rect.yMin) == pivot)
alignment = SpriteAlignment.BottomCenter;
else if(new Vector2(rect.xMax, rect.yMin) == pivot)
alignment = SpriteAlignment.BottomRight;
else
alignment = SpriteAlignment.Custom;
}

public static Vector2 GetPivotPoint(Rect rect, SpriteAlignment alignment, Vector2 customPivot)
{
switch (alignment)
{
case SpriteAlignment.TopLeft:
return new Vector2(rect.xMin, rect.yMax);

case SpriteAlignment.TopCenter:
return new Vector2(rect.center.x, rect.yMax);

case SpriteAlignment.TopRight:
return new Vector2(rect.xMax, rect.yMax);

case SpriteAlignment.LeftCenter:
return new Vector2(rect.xMin, rect.center.y);

case SpriteAlignment.Center:
return new Vector2(rect.center.x, rect.center.y);

case SpriteAlignment.RightCenter:
return new Vector2(rect.xMax, rect.center.y);

case SpriteAlignment.BottomLeft:
return new Vector2(rect.xMin, rect.yMin);

case SpriteAlignment.BottomCenter:
return new Vector2(rect.center.x, rect.yMin);

case SpriteAlignment.BottomRight:
return new Vector2(rect.xMax, rect.yMin);

case SpriteAlignment.Custom:
return new Vector2(customPivot.x * rect.width, customPivot.y * rect.height);
}
return Vector2.zero;
}

public static string GetUniqueSpriteName(string name, UniqueNameGenerator generator, bool keepDupilcateSpriteName)
{
if (keepDupilcateSpriteName)
return name;
return generator.GetUniqueName(name);
}

public static bool VisibleInHierarchy(List<PSDLayer> psdGroup, int index)
{
var psdLayer = psdGroup[index];
var parentVisible = true;
if (psdLayer.parentIndex >= 0)
parentVisible = VisibleInHierarchy(psdGroup, psdLayer.parentIndex);
return parentVisible && psdLayer.isVisible;
}

public static bool SpriteIsMainFromSpriteLib(List<SpriteCategory> categories, string spriteId, out string categoryName)
{
categoryName = "";
if (categories != null)
{
foreach (var category in categories)
{
var index = category.labels.FindIndex(x => x.spriteId == spriteId);
if (index == 0)
{
categoryName = category.name;
return true;
}
if (index > 0)
return false;
}
}
return true;
}
}
}
2 changes: 1 addition & 1 deletion Editor/PSDImportData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void CreatePSDLayerData(BitmapLayer layer, List<PSDLayerData> layerData, int par
layerID = layer.LayerID,
name = layer.Name,
parentIndex = parentIndex,
layerSizeOnFile = new Vector2Int(layer.rect.Width, layer.rect.Height)
layerSizeOnFile = new Vector2Int(layer.documentRect.Width, layer.documentRect.Height)
});
parentIndex = layerData.Count - 1;
foreach (var fileLayer in layer.ChildLayer)
Expand Down
Loading

0 comments on commit cb142bb

Please sign in to comment.