在运行时我创建了一个网格。我想保存它,使其成为我项目中的一项 Assets ,这样我就不必每次都重新创建它。
如何将运行时创建的网格保存到我的资源文件夹中?
最佳答案
您可以使用 Mesh Serializer: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);
}
它不会保存到 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;
}
关于c# - 如何将 Unity3d Mesh 保存到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35674647/