【问题标题】:I need to set the path for saving the pictures with an application for Android and IOS with UNITY我需要使用 UNITY 的 Android 和 IOS 应用程序设置保存图片的路径
【发布时间】:2012-07-28 08:38:23
【问题描述】:

我的实际代码:

function Update() { 
    if(Input.GetMouseButtonDown(0)) {
       Debug.Log("foto");
       Application.CaptureScreenshot(Application.dataPath + "Screenshot.png");
    }
}

我需要此功能的每张照片的输出路径。

谢谢!

【问题讨论】:

  • 统一标签适用于 Microsoft Unity。请不要滥用它。
  • 你找到解决这个问题的方法了吗?

标签: android path screenshot unity3d image


【解决方案1】:

Unity 的Application.persistentDataPath 始终指向IOS 中的文档文件夹,而android 则指向/mnt/android/data/application-bundle-identifier/

如果您不提供 Application.CaptureScreenshot 的路径,而是只提供屏幕截图的名称,它将自动保存在 IOS 的 Documents 文件夹中,尚未测试 android .

希望这会有所帮助。

【讨论】:

    【解决方案2】:
    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);
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      至少有3 ways 用于使用Unity3D 保存某种屏幕截图:Application.CaptureScreenshotTexture2D.ReadPixelsRenderTexture

      我只知道第一个:Application.CaptureScreenshot。这肯定是最简单的一个。它不采用路径,它将保存到Application.persistentDataPathApplication.dataPath + "/../",具体取决于您的平台。不幸的是,目前无法确定或仅通过代码知道 - 它只是以这种方式破坏。此外,它很慢,可能需要几秒钟的时间来处理。它作为一个单独的进程运行,因此您应该有一个 Update 或 Coroutine 等待文件被创建,然后再将其用于任何事情。

      如果您想选择文件的保存位置,您需要执行以下操作:

      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;
              }
          }
      }
      

      【讨论】:

      【解决方案4】:

      您可以将文件保存到Application.persistentDataPath。 Unity 应用在 Android 或 iOS 设备上没有对Application.dataPath 的写入权限。

      另外,不要忘记用正斜杠连接路径和文件夹名称:

      Application.CaptureScreenshot(Application.persistentDataPath + "/Screenshot.png");
      

      【讨论】:

      • 我试过你的代码。但它没有保存到android中的Application.persistentDataPath。为什么会这样?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2011-01-30
      • 1970-01-01
      相关资源
      最近更新 更多