I need to set the path for saving the pictures wit

2019-02-17 18:07发布

问题:

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!

回答1:

You can save files to Application.persistentDataPath. Unity apps do not have write permissions to Application.dataPath on Android or iOS devices.

Also, don't forget to join the path and the folder name with a forward slash:

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


回答2:

There are at least 3 ways for saving some kind of screenshot with Unity3D: Application.CaptureScreenshot, Texture2D.ReadPixels and 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.

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


回答3:

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


回答4:

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.

Hope this helps.