【菜菜丸的菜鸟教程】做一个简单的游戏截图相册(4)

本文介绍了如何在项目中创建一个相册已满的提示窗口,包括UI设计和Unity脚本的配合。开发者学习了如何制作提示窗口、设置弹出时机以及管理截图数量。

四、相册已满时的提示

(1)制作提示窗口的UI
在这个项目中,我们一共需要三个弹出窗口,所以这里先新建UI>Canvas,命名为Popup Window Canvas,将所有弹出窗口都放到这个画布下。
在Popup Window Canvas下新建UI>Panel,重命名为Alert,按照之前介绍的方法设置大小、位置、背景图等。(参考大小:长600宽300)
在Alert下新建UI>Text,放到Alert偏上居中的位置,调整大小。(参考大小:长460宽180)在Text的Text组件中输入以下文字并调整字体、字号等:
相册已满!
请至少删除一张图片后再尝试截屏
在Alert下新建UI>Button,放到Alert偏下居中的位置,命名为Alert Btn,调整大小。在Alert Btn的子物体Text的Text组件中,输入文字“确定”,并调整字体、字号等。
完成后的提示窗口UI将类似于下图:
做完提示窗口UI后,取消勾选Alert,令其隐藏。
(2)当相册已满时,弹出提示窗口
这一步并不难,之前我们已经做完了截图功能,现在我们只需要加一个条件,也就是当已有截图数量小于相册格子数时才进行截图,否则就显示警告窗口。因为这里我们声明了一个公开的GameObject变量,所以记得写完脚本后要回到Inspector中,将Alert挂载到相应栏位。
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); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值