我对C#和Unity.com相当陌生,所以我在这里有个小问题。我正在开发某种“照片展位应用程序”,这使得画廊成为了其中的很大一部分。然而,我设法整理出截图的部分,并展示给他们看,我的问题是,我似乎不知道如何删除画廊的图片(我是说,在应用程序中)。
到目前为止,我正在使用这段代码(只是为了给它一些意义),但是这一点擦除了所有的照片,而不仅仅是当前显示的照片。
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();
}
}我希望这有意义?提前谢谢你!
无法知道如何删除画廊中显示的当前图片。
发布于 2018-07-10 17:16:21
您的文件路径存储在string[] files变量中。whichScreenShotIsShown变量是确定当前显示的路径的当前索引。这两个变量在ScreenShotPreview脚本中声明。
因此,要删除当前文件,您可以执行如下操作:
string currentFile = files[whichScreenShotIsShown];
File.Delete(currentFile );https://stackoverflow.com/questions/51270300
复制相似问题