Skip to content

Commit

Permalink
[unity] Fixed Resolve atlases dialog failing to load just created a…
Browse files Browse the repository at this point in the history
…tlas asset. Closes EsotericSoftware#2024. Fixed an unnecessary exception thrown after aborting skeleton import and adjusting blend mode materials.
  • Loading branch information
HaraldCsaszar committed Jan 19, 2022
1 parent 1d5bf3b commit 5f51705
Showing 1 changed file with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,8 @@ internal static SkeletonDataAsset IngestSpineProject (TextAsset spineJson, param
skeletonDataAsset.Clear();
}
var skeletonData = skeletonDataAsset.GetSkeletonData(true);
BlendModeMaterialsUtility.UpdateBlendModeMaterials(skeletonDataAsset, ref skeletonData);
if (skeletonData != null)
BlendModeMaterialsUtility.UpdateBlendModeMaterials(skeletonDataAsset, ref skeletonData);
AssetDatabase.SaveAssets();

return skeletonDataAsset;
Expand Down Expand Up @@ -975,9 +976,9 @@ public static void SkeletonImportDialog (string skeletonPath, List<AtlasAssetBas
);

switch (result) {
case -1:
//Debug.Log("Select Atlas");
AtlasAssetBase selectedAtlas = BrowseAtlasDialog(Path.GetDirectoryName(skeletonPath).Replace('\\', '/'));
case -1: { // Select Atlas
string pathForwardSlash = Path.GetDirectoryName(skeletonPath).Replace('\\', '/');
AtlasAssetBase selectedAtlas = BrowseAtlasDialog(pathForwardSlash, localAtlases);
if (selectedAtlas != null) {
localAtlases.Clear();
localAtlases.Add(selectedAtlas);
Expand All @@ -988,14 +989,17 @@ public static void SkeletonImportDialog (string skeletonPath, List<AtlasAssetBas
}
}
break;
case 0: // Resolve AtlasAssets...
var atlasList = MultiAtlasDialog(requiredPaths, Path.GetDirectoryName(skeletonPath).Replace('\\', '/'),
Path.GetFileNameWithoutExtension(skeletonPath));
}
case 0: { // Resolve AtlasAssets...
string pathForwardSlash = Path.GetDirectoryName(skeletonPath).Replace('\\', '/');
var atlasList = MultiAtlasDialog(requiredPaths, pathForwardSlash,
localAtlases, filename);
if (atlasList != null)
AssetUtility.IngestSpineProject(AssetDatabase.LoadAssetAtPath<TextAsset>(skeletonPath), atlasList.ToArray());

resolved = true;
break;
}
case 1: // Import without atlas
Debug.LogWarning("Imported with missing atlases. Skeleton will not render: " + Path.GetFileName(skeletonPath));
AssetUtility.IngestSpineProject(AssetDatabase.LoadAssetAtPath<TextAsset>(skeletonPath), new AtlasAssetBase[] { });
Expand All @@ -1009,7 +1013,9 @@ public static void SkeletonImportDialog (string skeletonPath, List<AtlasAssetBas
}
}

public static List<AtlasAssetBase> MultiAtlasDialog (List<string> requiredPaths, string initialDirectory, string filename = "") {
public static List<AtlasAssetBase> MultiAtlasDialog (List<string> requiredPaths, string initialDirectory,
List<AtlasAssetBase> localAtlases, string filename = "") {

List<AtlasAssetBase> atlasAssets = new List<AtlasAssetBase>();
bool resolved = false;
string lastAtlasPath = initialDirectory;
Expand Down Expand Up @@ -1064,7 +1070,7 @@ public static List<AtlasAssetBase> MultiAtlasDialog (List<string> requiredPaths,

switch (result) {
case 0: // Browse...
AtlasAssetBase selectedAtlasAsset = BrowseAtlasDialog(lastAtlasPath);
AtlasAssetBase selectedAtlasAsset = BrowseAtlasDialog(lastAtlasPath, localAtlases);
if (selectedAtlasAsset != null) {
if (!atlasAssets.Contains(selectedAtlasAsset)) {
var atlas = selectedAtlasAsset.GetAtlas();
Expand Down Expand Up @@ -1092,20 +1098,28 @@ public static List<AtlasAssetBase> MultiAtlasDialog (List<string> requiredPaths,
return atlasAssets;
}

public static AtlasAssetBase BrowseAtlasDialog (string dirPath) {
public static AtlasAssetBase BrowseAtlasDialog (string dirPath, List<AtlasAssetBase> localAtlases) {
string path = EditorUtility.OpenFilePanel("Select AtlasAsset...", dirPath, "asset");
if (path == "")
return null; // Canceled or closed by user.

int subLen = Application.dataPath.Length - 6;
string assetRelativePath = path.Substring(subLen, path.Length - subLen).Replace("\\", "/");

var obj = AssetDatabase.LoadAssetAtPath(assetRelativePath, typeof(AtlasAssetBase));
UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(assetRelativePath, typeof(AtlasAssetBase));
if (obj == null) {
// atlas assets that were just created fail to load, search localAtlases
foreach (AtlasAssetBase localAtlas in localAtlases) {
string newAtlasPath = AssetDatabase.GetAssetPath(localAtlas);
if (newAtlasPath == assetRelativePath)
return localAtlas;
}
}

if (obj == null || !(obj is AtlasAssetBase)) {
Debug.Log("Chosen asset was not of type AtlasAssetBase");
return null;
}

return (AtlasAssetBase)obj;
}
#endregion
Expand Down

0 comments on commit 5f51705

Please sign in to comment.