Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
## [1.0.0-preview.4] - 2019-04-23
### Added
- Fix potential name clashing issues with ScriptedImporter
- Fix Prefab asset using wrong name. Note this will break Prefab references if upgrading from previous versions.
  • Loading branch information
Unity Technologies committed Apr 22, 2019
1 parent 20d8654 commit 977a46b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this package will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.0.0-preview.4] - 2019-04-23
### Added
- Fix potential name clashing issues with ScriptedImporter
- Fix Prefab asset using wrong name. Note this will break Prefab references if upgrading from previous versions.

## [1.0.0-preview.3] - 2019-02-19
### Added
- Fix compilation error in .NET 3.5
Expand Down
60 changes: 41 additions & 19 deletions Editor/PSDImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ namespace UnityEditor.Experimental.U2D.PSD
[ScriptedImporter(1, "psb")]
public class PSDImporter : ScriptedImporter, ISpriteEditorDataProvider, IAnimationAssetPostProcess
{
class GameObjectCreationFactory
{
List<int> m_GameObjectNameHash = new List<int>();

public GameObject CreateGameObject(string name, params System.Type[] components)
{
var newName = GetUniqueName(name, m_GameObjectNameHash);
return new GameObject(newName, components);
}
}

[Serializable]
struct BoneGO
{
Expand Down Expand Up @@ -50,6 +61,8 @@ struct BoneGO
[SerializeField]
Vector2Int m_DocumentSize;

GameObjectCreationFactory m_GameObjectFactory = new GameObjectCreationFactory();

[SerializeField]
int m_TextureActualWidth;
public int textureActualWidth
Expand Down Expand Up @@ -88,6 +101,12 @@ public int textureActualHeight
[SerializeField]
bool m_GenerateGOHierarchy = false;

[SerializeField]
string m_TextureAssetName = null;

[SerializeField]
string m_PrefabAssetName = null;

public PSDImporter()
{
m_TextureImporterSettings = new TextureImporterSettings();
Expand Down Expand Up @@ -194,7 +213,7 @@ public override void OnImportAsset(AssetImportContext ctx)
{
spriteImportData.Add(new SpriteMetaData());
}
spriteImportData[0].name = System.IO.Path.GetFileNameWithoutExtension(ctx.assetPath) +"_1";
spriteImportData[0].name = System.IO.Path.GetFileNameWithoutExtension(ctx.assetPath) + "_1";
spriteImportData[0].alignment = (SpriteAlignment)m_TextureImporterSettings.spriteAlignment;
spriteImportData[0].border = m_TextureImporterSettings.spriteBorder;
spriteImportData[0].pivot = m_TextureImporterSettings.spritePivot;
Expand Down Expand Up @@ -512,31 +531,33 @@ void RegisterAssets(AssetImportContext ctx, TextureGenerationOutput output)
{
throw new Exception("Texture import fail");
}
var assetName = GetUniqueName(System.IO.Path.GetFileNameWithoutExtension(ctx.assetPath), assetNameHash, true);
output.texture.name = assetName;
ctx.AddObjectToAsset(assetName, output.texture, output.thumbNail);

if (string.IsNullOrEmpty(m_TextureAssetName))
m_TextureAssetName = GetUniqueName(System.IO.Path.GetFileNameWithoutExtension(ctx.assetPath), assetNameHash, true);
output.texture.name = m_TextureAssetName;
ctx.AddObjectToAsset(m_TextureAssetName, output.texture, output.thumbNail);
UnityEngine.Object mainAsset = output.texture;


if (output.sprites != null)
{
foreach (var s in output.sprites)
{
assetName = GetUniqueName(s.name, assetNameHash, true, s);
ctx.AddObjectToAsset(assetName, s);
}


if (shouldProduceGameObject)
{
var prefab = OnProducePrefab(assetName, output.sprites);
var prefab = OnProducePrefab(m_TextureAssetName, output.sprites);
if (prefab != null)
{
assetName = GetUniqueName(prefab.name, assetNameHash, true, prefab);
ctx.AddObjectToAsset(assetName, prefab);
if (string.IsNullOrEmpty(m_PrefabAssetName))
m_PrefabAssetName = GetUniqueName(prefab.name, assetNameHash, true, prefab);

ctx.AddObjectToAsset(m_PrefabAssetName, prefab);
mainAsset = prefab;
}
}
foreach (var s in output.sprites)
{
var spriteAssetName = GetUniqueName(s.GetSpriteID().ToString(), assetNameHash, false, s);
ctx.AddObjectToAsset(spriteAssetName, s);
}
}
ctx.SetMainObject(mainAsset);
}
Expand All @@ -547,13 +568,14 @@ void BuildGroupGameObject(List<PSDLayer> psdGroup, int index, Transform root)
if (psdGroup[index].gameObject == null)
{
if (m_GenerateGOHierarchy || !psdGroup[index].spriteID.Empty())
psdGroup[index].gameObject = new GameObject(spriteData != null ? spriteData.name : psdGroup[index].name);
psdGroup[index].gameObject = m_GameObjectFactory.CreateGameObject(spriteData != null ? spriteData.name : psdGroup[index].name);
if (psdGroup[index].parentIndex >= 0 && m_GenerateGOHierarchy)
{
BuildGroupGameObject(psdGroup, psdGroup[index].parentIndex, root);
psdGroup[index].gameObject.transform.SetParent(psdGroup[psdGroup[index].parentIndex].gameObject.transform);
root = psdGroup[psdGroup[index].parentIndex].gameObject.transform;
}
else if (psdGroup[index].gameObject != null)

if (psdGroup[index].gameObject != null)
psdGroup[index].gameObject.transform.SetParent(root);
}
}
Expand Down Expand Up @@ -628,7 +650,7 @@ void CreateBoneGO(int index, SpriteBone[] bones, BoneGO[] bonesGO, Transform def
if (bone.parentId != -1 && bonesGO[bone.parentId].go == null)
CreateBoneGO(bone.parentId, bones, bonesGO, defaultRoot);

var go = new GameObject(bone.name);
var go = m_GameObjectFactory.CreateGameObject(bone.name);
if (bone.parentId == -1)
go.transform.SetParent(defaultRoot);
else
Expand Down
25 changes: 24 additions & 1 deletion Tests/EditorTests/PSDImporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using System;
using UnityEngine.Experimental.U2D.Animation;
using UnityEngine.Rendering;

namespace UnityEditor.Experimental.U2D.PSD.Tests
{
Expand Down Expand Up @@ -216,5 +215,29 @@ public void PSBImportWithReslice_RecreateSpritesFromLayer()
so = new SerializedObject(importer);
Assert.IsFalse(so.FindProperty("m_ResliceFromLayer").boolValue);
}

[Test]
public void PrefabReferenceRemains_AfterAssetRename()
{
var testAssetPath = CopyTestAssetFile();
var importer = AssetImporter.GetAtPath(testAssetPath) as PSDImporter;
var so = new SerializedObject(importer);
so.FindProperty("m_MosaicLayers").boolValue = true;
so.FindProperty("m_CharacterMode").boolValue = true;
var textureImporterSettingsSP = so.FindProperty("m_TextureImporterSettings");
textureImporterSettingsSP.FindPropertyRelative("m_TextureType").intValue = (int)TextureImporterType.Sprite;
textureImporterSettingsSP.FindPropertyRelative("m_SpriteMode").intValue = (int)SpriteImportMode.Multiple;
textureImporterSettingsSP.FindPropertyRelative("m_SpriteMode").intValue = (int)SpriteImportMode.Multiple;
so.ApplyModifiedPropertiesWithoutUndo();
importer.SaveAndReimport();

var prefabGO = AssetDatabase.LoadAssetAtPath<GameObject>(testAssetPath);
var instantiatedPrefab = PrefabUtility.InstantiatePrefab(prefabGO);
var fileName = System.IO.Path.GetFileNameWithoutExtension(testAssetPath);
var extension = System.IO.Path.GetExtension(testAssetPath);
var s = AssetDatabase.RenameAsset(testAssetPath, fileName + "-rename" + extension);
AssetDatabase.Refresh();
Assert.IsFalse(PrefabUtility.IsPrefabAssetMissing(instantiatedPrefab));
}
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.unity.2d.psdimporter",
"version": "1.0.0-preview.3",
"version": "1.0.0-preview.4",
"unity": "2018.3",
"displayName": "2D PSD Importer",
"description": "A ScriptedImporter for importing Adobe Photoshop file format. The ScriptedImporter is currently targeted for users who wants to create multi Sprite character animation using 2D Animation v2.",
Expand All @@ -12,11 +12,11 @@
"category": "2D",
"dependencies": {
"com.unity.2d.common": "1.0.11-preview.1",
"com.unity.2d.animation": "2.0.0-preview.1"
"com.unity.2d.animation": "2.0.0-preview.3"
},
"repository": {
"type": "git",
"url": "[email protected]:unity/com.unity.2d.psdimporter.git",
"revision": "acc408ecd9c2e774cd8d4a0e9c4f6d5e4d6d3b17"
"revision": "4cd558eb44a521230c0fdd39a5e2df45e46583f6"
}
}

0 comments on commit 977a46b

Please sign in to comment.