accessing user documents folder on unity3d

2019-09-07 04:09发布

问题:

is it possible to get the path to the current user documents folder on Unity3D using UnityScript? I am trying to access it mainly on desktop systems (Windows, Linux or OSX). On mobile systems, Application.persistentDataPath do the trick for me, but for desktop I would like to use the documents folder where the users can see and change the files easily.

回答1:

I don't know if Javascript has a way to get Windows special folders, but C# has Environment.GetFolderPath.

So one way of doing this is to create a C# script that will give you the My Documents path and put it in the Standard Assets folder. That way, javascript can call that script.

C# file

using System;
public class GetUserPathCSharp
{
    public static string GetUserPath()
    {
        return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    }
}

Javascript file

#pragma strict
function Start () {
    var test = GetUserPathCSharp.GetUserPath();
    print("Path is " + test);
}

Note: Make sure that the C# script is inside the a folder called Standard Assets. It's important.