我需要使用 UNITY 的 Android 和 IOS 应用程序设置保存图片的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11725548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I need to set the path for saving the pictures with an application for Android and IOS with UNITY
提问by Fernando Moral
My actual code:
我的实际代码:
function Update() {
if(Input.GetMouseButtonDown(0)) {
Debug.Log("foto");
Application.CaptureScreenshot(Application.dataPath + "Screenshot.png");
}
}
I need the path for the output of every photo for this function.
我需要此功能的每张照片的输出路径。
Thanks!
谢谢!
回答by ddk
You can save files to Application.persistentDataPath. Unity apps do not have write permissions to Application.dataPath
on Android or iOS devices.
您可以将文件保存到Application.persistentDataPath。Unity 应用程序Application.dataPath
在 Android 或 iOS 设备上没有写入权限。
Also, don't forget to join the path and the folder name with a forward slash:
另外,不要忘记用正斜杠连接路径和文件夹名称:
Application.CaptureScreenshot(Application.persistentDataPath + "/Screenshot.png");
回答by Parvaz Bhaskar
Application.persistentDataPath
of unity always points towards documents folder in IOS
and to /mnt/android/data/application-bundle-identifier/
for android
.
And if you don't give the path for Application.CaptureScreenshot
put instead give only the name for the screenshot it'll automatically get saved in the Documents folder of IOS
haven't tested for android
.
Application.persistentDataPath
of unity 始终指向文档文件夹 inIOS
和/mnt/android/data/application-bundle-identifier/
for android
。
如果您不提供Application.CaptureScreenshot
put的路径,而只提供屏幕截图的名称,它会自动保存在IOS
尚未测试的 Documents 文件夹中android
。
Hope this helps.
希望这可以帮助。
回答by krodil
static function CaptureScreen(){
var filename;
var name :String = GuiScript.stringToEdit;
var allowedScreenshots:int=1000;
for(var i = 0; i <= allowedScreenshots; i++)
{
if(Application.platform == RuntimePlatform.Android){
filename = "/mnt/sdcard/Android/data/com.xxxx.Test01/files/" + name + i + ".png";
} else {
filename = name + i + ".png";
}
if(!System.IO.File.Exists(filename))
{
Application.CaptureScreenshot(name + i + ".png" );
print("ScreenDUMP made!");
print(i);
print(name);
return;
}
else
{
print("filename already exists");
print(i);
}
}
}
回答by cregox
There are at least 3 waysfor saving some kind of screenshot with Unity3D: Application.CaptureScreenshot
, Texture2D.ReadPixels
and RenderTexture
.
至少有3 种方法可以使用 Unity3D 保存某种屏幕截图:Application.CaptureScreenshot
,Texture2D.ReadPixels
和RenderTexture
.
I only know about the first one: Application.CaptureScreenshot
. It is for sure the simplest one. It doesn't take paths and it will save to Application.persistentDataPath
or Application.dataPath + "/../"
, depending on your platform. Unfortunately there's currently no way of knowing for sure or through code only - it's just broken in that way. Also, it is slow and it may take some seconds to process. It runs as a separate process so you should have an Update or a Coroutine waiting for the file to be created before using it for anything.
我只知道第一个:Application.CaptureScreenshot
。这肯定是最简单的。它不采用路径,它将保存到Application.persistentDataPath
或Application.dataPath + "/../"
,具体取决于您的平台。不幸的是,目前无法确定或仅通过代码知道 - 它只是以这种方式被破坏。此外,它很慢,可能需要几秒钟的时间来处理。它作为一个单独的进程运行,所以你应该有一个更新或一个协程等待文件被创建,然后再将它用于任何事情。
If you want to choose where the file will be saved with it, you need to do something like this:
如果您想选择文件的保存位置,您需要执行以下操作:
bool creatingFile = false;
string fileName = "Screenshot.png"
function Update() {
if(Input.GetMouseButtonDown(0)) {
Application.CaptureScreenshot(fileName);
creatingFile = true;
}
if (creatingFile) {
string origin = System.IO.Path.Combine(Application.persistentDataPath, fileName);
string destination = "/sdcard/ScreenCapture/" + fileName; // could be anything
if (System.IO.File.Exists(origin)) {
System.IO.File.Move(origin, destination);
creatingFile = false;
}
}
}