Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取文件时出现文件路径错误_C#_Android_Ios_Unity3d - Fatal编程技术网

C# 读取文件时出现文件路径错误

C# 读取文件时出现文件路径错误,c#,android,ios,unity3d,C#,Android,Ios,Unity3d,我正在为Android和iOS制作一个游戏,并使用这个脚本拍摄一个屏幕截图,当你获得高分时将其应用到一个精灵图像上getScreenshot()进行初始化。使用Application.persistentDataPath+“/”+screenshotFilename在windows上的播放器中运行良好但是我猜iOS可能需要\来代替,但是我仍然在Xcode中接收到函数LoadPNG()的NullReferenceException publicstaticstringscreenshotfilen

我正在为Android和iOS制作一个游戏,并使用这个脚本拍摄一个屏幕截图,当你获得高分时将其应用到一个精灵图像上<调用code>getScreenshot()进行初始化。使用
Application.persistentDataPath+“/”+screenshotFilename在windows上的播放器中运行良好
但是我猜iOS可能需要
\
来代替,但是我仍然在Xcode中接收到函数
LoadPNG()的NullReferenceException

publicstaticstringscreenshotfilename=“MM_Screenshot.png”;
字符串路径图像;
public-void-getScreenshot(){
pathToImage=Application.persistentDataPath+“\\”+屏幕快照文件名;
Application.CaptureScreenshot(路径图像);
开始例行程序(“加载屏幕截图”);
}
IEnumerator加载屏幕截图(){
Sprite NewSprite=新Sprite();
Texture2D SpriteTexture=LoadPNG(路径图像);
GameObject.Find(“CaptureImage”).GetComponent().sprite=sprite.Create(SpriteTexture,新Rect(0,0,SpriteTexture.width,SpriteTexture.height),新矢量2(0.5f,0.8f));
收益返回空;
}
公共静态纹理2d LoadPNG(字符串文件路径){
Texture2D tex=null;
字节[]文件数据;
if(File.Exists(filePath)){
fileData=File.ReadAllBytes(文件路径);
tex=新纹理2d(2,2);
tex.LoadImage(fileData);/…这将自动调整纹理尺寸。
}
返回tex;
}
public static string screenshotFilename = "MM_Screenshot.png";
string pathToImage;

public void getScreenshot() {
    pathToImage = Application.persistentDataPath + "\\" + screenshotFilename;
    Application.CaptureScreenshot (pathToImage);
    StartCoroutine("LoadScreenshot");
}

IEnumerator LoadScreenshot() {
    Sprite NewSprite = new Sprite();
    Texture2D SpriteTexture = LoadPNG(pathToImage);
    GameObject.Find("CaptureImage").GetComponent<Image>().sprite = Sprite.Create(SpriteTexture,new Rect(0, 0, SpriteTexture.width, SpriteTexture.height), new Vector2(0.5f, 0.8f));
    yield return null;
}

public static Texture2D LoadPNG(string filePath) {

    Texture2D tex = null;
    byte[] fileData;

    if (File.Exists(filePath))     {
        fileData = File.ReadAllBytes(filePath);
        tex = new Texture2D(2, 2);
        tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
    }
    return tex;
}