首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unity3d删除画廊中显示的当前图片

Unity3d删除画廊中显示的当前图片
EN

Stack Overflow用户
提问于 2018-07-10 16:35:13
回答 1查看 633关注 0票数 1

我对C#和Unity.com相当陌生,所以我在这里有个小问题。我正在开发某种“照片展位应用程序”,这使得画廊成为了其中的很大一部分。然而,我设法整理出截图的部分,并展示给他们看,我的问题是,我似乎不知道如何删除画廊的图片(我是说,在应用程序中)。

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

代码语言:javascript
复制
tring path = Application.persistentDataPath;

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

    foreach (FileInfo f in info)
    {
        File.Delete(f.FullName);
    }

我不知道这是否有帮助,但这是我用来拍摄和保存截图的代码:

代码语言:javascript
复制
 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();

我在画廊里给他们看的那个:

代码语言:javascript
复制
   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();
    }
}

我希望这有意义?提前谢谢你!

无法知道如何删除画廊中显示的当前图片。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-10 17:16:21

您的文件路径存储在string[] files变量中。whichScreenShotIsShown变量是确定当前显示的路径的当前索引。这两个变量在ScreenShotPreview脚本中声明。

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

代码语言:javascript
复制
string currentFile = files[whichScreenShotIsShown];
File.Delete(currentFile );
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51270300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档