Jus*_*808 4 c# unity-game-engine
在运行时我创建了一个网格。我想保存它,因此它是我项目中的一项资产,因此我不必每次都重新创建它。
如何将运行时创建的网格保存到我的资产文件夹中?
您可以使用该网格序列化器:http ://wiki.unity3d.com/index.php?title= MeshSerializer2
public static void CacheItem(string url, Mesh mesh)
{
string path = Path.Combine(Application.persistentDataPath, url);
byte [] bytes = MeshSerializer.WriteMesh(mesh, true);
File.WriteAllBytes(path, bytes);
}
Run Code Online (Sandbox Code Playgroud)
它不会保存到 Asset 文件夹中,因为该文件夹在运行时不再存在。您很可能会将其保存到持久数据路径中,实际上该路径用于存储数据。
然后你可以反过来检索它:
public static Mesh GetCacheItem(string url)
{
string path = Path.Combine(Application.persistentDataPath, url);
if(File.Exists(path) == true)
{
byte [] bytes = File.ReadAllBytes(path);
return MeshSerializer.ReadMesh(bytes);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)