Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
## [7.0.0-pre.4] - 2021-11-24
### Fixed
- Fixed per platform settings does not get applied in Windows platform. (Case 1376608)
- Fixed unable to change mipmap settings in inspector. (Case 1379426)
- Fixed PSDImporter to able to specify swizzle data.

### Added
- Added ability to change swizzle format in inspector.
  • Loading branch information
Unity Technologies committed Nov 24, 2021
1 parent eee39c8 commit 392315a
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 22 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## [7.0.0-pre.4] - 2021-11-24
### Fixed
- Fixed per platform settings does not get applied in Windows platform. (Case 1376608)
- Fixed unable to change mipmap settings in inspector. (Case 1379426)
- Fixed PSDImporter to able to specify swizzle data.

### Added
- Added ability to change swizzle format in inspector.

## [7.0.0-pre.3] - 2021-10-21
### Changed
- Update to latest com.unity.2d.animation package
Expand Down
17 changes: 15 additions & 2 deletions Editor/PSDImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace UnityEditor.U2D.PSD
/// ScriptedImporter to import Photoshop files
/// </summary>
// Version using unity release + 5 digit padding for future upgrade. Eg 2021.2 -> 21200000
[ScriptedImporter(22100000, new string[]{"psb"}, new []{"psd"}, AllowCaching = true)]
[ScriptedImporter(22100001, new string[]{"psb"}, new []{"psd"}, AllowCaching = true)]
[HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.psdimporter@latest")]
[MovedFrom("UnityEditor.Experimental.AssetImporters")]
public partial class PSDImporter : ScriptedImporter, ISpriteEditorDataProvider
Expand Down Expand Up @@ -99,6 +99,10 @@ internal enum ECustomSpriteMode
wrapModeU = TextureWrapMode.Repeat,
wrapModeV = TextureWrapMode.Repeat,
wrapModeW = TextureWrapMode.Repeat,
swizzleR = TextureImporterSwizzle.R,
swizzleG = TextureImporterSwizzle.G,
swizzleB = TextureImporterSwizzle.B,
swizzleA = TextureImporterSwizzle.A,
};

[SerializeField]
Expand Down Expand Up @@ -241,6 +245,14 @@ ECustomSpriteMode GetCustomSpriteMode()
}
return ECustomSpriteMode.Multiple;
}

public PSDImporter()
{
m_TextureImporterSettings.swizzleA = TextureImporterSwizzle.A;
m_TextureImporterSettings.swizzleR = TextureImporterSwizzle.R;
m_TextureImporterSettings.swizzleG = TextureImporterSwizzle.G;
m_TextureImporterSettings.swizzleB = TextureImporterSwizzle.B;
}

