diff --git a/CHANGELOG.md b/CHANGELOG.md index 52a64aa..95b5e69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Editor/PSDImporter.cs b/Editor/PSDImporter.cs index 8b54b96..a708aa8 100644 --- a/Editor/PSDImporter.cs +++ b/Editor/PSDImporter.cs @@ -17,6 +17,17 @@ namespace UnityEditor.Experimental.U2D.PSD [ScriptedImporter(1, "psb")] public class PSDImporter : ScriptedImporter, ISpriteEditorDataProvider, IAnimationAssetPostProcess { + class GameObjectCreationFactory + { + List m_GameObjectNameHash = new List(); + + public GameObject CreateGameObject(string name, params System.Type[] components) + { + var newName = GetUniqueName(name, m_GameObjectNameHash); + return new GameObject(newName, components); + } + } + [Serializable] struct BoneGO { @@ -50,6 +61,8 @@ struct BoneGO [SerializeField] Vector2Int m_DocumentSize; + GameObjectCreationFactory m_GameObjectFactory = new GameObjectCreationFactory(); + [SerializeField] int m_TextureActualWidth; public int textureActualWidth @@ -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(); @@ -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; @@ -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); } @@ -547,13 +568,14 @@ void BuildGroupGameObject(List 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); } } @@ -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 diff --git a/Tests/EditorTests/PSDImporterTests.cs b/Tests/EditorTests/PSDImporterTests.cs index 8e1d03d..7815ebf 100644 --- a/Tests/EditorTests/PSDImporterTests.cs +++ b/Tests/EditorTests/PSDImporterTests.cs @@ -4,7 +4,6 @@ using System.Linq; using System; using UnityEngine.Experimental.U2D.Animation; -using UnityEngine.Rendering; namespace UnityEditor.Experimental.U2D.PSD.Tests { @@ -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(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)); + } } } diff --git a/package.json b/package.json index 66a3642..9426196 100644 --- a/package.json +++ b/package.json @@ -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.", @@ -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": "git@github.cds.internal.unity3d.com:unity/com.unity.2d.psdimporter.git", - "revision": "acc408ecd9c2e774cd8d4a0e9c4f6d5e4d6d3b17" + "revision": "4cd558eb44a521230c0fdd39a5e2df45e46583f6" } }