我们正面临Unity的问题。我们正在开发一个IPAD应用程序。一个功能是截取屏幕截图并将其用作按钮。因为IPAD中的树目录不存在Resources文件夹,所以我们不能使用“Resources.Load”,即使如此,因为这个功能必须如何工作,所以纹理在开头就不可用了,所以我们最初不能把它放在Resources文件夹中。我们已经尝试了几种解决方案,比如在IPAD中创建一个Resources文件夹(在Documents文件夹中)并尝试从中加载纹理,甚至是这段代码:
public Texture LoadTextureFromFile(string filename)
{
Texture2D texture = new Texture2D(1024, 768);
FileStream fs = new FileStream(filename, FileMode.Open,
FileAccess.Read);
byte[] imageData = new byte[fs.Length];
fs.Read(imageData, 0, (int)fs.Length);
texture.LoadImage(imageData);
return (texture as Texture);
}
非常感谢任何帮助。我们已经坚持了几天这个问题
答案 0 :(得分:1)
您可以尝试以下操作:
Texture2D tex = new Texture2D(Screen.width, Screen.height);
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
tex.Apply();
将tex
放在按钮上。
如果要将其保存到文件中以供日后使用,可以执行以下操作:
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
Destroy( tex );
您将拥有一个可以随意使用的PNG文件。
当您保存文件并想要从Documents文件夹中的某处加载文件时,您可以尝试以下操作:
WWW imageToLoadPath = new WWW(/var/mobile/Applications/XXXXYYYY-XXXX-XXXX-XXXX-XXXXYYYYZZZZ/Documents/sample.png) //rendering texture
yield return imageToLoadPath; // <- the actual load of file
imageToLoadPath.LoadImageIntoTexture(buttonObject.renderer.material.mainTexture as Texture2D);
那些X和Y是您的应用程序的ID代码。当您运行一次应用程序时,您可以在Application.dataPath
中找到该文件夹。你可以偷看并检查你需要填写什么。