Last active
July 10, 2020 04:15
-
-
Save qwe321qwe321qwe321/74ed2c0e8722b0b88c84ddb402d253e6 to your computer and use it in GitHub Desktop.
The helper class to expose some *REQUIRED* non-public members in the UnityEditor.U2D.PSD.PSDImporter. Such as TextureImporterSettings(Readable), TextureImporterPlatformSettings, "Character Rig", and "Use Layer Grouping".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* The PSDImporterHelper is to expose some IMPORTANT non-public members in PSDImporter. | |
* Such as TextureImporterSettings(Readable), TextureImporterPlatformSettings, "Character Rig", and "Use Layer Grouping". | |
* Created by PeDev 2020 https://gist.github.com/qwe321qwe321qwe321/74ed2c0e8722b0b88c84ddb402d253e6 | |
*/ | |
#if UNITY_EDITOR | |
#define INCLUDE_PSD_IMPORTER // <---- Comment/Uncomment by yourself. | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEditor; | |
#if INCLUDE_PSD_IMPORTER | |
using UnityEditor.U2D.PSD; | |
#endif | |
using System.Reflection; | |
namespace Pedev { | |
#if INCLUDE_PSD_IMPORTER | |
/// <summary> | |
/// The helper class to expose some *REQUIRED* non-public members in PSDImporter. | |
/// </summary> | |
public static class PSDImporterHelper { | |
private static FieldInfo s_TextureImporterSettingsField = typeof(PSDImporter).GetField("m_TextureImporterSettings", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | |
private static FieldInfo s_CharacterModeField = typeof(PSDImporter).GetField("m_CharacterMode", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | |
private static FieldInfo s_GenerateGOHierarchyField = typeof(PSDImporter).GetField("m_GenerateGOHierarchy", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | |
private static FieldInfo s_PlatformSettingsField = typeof(PSDImporter).GetField("m_PlatformSettings", System.Reflection.BindingFlags.NonPublic | BindingFlags.Instance); | |
private static PropertyInfo s_TextureActualWidthProp = typeof(PSDImporter).GetProperty("textureActualWidth", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | |
private static PropertyInfo s_TextureActualHeightProp = typeof(PSDImporter).GetProperty("textureActualHeight", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); | |
/// <summary> | |
/// Set "Enable Read/Write" in Editor. | |
/// </summary> | |
/// <param name="importer"></param> | |
/// <param name="readable"></param> | |
public static void SetReadable(this PSDImporter importer, bool readable) { | |
TextureImporterSettings importerSettings = s_TextureImporterSettingsField.GetValue(importer) as TextureImporterSettings; | |
importerSettings.readable = readable; | |
EditorUtility.SetDirty(importer); | |
} | |
/// <summary> | |
/// Get "Enable Read/Write" in Editor. | |
/// </summary> | |
/// <param name="importer"></param> | |
/// <returns></returns> | |
public static bool GetReadable(this PSDImporter importer) { | |
TextureImporterSettings importerSettings = s_TextureImporterSettingsField.GetValue(importer) as TextureImporterSettings; | |
return importerSettings.readable; | |
} | |
/// <summary> | |
/// Get "Pixel Per Unit" in Editor. | |
/// </summary> | |
/// <param name="importer"></param> | |
/// <returns></returns> | |
public static float GetPixelsPerUnit(this PSDImporter importer) { | |
TextureImporterSettings importerSettings = s_TextureImporterSettingsField.GetValue(importer) as TextureImporterSettings; | |
return importerSettings.spritePixelsPerUnit; | |
} | |
/// <summary> | |
/// Get "Sprite Mesh Type" in Editor. | |
/// </summary> | |
/// <param name="importer"></param> | |
/// <returns></returns> | |
public static UnityEngine.SpriteMeshType GetSpriteMeshType(this PSDImporter importer) { | |
TextureImporterSettings importerSettings = s_TextureImporterSettingsField.GetValue(importer) as TextureImporterSettings; | |
return importerSettings.spriteMeshType; | |
} | |
/// <summary> | |
/// Get "Extrude Edges" in Editor | |
/// </summary> | |
/// <param name="importer"></param> | |
/// <returns></returns> | |
public static uint GetSpriteExtrude(this PSDImporter importer) { | |
TextureImporterSettings importerSettings = s_TextureImporterSettingsField.GetValue(importer) as TextureImporterSettings; | |
return importerSettings.spriteExtrude; | |
} | |
/// <summary> | |
/// Get TextureImporterSettings. | |
/// </summary> | |
/// <param name="originImporter"></param> | |
/// <returns></returns> | |
public static TextureImporterSettings GetImporterSettings(this PSDImporter originImporter) { | |
TextureImporterSettings importerSettings = s_TextureImporterSettingsField.GetValue(originImporter) as TextureImporterSettings; | |
return importerSettings; | |
} | |
/// <summary> | |
/// Get TextureImporterPlatformSettings by private method. | |
/// </summary> | |
/// <param name="importer"></param> | |
/// <returns></returns> | |
public static List<TextureImporterPlatformSettings> GetTextureImporterPlatformSettings(this PSDImporter importer) { | |
return s_PlatformSettingsField.GetValue(importer) as List<TextureImporterPlatformSettings>; | |
} | |
/// <summary> | |
/// Get textureActualWidth and textureActualHeight. | |
/// </summary> | |
/// <param name="importer"></param> | |
/// <param name="width"></param> | |
/// <param name="height"></param> | |
public static void GetTextureActualSize(this PSDImporter importer, out int width, out int height) { | |
width = (int)s_TextureActualWidthProp.GetValue(importer); | |
height = (int)s_TextureActualHeightProp.GetValue(importer); | |
} | |
/// <summary> | |
/// Get the PSDImporter.characterMode value which is "Character Rig" in Editor. | |
/// </summary> | |
/// <param name="importer"></param> | |
/// <param name="value"></param> | |
public static bool GetCharacterMode(this PSDImporter importer) { | |
bool characterMode = (bool)s_CharacterModeField.GetValue(importer); | |
return characterMode; | |
} | |
/// <summary> | |
/// Set the PSDImporter.characterMode value which is "Character Rig" in Editor. | |
/// </summary> | |
/// <param name="importer"></param> | |
/// <param name="value"></param> | |
public static void SetCharacterMode(this PSDImporter importer, bool value) { | |
s_CharacterModeField.SetValue(importer, value); | |
} | |
/// <summary> | |
/// Get the PSDImporter.m_GenerateGOHierarchy value which is "Use Layer Grouping" in Editor. | |
/// </summary> | |
/// <param name="importer"></param> | |
/// <param name="value"></param> | |
public static bool GetGenerateGOHierarchy(this PSDImporter importer) { | |
bool generateGOHierarchy = (bool)s_GenerateGOHierarchyField.GetValue(importer); | |
return generateGOHierarchy; | |
} | |
/// <summary> | |
/// Set the PSDImporter.m_GenerateGOHierarchy value which is "Use Layer Grouping" in Editor. | |
/// </summary> | |
/// <param name="importer"></param> | |
/// <param name="value"></param> | |
public static void SetGenerateGOHierarchy(this PSDImporter importer, bool value) { | |
s_GenerateGOHierarchyField.SetValue(importer, value); | |
} | |
} | |
#endif | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment