Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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# Unity3d删除库中显示的当前图片_C#_Unity3d - Fatal编程技术网

C# Unity3d删除库中显示的当前图片

C# Unity3d删除库中显示的当前图片,c#,unity3d,C#,Unity3d,我对C#and Unity相当陌生,所以这里有一个小问题。我正在开发某种“photobooth应用程序”,这使得画廊成为其中的一个重要部分。然而,我成功地整理了截图部分并展示了它们,我的问题是我似乎不知道如何从图库中删除图片(我的意思是,在应用程序内部) 到目前为止,我正在使用这段代码(只是为了给它一些感觉),但这段代码会擦除所有拍摄的照片,而不仅仅是当前显示的照片 tring path = Application.persistentDataPath; DirectoryInfo d

我对C#and Unity相当陌生,所以这里有一个小问题。我正在开发某种“photobooth应用程序”,这使得画廊成为其中的一个重要部分。然而,我成功地整理了截图部分并展示了它们,我的问题是我似乎不知道如何从图库中删除图片(我的意思是,在应用程序内部)

到目前为止,我正在使用这段代码(只是为了给它一些感觉),但这段代码会擦除所有拍摄的照片,而不仅仅是当前显示的照片

tring path = Application.persistentDataPath;

    DirectoryInfo dir = new DirectoryInfo(path);
    FileInfo[] info = dir.GetFiles("*.png");

    foreach (FileInfo f in info)
    {
        File.Delete(f.FullName);
    }
我不知道这是否有用,但这是我用来拍摄和保存屏幕截图的代码:

 yield return new WaitForEndOfFrame();

    string timeStamp = System.DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss");
    string fileName = "Screenshot" + timeStamp + ".png";
    string pathToSave = fileName;
    ScreenCapture.CaptureScreenshot(pathToSave);
    yield return new WaitForEndOfFrame();
还有,我在画廊里展示他们时用的那个:

   public class ScreenShotPreview : MonoBehaviour
{
    [SerializeField]
    GameObject Panel;
    [SerializeField]
    string sceneName;

string[] files = null;
int whichScreenShotIsShown = 0;

void Start()
{

    files = Directory.GetFiles(Application.persistentDataPath + "/", "*.png");
    if (files.Length > 0)
    {
        GetPictureAndShowIt();
    }
}

void GetPictureAndShowIt()
{
    string pathToFile = files[whichScreenShotIsShown];
    Texture2D texture = GetScreenshotImage(pathToFile);
    Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
    Panel.GetComponent<Image>().sprite = sp;
}

Texture2D GetScreenshotImage(string filePath)
{
    Texture2D texture = null;
    byte[] fileBytes;
    if (File.Exists(filePath))
    {
        fileBytes = File.ReadAllBytes(filePath);
        texture = new Texture2D(2, 2, TextureFormat.RGB24, false);
        texture.LoadImage(fileBytes);
    }
    return texture;
}

public void NextPicture()
{
    if (files.Length > 0)
    {
        whichScreenShotIsShown += 1;
        if (whichScreenShotIsShown > files.Length - 1)
            whichScreenShotIsShown = 0;
        GetPictureAndShowIt();
    }
}

public void PreviousPicture()
{
    if (files.Length > 0)
    {
        whichScreenShotIsShown -= 1;
        if (whichScreenShotIsShown < 0)
            whichScreenShotIsShown = files.Length - 1;
        GetPictureAndShowIt();
    }
}
公共类截屏预览:MonoBehavior
{
[序列化字段]
游戏对象面板;
[序列化字段]
字符串场景名称;
string[]files=null;
int,其中screenshotisshown=0;
void Start()
{
files=Directory.GetFiles(Application.persistentDataPath+“/”,“*.png”);
如果(files.Length>0)
{
GetPictureAndShowIt();
}
}
void GetPictureAndShowIt()
{
字符串pathToFile=文件[whichScreenShotIsShown];
Texture2D纹理=GetScreenshotImage(路径文件);
Sprite sp=Sprite.Create(纹理,新矩形(0,0,texture.width,texture.height),新向量2(0.5f,0.5f));
Panel.GetComponent().sprite=sp;
}
Texture2D GetScreenshotImage(字符串文件路径)
{
Texture2D texture=null;
字节[]文件字节;
if(File.Exists(filePath))
{
fileBytes=File.ReadAllBytes(文件路径);
纹理=新纹理2d(2,2,TextureFormat.RGB24,false);
加载图像(文件字节);
}
返回纹理;
}
公共无效下一张图片()
{
如果(files.Length>0)
{
其中Screenshotisshown+=1;
if(whichScreenShotIsShown>files.Length-1)
其中Screenshotshown=0;
GetPictureAndShowIt();
}
}
公开作废以前的图片()
{
如果(files.Length>0)
{
其中Screenshotisshown-=1;
如果(哪个屏幕显示<0)
whichScreenShotIsShown=files.Length-1;
GetPictureAndShowIt();
}
}
我希望它有意义?提前谢谢你


TLDR;无法确定如何删除库中显示的当前图片。

您的文件路径存储在
字符串[]中文件
变量。
whichScreenShotIsShown
变量是确定当前显示路径的当前索引。这两个变量在
屏幕快照预览
脚本中声明

因此,要删除当前文件,请执行以下操作:

string currentFile = files[whichScreenShotIsShown];
File.Delete(currentFile );

您可以在尝试删除正在显示的特定图像的位置显示代码吗?您已经向我们显示了删除代码的代码(删除所有内容)但是你从哪里来的呢正如我所说,我不太确定如何实现代码,所以我现在只是在解决这个问题。我正在编辑文章以添加它,谢谢。你需要知道你要删除的图像的文件名。我猜你可以以某种方式生成它们的列表吗?然后,在你的删除代码中,你只需要删除它们只删除这些。哦,这很有意义。你是说,查看所有保存的图片名称,然后删除特定路径,对吗?嗯。好的,我想我可以查看一下。非常感谢!