From f6e62dea9a11e5b9b1b68fe6644cd057364973fc Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 3 Jun 2022 15:48:26 +0900 Subject: [PATCH] =?UTF-8?q?readonly=20struct=20=E3=81=AA=E3=81=AE=E3=81=A7?= =?UTF-8?q?=20null=20=E3=82=92=E8=BF=94=E3=81=97=E3=81=A6=E5=A4=B1?= =?UTF-8?q?=E6=95=97=E3=82=92=E8=A1=A8=E7=8F=BE=E3=81=A7=E3=81=8D=E3=81=AA?= =?UTF-8?q?=E3=81=84=E3=80=82throw=20=E3=81=97=E3=81=A6=E3=81=9F=E3=81=AE?= =?UTF-8?q?=E3=81=A0=E3=81=91=E3=81=A9=E3=80=81=E9=A0=86=E6=AC=A1=20TryGet?= =?UTF-8?q?=20=E3=81=AB=E5=A4=89=E3=81=88=E3=81=A6=E3=81=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/VRM10/Editor/MigrationMenu.cs | 8 +++--- .../VRMShaders/GLTF/IO/Editor/PathObject.cs | 25 +++++++++++-------- .../GLTF/IO/Tests/PathObjectTests.cs | 2 +- 3 files changed, 21 insertions(+), 14 deletions(-) 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); } } }