Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
## [3.1.4] - 2020-04-09
### Fixed
- Fix PSD import issues with PSD file without unique layer id
- Fix crash on importing huge PSD files
- Fix metafile not updated when reimporting
- Fix error when importing PSB files with 32-bit color

### Changed
- Improve PSD file import performance
  • Loading branch information
Unity Technologies committed Apr 8, 2020
1 parent 3c1308a commit c6c5be0
Show file tree
Hide file tree
Showing 36 changed files with 1,084 additions and 903 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# Changelog
## [3.1.4] - 2020-04-09
### Fixed
- Fix PSD import issues with PSD file without unique layer id
- Fix crash on importing huge PSD files
- Fix metafile not updated when reimporting
- Fix error when importing PSB files with 32-bit color

### Changed
- Improve PSD file import performance

## [3.1.3] - 2020-03-20
### Changed
Expand Down
3 changes: 2 additions & 1 deletion Documentation~/PSDImporter.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ namespace UnityEditor.U2D.PSD
foreach (var obj in Selection.objects)
{
var path = AssetDatabase.GetAssetPath(obj);
var ext = Path.GetExtension(path);
var ext = System.IO.Path.GetExtension(path);
if (ext == ".psd")
{
var importer = AssetImporter.GetAtPath(path);
Expand All @@ -206,6 +206,7 @@ namespace UnityEditor.U2D.PSD
}
}
}
}
```

#### PSDImporterOverrideEditor.cs
Expand Down
106 changes: 81 additions & 25 deletions Editor/PSDImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using UnityEngine.Experimental.U2D.Animation;
using UnityEngine.U2D;
using UnityEngine.U2D.Animation;
using UnityEditor.MemoryProfiler;
using UnityEngine.Profiling.Memory.Experimental;

namespace UnityEditor.U2D.PSD
{
Expand All @@ -23,13 +25,32 @@ namespace UnityEditor.U2D.PSD
[HelpURL("https://docs.unity3d.com/Packages/[email protected]/manual/index.html")]
public class PSDImporter : ScriptedImporter, ISpriteEditorDataProvider
{
class GameObjectCreationFactory
class UniqueNameGenerator
{
List<int> m_NameHash = new List<int>();

public bool ContainHash(int i)
{
return m_NameHash.Contains(i);
}

public void AddHash(int i)
{
m_NameHash.Add(i);
}

public string GetUniqueName(string name)
{
return PSDImporter.GetUniqueName(name, m_NameHash);
}
}

class GameObjectCreationFactory : UniqueNameGenerator
{
List<int> m_GameObjectNameHash = new List<int>();

public GameObject CreateGameObject(string name, params System.Type[] components)
{
var newName = GetUniqueName(name, m_GameObjectNameHash);
var newName = GetUniqueName(name);
return new GameObject(newName, components);
}
}
Expand Down Expand Up @@ -184,10 +205,6 @@ internal int textureActualHeight
/// </param>
public override void OnImportAsset(AssetImportContext ctx)
{
string ext = System.IO.Path.GetExtension(ctx.assetPath).ToLower();
if (ext != ".psb")
throw new Exception("File does not have psb extension");

FileStream fileStream = new FileStream(ctx.assetPath, FileMode.Open, FileAccess.Read);
Document doc = null;
try
Expand All @@ -198,29 +215,16 @@ public override void OnImportAsset(AssetImportContext ctx)
doc = PaintDotNet.Data.PhotoshopFileType.PsdLoad.Load(fileStream);
UnityEngine.Profiling.Profiler.EndSample();

// Is layer id truely unique?
for (int i = 0; i < doc.Layers.Count; ++i)
{
for (int j = 0; j < doc.Layers.Count; ++j)
{
if (i == j)
continue;
if (doc.Layers[i].LayerID == doc.Layers[j].LayerID)
{
Debug.LogWarning("File's Layer ID is not unique. Please report to developer. " + doc.Layers[i].LayerID + " " + doc.Layers[i].Name + "::" + doc.Layers[j].Name);
doc.Layers[i].LayerID = doc.Layers[i].Name.GetHashCode();
}
}
}

ValidatePSDLayerId(doc);

m_DocumentSize = new Vector2Int(doc.width, doc.height);
bool singleSpriteMode = m_TextureImporterSettings.textureType == TextureImporterType.Sprite && m_TextureImporterSettings.spriteMode != (int)SpriteImportMode.Multiple;
EnsureSingleSpriteExist();

if (m_TextureImporterSettings.textureType != TextureImporterType.Sprite ||
m_MosaicLayers == false || singleSpriteMode)
{
var image = new NativeArray<Color32>(doc.width * doc.height, Allocator.Temp);
var image = new NativeArray<Color32>(doc.width * doc.height, Allocator.Persistent);
try
{
var spriteImportData = GetSpriteImportData();
Expand Down Expand Up @@ -264,11 +268,49 @@ public override void OnImportAsset(AssetImportContext ctx)
if (doc != null)
doc.Dispose();
UnityEngine.Profiling.Profiler.EndSample();
EditorUtility.SetDirty(this);
}


// Debug Profiler.
// UnityEngine.Profiling.Memory.Experimental.MemoryProfiler.TakeSnapshot("snapshot.snap", MemorySnapshotFinish, CaptureFlags.ManagedObjects | CaptureFlags.NativeObjects | CaptureFlags.NativeAllocations | CaptureFlags.NativeStackTraces);
}

static void ValidatePSDLayerId(List<BitmapLayer> layers, UniqueNameGenerator uniqueNameGenerator)
{
for (int i = 0; i < layers.Count; ++i)
{
if (uniqueNameGenerator.ContainHash(layers[i].LayerID))
{
var importWarning = string.Format("Layer {0}: LayerId is not unique. Mapping will be done by Layer's name.", layers[i].Name);
var layerName = uniqueNameGenerator.GetUniqueName(layers[i].Name);
if (layerName != layers[i].Name)
importWarning += "\nLayer names are not unique. Please ensure they are unique to for SpriteRect to be mapped back correctly.";
layers[i].LayerID = layerName.GetHashCode();
Debug.LogWarning(importWarning);
}
else
uniqueNameGenerator.AddHash(layers[i].LayerID);
if (layers[i].ChildLayer != null)
{
ValidatePSDLayerId(layers[i].ChildLayer, uniqueNameGenerator);
}
}
}


void ValidatePSDLayerId(Document doc)
{
UniqueNameGenerator uniqueNameGenerator = new UniqueNameGenerator();

ValidatePSDLayerId(doc.Layers, uniqueNameGenerator);
}

TextureGenerationOutput ImportTexture(AssetImportContext ctx, NativeArray<Color32> imageData, int textureWidth, int textureHeight, int spriteStart, int spriteCount)
{
if (!imageData.IsCreated || imageData.Length == 0)
return new TextureGenerationOutput();

UnityEngine.Profiling.Profiler.BeginSample("ImportTexture");
var platformSettings = GetPlatformTextureSettings(ctx.selectedBuildTarget);

Expand Down Expand Up @@ -507,13 +549,21 @@ void ImportFromLayers(AssetImportContext ctx, Document doc)
}
}
}

foreach (var l in oldPsdLayers)
l.Dispose();
oldPsdLayers.Clear();

oldPsdLayers.AddRange(psdLayers);
m_ImportedTextureHeight = textureActualHeight = height;
m_ImportedTextureWidth = textureActualWidth = width;
var generatedTexture = ImportTexture(ctx, output, width, height, 0, spriteImportData.Count);
m_ImportedTextureHeight = generatedTexture.texture.height;
m_ImportedTextureWidth = generatedTexture.texture.width;

if (generatedTexture.texture)
{
m_ImportedTextureHeight = generatedTexture.texture.height;
m_ImportedTextureWidth = generatedTexture.texture.width;
}

RegisterAssets(ctx, generatedTexture);
}
Expand All @@ -524,6 +574,12 @@ void ImportFromLayers(AssetImportContext ctx, Document doc)
foreach (var l in oldPsdLayers)
l.Dispose();
}

}

void MemorySnapshotFinish(string path, bool done)
{

}

void EnsureSingleSpriteExist()
Expand Down
2 changes: 1 addition & 1 deletion Editor/PSDImporterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected override void Apply()
is_psd = IsPSD(doc),
color_mode = FileColorMode(doc)
};

doc.Cleanup();
AnalyticFactory.analytics.SendApplyEvent(evt);
base.Apply();
}
Expand Down
Loading

0 comments on commit c6c5be0

Please sign in to comment.