Merge pull request #1676 from ousttrue/fix/pathobject_TryGetFromAsset

PathObject 修正。TryGetFromAsset
This commit is contained in:
ousttrue 2022-06-06 19:16:00 +09:00 committed by GitHub
commit 6ee22b1491
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 14 deletions

View File

@ -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());

View File

@ -33,14 +33,14 @@ namespace VRMShaders
/// Application.dataPath は Assets を得る。
/// </summary>
/// <returns></returns>
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 $"<unity:{unityPath}>";
var assetPath = UnityAssetPath;
return $"<unity:{assetPath}>";
}
catch (ArgumentException)
{
@ -112,23 +112,28 @@ namespace VRMShaders
/// <param name="src">AssetDatabase が使うパス</param>
/// <returns></returns>
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)

View File

@ -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);
}
}
}