diff --git a/Assets/VRM10/Editor/MigrationMenu.cs b/Assets/VRM10/Editor/MigrationMenu.cs index d62cb76d8..d497f79a8 100644 --- a/Assets/VRM10/Editor/MigrationMenu.cs +++ b/Assets/VRM10/Editor/MigrationMenu.cs @@ -13,11 +13,10 @@ namespace UniVRM10 [MenuItem(CONTEXT_MENU, true)] static bool Enable() { - if (Selection.activeObject == null) + if (!VRMShaders.PathObject.TryGetFromAsset(Selection.activeObject, out VRMShaders.PathObject path)) { return false; } - var path = VRMShaders.PathObject.FromAsset(Selection.activeObject); var isVrm = path.Extension.ToLower() == ".vrm"; return isVrm; } @@ -25,7 +24,10 @@ namespace UniVRM10 [MenuItem(CONTEXT_MENU, false)] static void Exec() { - var path = VRMShaders.PathObject.FromAsset(Selection.activeObject); + if (!VRMShaders.PathObject.TryGetFromAsset(Selection.activeObject, out VRMShaders.PathObject path)) + { + return; + } // migrate var vrm1Bytes = MigrationVrm.Migrate(path.ReadAllBytes()); diff --git a/Assets/VRMShaders/GLTF/IO/Editor/PathObject.cs b/Assets/VRMShaders/GLTF/IO/Editor/PathObject.cs index aef30c3b1..56efe3a9c 100644 --- a/Assets/VRMShaders/GLTF/IO/Editor/PathObject.cs +++ b/Assets/VRMShaders/GLTF/IO/Editor/PathObject.cs @@ -33,14 +33,14 @@ namespace VRMShaders /// Application.dataPath は Assets を得る。 /// /// - public string UnityPath + public string UnityAssetPath { get { var root = UnityRoot; if (!IsDescendantOf(UnityRoot)) { - throw new ArgumentException($"{FullPath} is not under UnityPath"); + throw new ArgumentException($"{FullPath} is not under UnityAssetPath"); } return FullPath.Substring(root.FullPath.Length + 1); } @@ -94,8 +94,8 @@ namespace VRMShaders { try { - var unityPath = UnityPath; - return $""; + var assetPath = UnityAssetPath; + return $""; } catch (ArgumentException) { @@ -112,23 +112,28 @@ namespace VRMShaders /// AssetDatabase が使うパス /// - public static PathObject FromUnityPath(string src) + public static PathObject FromUnityAssetPath(string src) { return UnityRoot.Child(src); } - public static PathObject FromAsset(UnityEngine.Object src) + public static bool TryGetFromAsset(UnityEngine.Object src, out PathObject dst) { if (src == null) { - throw new ArgumentNullException(); + dst = default; + return false; } + var assetPath = AssetDatabase.GetAssetPath(src); if (string.IsNullOrEmpty(assetPath)) { - throw new ArgumentException($"{src} is not asset"); + dst = default; + return false; } - return FromUnityPath(assetPath); + + dst = FromUnityAssetPath(assetPath); + return true; } public PathObject Child(string child) @@ -161,7 +166,7 @@ namespace VRMShaders public void ImportAsset() { - AssetDatabase.ImportAsset(UnityPath); + AssetDatabase.ImportAsset(UnityAssetPath); } public bool TrySaveDialog(string title, string name, out PathObject dst) diff --git a/Assets/VRMShaders/GLTF/IO/Tests/PathObjectTests.cs b/Assets/VRMShaders/GLTF/IO/Tests/PathObjectTests.cs index 04028497f..1a14bda0d 100644 --- a/Assets/VRMShaders/GLTF/IO/Tests/PathObjectTests.cs +++ b/Assets/VRMShaders/GLTF/IO/Tests/PathObjectTests.cs @@ -22,7 +22,7 @@ namespace VRMShaders Assert.AreEqual(PathObject.UnityRoot.Child("Assets"), PathObject.UnityAssets); Assert.AreEqual(PathObject.UnityAssets.Parent, PathObject.UnityRoot); - Assert.AreEqual("Assets", PathObject.UnityAssets.UnityPath); + Assert.AreEqual("Assets", PathObject.UnityAssets.UnityAssetPath); } } }