四、相册已满时的提示

public GameObject alertWindow;
void Start()
{
//给警告窗口的关闭按钮加上监听事件
alertBtn.onClick.AddListener(CloseAlertWindow);
}
void Update()
{
//按S键截图
if (Input.GetKeyDown(KeyCode.S))
{
//如果已有截图数量小于相册格子数,则进行截图
if(fileCount < slots.Count)
{
StartCoroutine(CaptureScreenshot());
}
//如果已有截图数量大于相册格子数,则显示警告窗口
else
{
this.GetComponent<AlbumManager>().alertWindow.SetActive(true);
}
}
}
//关闭警告窗口
public void CloseAlertWindow()
{
alertWindow.SetActive(false);
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System;
using UnityEngine.UI;
using System.IO;
using UnityEditor;
using System.Linq;
public class AlbumManager : MonoBehaviour
{
private string folderPath;
DirectoryInfo screenshotDir;
private int screenshotCount = 0;
static List<int> screenshotlist = new List<int>();
private string screenshotFileName;
private int fileCount = 0;
Texture2D[] textures;
Sprite[] sprites;
String[] screenshotNames;
String[] screenshotPaths;
public List<GameObject> slots = new List<GameObject>();
public GameObject album;
public Button albumCloseBtn;
bool isOpen = false;
public Button albumCloseBtn;
public GameObject alertWindow;
void Start()
{
folderPath = Application.persistentDataPath + "/Textures/";
screenshotDir = new DirectoryInfo(folderPath);
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
albumCloseBtn.onClick.AddListener(ShowOrHideAlbum);
screenshotlist.Add(screenshotCount);
screenshotCount = PlayerPrefs.GetInt("MaxNumber");
albumCloseBtn.onClick.AddListener(ShowOrHideAlbum);
alertBtn.onClick.AddListener(CloseAlertWindow);
ReplaceImages();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
ShowOrHideAlbum();
}
if (Input.GetKeyDown(KeyCode.S))
{
if(fileCount < slots.Count)
{
StartCoroutine(CaptureScreenshot());
}
else
{
this.GetComponent<AlbumManager>().alertWindow.SetActive(true);
}
}
}
private IEnumerator CaptureScreenshot()
{
screenshotCount ++;
screenshotlist.Add(screenshotCount);
PlayerPrefs.SetInt("MaxNumber", screenshotlist.Max());
screenshotFileName = "Screenshot_" + screenshotCount + ".png";
ScreenCapture.CaptureScreenshot(folderPath + screenshotFileName);
yield return new WaitForSeconds(0.5f);
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
}
public void CreateSprites()
{
FileInfo[] screenshotFiles = screenshotDir.GetFiles("*.png");
fileCount = screenshotDir.GetFiles("*.png").Length;
Array.Resize(ref textures, fileCount);
Array.Resize(ref sprites, fileCount);
Array.Resize(ref screenshotNames, fileCount);
Array.Resize(ref screenshotPaths, fileCount);
for (int i = 0; i < fileCount; i++)
{
textures[i] = new Texture2D (256, 256);
screenshotPaths[i] = screenshotFiles[i].FullName;
screenshotNames[i] = screenshotFiles[i].Name;
byte[] rawPNG = File.ReadAllBytes(screenshotPaths[i]);
textures[i].LoadImage(rawPNG);
sprites[i] = Sprite.Create(textures[i], new Rect(0f, 0f, textures[i].width, textures[i].height), Vector2.zero, 100.0f);
sprites[i].name = "Sprite" + i;
}
}
public void ReplaceImages()
{
CreateSprites();
for (int j = 0; j < slots.Count; j++)
{
slots[j].transform.GetChild(0).gameObject.GetComponent<Image>().sprite = null;
slots[j].transform.GetChild(0).gameObject.SetActive(false);
}
for (int i = 0; i < sprites.Length; i++)
{
if (slots[i].transform.GetChild(0).gameObject.activeInHierarchy == false)
{
slots[i].transform.GetChild(0).gameObject.SetActive(true);
slots[i].transform.GetChild(0).gameObject.GetComponent<Image>().sprite = sprites[i];
}
}
}
public void ShowOrHideAlbum()
{
isOpen = !isOpen;
album.SetActive(isOpen);
}
public void CloseAlertWindow()
{
alertWindow.SetActive(false);
}
267



被折叠的 条评论
为什么被折叠?



