有什么方法可以根据收到的对象类型确定资产的范围吗?
Is there any way to determine the extent of an asset based on the type of object that is received?
我想知道是否有任何方法可以通过简单地传递对象在 Unity 中创建资产,并且可以确定扩展而无需逐一检查 getType 是否属于一种类型或另一个放置一个或另一个扩展名,这是一个例子:
public void SaveAsset(Object obj)
{
string ext = ""; //How to get this?
AssetDatabase.CreateAsset(obj, "Assets/" + obj.name + ext);
}
不,afaik 你必须按类型去..虽然检查列表似乎非常有限:
'.mat' for materials, '.cubemap' for cubemaps, '.GUISkin' for skins, '.anim' for animations and '.asset' for arbitrary other assets.
所以在大多数情况下它只是 .asset
除非你在做一些花哨的事情;)
having to go one by one checking with getType
实际上已经不是这样了!我认为从 c# 7 开始,您可以使用基于 switch
的类型,例如
string ext;
switch(obj)
{
case Material:
ext = ".mat";
break;
case Cubemap:
ext = ".Cubemap";
break;
case GUISkin:
ext = ".GUISkin";
break;
case AnimationClip:
ext = ".anim";
break;
default:
ext = ".asset";
break;
}
基本上这等于或多或少写
string ext;
if(obj is Material)
{
ext = ".mat";
}
else if(obj is Cubemap)
{
ext = ".Cubemap";
}
else if(obj is GUISkin)
{
ext = ".GUISkin";
}
else if(obj is AnimationClip)
{
ext = ".anim";
}
else
{
ext = ".asset";
}
另请参阅 Tutorial: Use pattern matching to build type-driven and data-driven algorithms so latest in c# 8 you can write the same thing also as a Pattern Matching,例如(虽然我不太喜欢那样)
var ext = obj switch
{
Material _ => ".mat",
Cubemap _ => ".cubemap",
GUISkin _ => ".GUISkin",
AnimationClip _ => ".anim",
_ => ".asset"
};
因为无论如何您的代码只会 运行 在编辑器本身中(在其他地方 AssetDatabase
不可用)性能不应该是您关心的问题,因为它不需要实时。
我想知道是否有任何方法可以通过简单地传递对象在 Unity 中创建资产,并且可以确定扩展而无需逐一检查 getType 是否属于一种类型或另一个放置一个或另一个扩展名,这是一个例子:
public void SaveAsset(Object obj)
{
string ext = ""; //How to get this?
AssetDatabase.CreateAsset(obj, "Assets/" + obj.name + ext);
}
不,afaik 你必须按类型去..虽然检查列表似乎非常有限:
'.mat' for materials, '.cubemap' for cubemaps, '.GUISkin' for skins, '.anim' for animations and '.asset' for arbitrary other assets.
所以在大多数情况下它只是 .asset
除非你在做一些花哨的事情;)
having to go one by one checking with getType
实际上已经不是这样了!我认为从 c# 7 开始,您可以使用基于 switch
的类型,例如
string ext;
switch(obj)
{
case Material:
ext = ".mat";
break;
case Cubemap:
ext = ".Cubemap";
break;
case GUISkin:
ext = ".GUISkin";
break;
case AnimationClip:
ext = ".anim";
break;
default:
ext = ".asset";
break;
}
基本上这等于或多或少写
string ext;
if(obj is Material)
{
ext = ".mat";
}
else if(obj is Cubemap)
{
ext = ".Cubemap";
}
else if(obj is GUISkin)
{
ext = ".GUISkin";
}
else if(obj is AnimationClip)
{
ext = ".anim";
}
else
{
ext = ".asset";
}
另请参阅 Tutorial: Use pattern matching to build type-driven and data-driven algorithms so latest in c# 8 you can write the same thing also as a Pattern Matching,例如(虽然我不太喜欢那样)
var ext = obj switch
{
Material _ => ".mat",
Cubemap _ => ".cubemap",
GUISkin _ => ".GUISkin",
AnimationClip _ => ".anim",
_ => ".asset"
};
因为无论如何您的代码只会 运行 在编辑器本身中(在其他地方 AssetDatabase
不可用)性能不应该是您关心的问题,因为它不需要实时。