/// <summary>
/// Implementation of ScriptedImporter.OnImportAsset
Expand All @@ -251,6 +263,7 @@ ECustomSpriteMode GetCustomSpriteMode()
/// </param>
public override void OnImportAsset(AssetImportContext ctx)
{

FileStream fileStream = new FileStream(ctx.assetPath, FileMode.Open, FileAccess.Read);
Document doc = null;
if(m_ImportData == null)
Expand Down Expand Up @@ -760,7 +773,7 @@ void EnsureSingleSpriteExist()

TextureImporterPlatformSettings GetPlatformTextureSettings(BuildTarget buildTarget)
{
var buildTargetName = TexturePlatformSettingsHelper.GetBuildTargetName(buildTarget);
var buildTargetName = TexturePlatformSettingsHelper.GetBuildTargetGroupName(buildTarget);
TextureImporterPlatformSettings platformSettings = null;
platformSettings = m_PlatformSettings.SingleOrDefault(x => x.name == buildTargetName && x.overridden == true);
platformSettings = platformSettings ?? m_PlatformSettings.SingleOrDefault(x => x.name == TexturePlatformSettingsHelper.defaultPlatformName);
Expand Down
62 changes: 53 additions & 9 deletions Editor/PSDImporterAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ public bool mipMapsPreserveCoverage
/// <exception cref="ArgumentException">Exception when non valid values are set.</exception>
public SpriteImportMode spriteImportMode
{
get
{
return (SpriteImportMode)m_TextureImporterSettings.spriteMode;
}
get { return (SpriteImportMode)m_TextureImporterSettings.spriteMode; }
set
{
if (value == SpriteImportMode.Multiple || value == SpriteImportMode.Single)
Expand All @@ -185,10 +182,7 @@ public SpriteImportMode spriteImportMode
/// <exception cref="ArgumentException">Exception when non valid values are set.</exception>
public TextureImporterType textureType
{
get
{
return (TextureImporterType)m_TextureImporterSettings.textureType;
}
get { return (TextureImporterType)m_TextureImporterSettings.textureType; }
set
{
if (value == TextureImporterType.Sprite || value == TextureImporterType.Default)
Expand Down Expand Up @@ -331,8 +325,58 @@ public bool useMosaicMode
SetDirty();
}
}

internal TextureImporterSwizzle swizzeR
{
get { return m_TextureImporterSettings.swizzleR; }
set
{
m_TextureImporterSettings.swizzleR = value;
SetDirty();
}
}

internal TextureImporterSwizzle swizzeG
{
get { return m_TextureImporterSettings.swizzleG; }
set
{
m_TextureImporterSettings.swizzleG = value;
SetDirty();
}
}

void SetDirty()
internal TextureImporterSwizzle swizzeB
{
get { return m_TextureImporterSettings.swizzleB; }
set
{
m_TextureImporterSettings.swizzleB = value;
SetDirty();
}
}

internal TextureImporterSwizzle swizzeA
{
get { return m_TextureImporterSettings.swizzleA; }
set
{
m_TextureImporterSettings.swizzleA = value;
SetDirty();
}
}

internal bool sRGBTexture
{
get { return m_TextureImporterSettings.sRGBTexture; }
set
{
m_TextureImporterSettings.sRGBTexture = value;
SetDirty();
}
}

void SetDirty()
{
EditorUtility.SetDirty(this);
}
Expand Down
70 changes: 66 additions & 4 deletions Editor/PSDImporterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct InspectorGUI
SerializedProperty m_IsReadable;
SerializedProperty m_sRGBTexture;
SerializedProperty m_AlphaSource;
SerializedProperty m_Swizzle;
SerializedProperty m_MipMapMode;
SerializedProperty m_EnableMipMap;
SerializedProperty m_FadeOut;
Expand Down Expand Up @@ -75,7 +76,8 @@ struct InspectorGUI

private SkeletonAsset m_SkeletonAsset;
readonly int[] m_FilterModeOptions = (int[])(Enum.GetValues(typeof(FilterMode)));

static readonly int s_SwizzleFieldHash = "SwizzleField".GetHashCode();

bool m_IsPOT = false;
Dictionary<TextureImporterType, Action[]> m_AdvanceInspectorGUI = new Dictionary<TextureImporterType, Action[]>();
int m_PlatformSettingsIndex;
Expand Down Expand Up @@ -131,6 +133,7 @@ public override void OnEnable()
m_AlphaSource = textureImporterSettingsSP.FindPropertyRelative("m_AlphaSource");
m_MipMapMode = textureImporterSettingsSP.FindPropertyRelative("m_MipMapMode");
m_EnableMipMap = textureImporterSettingsSP.FindPropertyRelative("m_EnableMipMap");
m_Swizzle = textureImporterSettingsSP.FindPropertyRelative("m_Swizzle");
m_FadeOut = textureImporterSettingsSP.FindPropertyRelative("m_FadeOut");
m_BorderMipMap = textureImporterSettingsSP.FindPropertyRelative("m_BorderMipMap");
m_MipMapsPreserveCoverage = textureImporterSettingsSP.FindPropertyRelative("m_MipMapsPreserveCoverage");
Expand Down Expand Up @@ -159,15 +162,17 @@ public override void OnEnable()
AlphaHandlingGUI,
POTScaleGUI,
ReadableGUI,
MipMapGUI
MipMapGUI,
SwizzleGUI
};
m_AdvanceInspectorGUI.Add(TextureImporterType.Sprite, advanceGUIAction);

advanceGUIAction = new Action[]
{
POTScaleGUI,
ReadableGUI,
MipMapGUI
MipMapGUI,
SwizzleGUI
};
m_AdvanceInspectorGUI.Add(TextureImporterType.Default, advanceGUIAction);
LoadPlatformSettings();
Expand Down Expand Up @@ -911,7 +916,7 @@ void MipMapGUI()
{
EditorGUI.indentLevel++;
ToggleFromInt(m_BorderMipMap, s_Styles.borderMipMaps);
EditorGUILayout.Popup(s_Styles.mipMapFilter, m_MipMapMode.intValue, s_Styles.mipMapFilterOptions);
m_MipMapMode.intValue = EditorGUILayout.Popup(s_Styles.mipMapFilter, m_MipMapMode.intValue, s_Styles.mipMapFilterOptions);

ToggleFromInt(m_MipMapsPreserveCoverage, s_Styles.mipMapsPreserveCoverage);
if (m_MipMapsPreserveCoverage.intValue != 0 && !m_MipMapsPreserveCoverage.hasMultipleDifferentValues)
Expand Down Expand Up @@ -978,6 +983,42 @@ void ExportMosaicTexture()
AssetDatabase.Refresh();
}

static void SwizzleField(SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(EditorGUILayout.BeginHorizontal(), label, property);
EditorGUI.BeginChangeCheck();
EditorGUI.showMixedValue = property.hasMultipleDifferentValues;
var rect = EditorGUILayout.GetControlRect(true, EditorGUI.GetPropertyHeight(SerializedPropertyType.Vector4, label), EditorStyles.numberField);
var id = GUIUtility.GetControlID(s_SwizzleFieldHash, FocusType.Keyboard, rect);
rect = EditorGUI.PrefixLabel(rect, id, label);
var value = property.uintValue;
float w = (rect.width - 3 * EditorGUIUtility.standardVerticalSpacing) / 4;
var subRect = new Rect(rect) {width = w};
var oldIndent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
for (int i = 0; i < 4; i++)
{
int shift = 8 * i;
uint swz = (value >> shift) & 0xFF;
swz = (uint)EditorGUI.Popup(subRect, (int)swz, s_Styles.swizzleOptions);
value &= ~(0xFFu << shift);
value |= swz << shift;
subRect.x += w + EditorGUIUtility.standardVerticalSpacing;
}
EditorGUI.indentLevel = oldIndent;

EditorGUI.showMixedValue = false;
if (EditorGUI.EndChangeCheck())
property.uintValue = value;
EditorGUILayout.EndHorizontal();
EditorGUI.EndProperty();
}

void SwizzleGUI()
{
SwizzleField(m_Swizzle, s_Styles.swizzle);
}

/// <summary>
/// Implementation of AssetImporterEditor.ResetValues.
/// </summary>
Expand Down Expand Up @@ -1068,6 +1109,14 @@ void ITexturePlatformSettingsDataProvider.GetImporterSettings(int i, TextureImpo
GetSerializedPropertySettings(settings);
}

static (TextureImporterSwizzle r, TextureImporterSwizzle g, TextureImporterSwizzle b, TextureImporterSwizzle a) ConvertSwizzleRaw(uint value)
{
return ((TextureImporterSwizzle)((int)value & (int)byte.MaxValue),
(TextureImporterSwizzle) ((int)(value >> 8) & (int) byte.MaxValue),
(TextureImporterSwizzle) ((int)(value >> 16) & (int) byte.MaxValue),
(TextureImporterSwizzle) ((int)(value >> 24) & (int) byte.MaxValue));
}

internal TextureImporterSettings GetSerializedPropertySettings(TextureImporterSettings settings)
{
if (!m_AlphaSource.hasMultipleDifferentValues)
Expand Down Expand Up @@ -1100,6 +1149,15 @@ internal TextureImporterSettings GetSerializedPropertySettings(TextureImporterSe
if (!m_MipMapMode.hasMultipleDifferentValues)
settings.mipmapFilter = (TextureImporterMipFilter)m_MipMapMode.intValue;

if (!m_Swizzle.hasMultipleDifferentValues)
{
var swizzleValue = ConvertSwizzleRaw(m_Swizzle.uintValue);
settings.swizzleR = swizzleValue.r;
settings.swizzleG = swizzleValue.g;
settings.swizzleB = swizzleValue.b;
settings.swizzleA = swizzleValue.a;
}

if (!m_FadeOut.hasMultipleDifferentValues)
settings.fadeOut = m_FadeOut.intValue > 0;

Expand Down Expand Up @@ -1435,6 +1493,10 @@ internal class Styles
EditorGUIUtility.TrTextContent("Layer Management", "Layer merge settings.")
};

public readonly GUIContent swizzle = EditorGUIUtility.TrTextContent("Swizzle",
"Reorder and invert texture color channels. For each of R,G,B,A channels pick where the channel data comes from.");
public readonly string[] swizzleOptions = new[] {"R","G","B","A", "1-R","1-G","1-B","1-A", "0","1" };

public Styles()
{
// This is far from ideal, but it's better than having tons of logic in the GUI code itself.
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"name": "com.unity.2d.psdimporter",
"version": "7.0.0-pre.3",
"version": "7.0.0-pre.4",
"unity": "2022.1",
"displayName": "2D PSD Importer",
"description": "A ScriptedImporter for importing Adobe Photoshop PSB (Photoshop Big) file format. The ScriptedImporter is currently targeted for users who wants to create multi Sprite character animation using Unity 2D Animation Package.\n\nDocumentation for v5.0 is currently being updated.",
"description": "A ScriptedImporter for importing Adobe Photoshop PSB (Photoshop Big) file format. The ScriptedImporter is currently targeted for users who wants to create multi Sprite character animation using Unity 2D Animation Package.",
"keywords": [
"2d",
"psdimporter",
"assetimporter"
],
"category": "2D",
"dependencies": {
"com.unity.2d.animation": "8.0.0-pre.3",
"com.unity.2d.common": "7.0.0-pre.3",
"com.unity.2d.animation": "8.0.0-pre.4",
"com.unity.2d.common": "7.0.0-pre.4",
"com.unity.2d.sprite": "1.0.0"
},
"relatedPackages": {
"com.unity.2d.psdimporter.tests": "7.0.0-pre.3"
"com.unity.2d.psdimporter.tests": "7.0.0-pre.4"
},
"upmCi": {
"footprint": "5f2aad71791167dc2ab90b88c9659eb3088a025b"
"footprint": "f1a15a8f988d611cbb180bebea038e1fe445bf5e"
},
"repository": {
"url": "https://github.cds.internal.unity3d.com/unity/2d.git",
"type": "git",
"revision": "fc17ecffb5da8133c390aba47276756c1b22f123"
"revision": "2ccafc0d438881083c9c1ec71f92ef170494bd59"
}
}

0 comments on commit 392315a

Please sign in to comment.