[unity] More class segregation for SpineEditorUtilities.

This commit is contained in:
pharan 2018-04-17 08:01:10 +08:00
parent 2a792c0247
commit a04242cacf

View File

@ -81,9 +81,7 @@ namespace Spine.Unity.Editor {
public static Texture2D hingeChain;
public static Texture2D subMeshRenderer;
public static Texture2D skeletonDataAssetIcon;
public static Texture2D info;
public static Texture2D unity;
// public static Texture2D controllerIcon;
@ -163,8 +161,6 @@ namespace Spine.Unity.Editor {
static readonly List<ScriptableObject> protectFromStackGarbageCollection = new List<ScriptableObject>();
static HashSet<string> assetsImportedInWrongState = new HashSet<string>();
#if SPINE_TK2D
const float DEFAULT_DEFAULT_SCALE = 1f;
#else
@ -189,6 +185,10 @@ namespace Spine.Unity.Editor {
const string SHOW_HIERARCHY_ICONS_KEY = "SPINE_SHOW_HIERARCHY_ICONS";
public static bool showHierarchyIcons = DEFAULT_SHOW_HIERARCHY_ICONS;
const bool DEFAULT_SET_TEXTUREIMPORTER_SETTINGS = true;
const string SET_TEXTUREIMPORTER_SETTINGS_KEY = "SPINE_SET_TEXTUREIMPORTER_SETTINGS";
public static bool setTextureImporterSettings = DEFAULT_SET_TEXTUREIMPORTER_SETTINGS;
internal const float DEFAULT_MIPMAPBIAS = -0.5f;
public const float DEFAULT_SCENE_ICONS_SCALE = 1f;
@ -205,6 +205,7 @@ namespace Spine.Unity.Editor {
defaultZSpacing = EditorPrefs.GetFloat(DEFAULT_ZSPACING_KEY, DEFAULT_DEFAULT_ZSPACING);
defaultShader = EditorPrefs.GetString(DEFAULT_SHADER_KEY, DEFAULT_DEFAULT_SHADER);
showHierarchyIcons = EditorPrefs.GetBool(SHOW_HIERARCHY_ICONS_KEY, DEFAULT_SHOW_HIERARCHY_ICONS);
setTextureImporterSettings = EditorPrefs.GetBool(SET_TEXTUREIMPORTER_SETTINGS_KEY, DEFAULT_SET_TEXTUREIMPORTER_SETTINGS);
SpineHandles.handleScale = EditorPrefs.GetFloat(SCENE_ICONS_SCALE_KEY, DEFAULT_SCENE_ICONS_SCALE);
preferencesLoaded = true;
}
@ -285,6 +286,13 @@ namespace Spine.Unity.Editor {
EditorPrefs.SetString(DEFAULT_SHADER_KEY, defaultShader);
EditorGUILayout.Space();
EditorGUI.BeginChangeCheck();
setTextureImporterSettings = EditorGUILayout.Toggle(new GUIContent("Apply Atlas Texture Settings", "Apply the recommended settings for Texture Importers."), showHierarchyIcons);
if (EditorGUI.EndChangeCheck()) {
EditorPrefs.SetBool(SET_TEXTUREIMPORTER_SETTINGS_KEY, showHierarchyIcons);
SpineEditorHierarchyHandler.HierarchyIconsOnPlaymodeStateChanged();
}
EditorGUILayout.LabelField("Editor Instantiation", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
defaultZSpacing = EditorGUILayout.Slider("Default Slot Z-Spacing", defaultZSpacing, -0.1f, 0f);
@ -308,9 +316,9 @@ namespace Spine.Unity.Editor {
using (new GUILayout.HorizontalScope()) {
EditorGUILayout.PrefixLabel("Define TK2D");
if (GUILayout.Button("Enable", GUILayout.Width(64)))
EnableTK2D();
SpineTK2DEditorUtility.EnableTK2D();
if (GUILayout.Button("Disable", GUILayout.Width(64)))
DisableTK2D();
SpineTK2DEditorUtility.DisableTK2D();
}
}
#endregion
@ -624,12 +632,12 @@ namespace Spine.Unity.Editor {
break;
case ".json":
var jsonAsset = (TextAsset)AssetDatabase.LoadAssetAtPath(str, typeof(TextAsset));
if (jsonAsset != null && IsSpineData(jsonAsset))
if (jsonAsset != null && SkeletonDataFileValidator.IsSpineData(jsonAsset))
skeletonPaths.Add(str);
break;
case ".bytes":
if (str.ToLower().EndsWith(".skel.bytes", System.StringComparison.Ordinal)) {
if (IsSpineData((TextAsset)AssetDatabase.LoadAssetAtPath(str, typeof(TextAsset))))
if (SkeletonDataFileValidator.IsSpineData((TextAsset)AssetDatabase.LoadAssetAtPath(str, typeof(TextAsset))))
skeletonPaths.Add(str);
}
break;
@ -647,7 +655,7 @@ namespace Spine.Unity.Editor {
// Import skeletons and match them with atlases.
bool abortSkeletonImport = false;
foreach (string sp in skeletonPaths) {
if (!reimport && CheckForValidSkeletonData(sp)) {
if (!reimport && SkeletonDataFileValidator.CheckForValidSkeletonData(sp)) {
ReloadSkeletonData(sp);
continue;
}
@ -770,7 +778,6 @@ namespace Spine.Unity.Editor {
#endregion
#region Match SkeletonData with Atlases
//static readonly AttachmentType[] NonAtlasTypes = { AttachmentType.Boundingbox, AttachmentType.Path };
static readonly AttachmentType[] AtlasTypes = { AttachmentType.Region, AttachmentType.Linkedmesh, AttachmentType.Mesh };
static List<AtlasAsset> MultiAtlasDialog (List<string> requiredPaths, string initialDirectory, string filename = "") {
@ -1042,27 +1049,24 @@ namespace Spine.Unity.Editor {
string texturePath = assetPath + "/" + pageFiles[i];
Texture2D texture = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D));
if (setTextureImporterSettings) {
TextureImporter texImporter = (TextureImporter)TextureImporter.GetAtPath(texturePath);
if (texImporter == null) {
Debug.LogWarning(string.Format("{0} ::: Texture asset \"{1}\" not found. Skipping. Please check your atlas file for renamed files.", atlasAsset.name, texturePath));
continue;
}
#if UNITY_5_5_OR_NEWER
texImporter.textureCompression = TextureImporterCompression.Uncompressed;
texImporter.alphaSource = TextureImporterAlphaSource.FromInput;
#else
texImporter.textureType = TextureImporterType.Advanced;
texImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
#endif
texImporter.mipmapEnabled = false;
texImporter.alphaIsTransparency = false; // Prevent the texture importer from applying bleed to the transparent parts.
texImporter.alphaIsTransparency = false; // Prevent the texture importer from applying bleed to the transparent parts for PMA.
texImporter.spriteImportMode = SpriteImportMode.None;
texImporter.maxTextureSize = 2048;
EditorUtility.SetDirty(texImporter);
AssetDatabase.ImportAsset(texturePath);
AssetDatabase.SaveAssets();
}
string pageName = Path.GetFileNameWithoutExtension(pageFiles[i]);
@ -1116,7 +1120,7 @@ namespace Spine.Unity.Editor {
bool hasBakedRegions = false;
for (int i = 0; i < regions.Count; i++) {
AtlasRegion region = regions[i];
string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(region) + ".prefab").Replace("\\", "/");
string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeName(region.name) + ".prefab").Replace("\\", "/");
GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(GameObject));
if (prefab != null) {
BakeRegion(atlasAsset, region, false);
@ -1140,7 +1144,7 @@ namespace Spine.Unity.Editor {
string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset);
string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath);
string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name);
string bakedPrefabPath = Path.Combine(bakedDirPath, GetPathSafeRegionName(region) + ".prefab").Replace("\\", "/");
string bakedPrefabPath = Path.Combine(bakedDirPath, GetPathSafeName(region.name) + ".prefab").Replace("\\", "/");
GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(GameObject));
GameObject root;
@ -1239,12 +1243,13 @@ namespace Spine.Unity.Editor {
}
#endregion
#region Checking Methods
static int[][] compatibleBinaryVersions = { new[] {3, 6, 0}, new[] {3, 5, 0} };
#region SkeletonDataFileValidator
internal static class SkeletonDataFileValidator {
static int[][] compatibleBinaryVersions = { new[] { 3, 6, 0 }, new[] { 3, 5, 0 } };
static int[][] compatibleJsonVersions = { new[] { 3, 6, 0 }, new[] { 3, 7, 0 }, new[] { 3, 5, 0 } };
//static bool isFixVersionRequired = false;
static bool CheckForValidSkeletonData (string skeletonJSONPath) {
public static bool CheckForValidSkeletonData (string skeletonJSONPath) {
string dir = Path.GetDirectoryName(skeletonJSONPath);
TextAsset textAsset = (TextAsset)AssetDatabase.LoadAssetAtPath(skeletonJSONPath, typeof(TextAsset));
DirectoryInfo dirInfo = new DirectoryInfo(dir);
@ -1262,7 +1267,8 @@ namespace Spine.Unity.Editor {
}
public static bool IsSpineData (TextAsset asset) {
if (asset == null) return false;
if (asset == null)
return false;
bool isSpineData = false;
string rawVersion = null;
@ -1329,36 +1335,11 @@ namespace Spine.Unity.Editor {
return isSpineData;
}
}
#endregion
#region SkeletonAnimation Menu
// [MenuItem("Assets/Spine/Instantiate (SkeletonAnimation)", false, 10)]
// static void InstantiateSkeletonAnimation () {
// Object[] arr = Selection.objects;
// foreach (Object o in arr) {
// string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(o));
// string skinName = EditorPrefs.GetString(guid + "_lastSkin", "");
//
// InstantiateSkeletonAnimation((SkeletonDataAsset)o, skinName, false);
// SceneView.RepaintAll();
// }
// }
//
// [MenuItem("Assets/Spine/Instantiate (SkeletonAnimation)", true, 10)]
// static bool ValidateInstantiateSkeletonAnimation () {
// Object[] arr = Selection.objects;
//
// if (arr.Length == 0)
// return false;
//
// foreach (Object o in arr) {
// if (o.GetType() != typeof(SkeletonDataAsset))
// return false;
// }
//
// return true;
// }
public static void IngestAdvancedRenderSettings (SkeletonRenderer skeletonRenderer) {
const string PMAShaderQuery = "Spine/Skeleton";
const string TintBlackShaderQuery = "Tint Black";
@ -1501,10 +1482,11 @@ namespace Spine.Unity.Editor {
return anim;
}
#endif
#endif
#endregion
#region TK2D Support
#region SpineTK2DEditorUtility
internal static class SpineTK2DEditorUtility {
const string SPINE_TK2D_DEFINE = "SPINE_TK2D";
static bool IsInvalidGroup (BuildTargetGroup group) {
@ -1515,7 +1497,7 @@ namespace Spine.Unity.Editor {
group == BuildTargetGroup.Unknown;
}
static void EnableTK2D () {
internal static void EnableTK2D () {
bool added = false;
foreach (BuildTargetGroup group in System.Enum.GetValues(typeof(BuildTargetGroup))) {
if (IsInvalidGroup(group))
@ -1541,7 +1523,7 @@ namespace Spine.Unity.Editor {
}
static void DisableTK2D () {
internal static void DisableTK2D () {
bool removed = false;
foreach (BuildTargetGroup group in System.Enum.GetValues(typeof(BuildTargetGroup))) {
if (IsInvalidGroup(group))
@ -1565,10 +1547,15 @@ namespace Spine.Unity.Editor {
Debug.LogWarning("Already Removed Scripting Define Symbol " + SPINE_TK2D_DEFINE);
}
}
}
#endregion
public static string GetPathSafeRegionName (AtlasRegion region) {
return region.name.Replace("/", "_");
//public static string GetPathSafeRegionName (AtlasRegion region) {
// return region.name.Replace("/", "_");
//}
public static string GetPathSafeName (string name) {
return name.Replace("/", "_");
}
}