Unity實(shí)現(xiàn)通用的信息提示框
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)信息提示框的具體代碼,供大家參考,具體內(nèi)容如下
1、創(chuàng)建一個(gè)信息提示框添加InfoTipsFrameScale腳本(然后將其制作為預(yù)制體)
2、編寫該信息提示框的控制腳本
/*** * Title:"智慧工廠" 項(xiàng)目 * 主題:全局層:提示框的動(dòng)畫效果 * Description: * 功能:實(shí)現(xiàn)提示框的縮放功能 * Date:2018 * Version:0.1版本 * Author:Coffee * Modify Recoder: */ using System.Collections; using System.Collections.Generic; using UnityEngine; using Global; using kernal; using UnityEngine.UI; namespace View { public class InfoTipsFrameScale : Global_baseScalePopUp { private ScaleType _ScaleType = ScaleType.Scale; //縮放類型為Scale public Button btnClose; //關(guān)閉按鈕 public Text text_TipsTitle; //提示框的標(biāo)題 public Text text_TipsContent; //提示框的內(nèi)容 private void Start() { //注冊相關(guān)按鈕 ResigterBtn(); } //注冊按鈕 /// <summary> /// 注冊相關(guān)按鈕 /// </summary> public void ResigterBtn() { if (btnClose != null) { EventTriggerListener.Get(btnClose.gameObject).onClick += BtnCloseMethod; } } /// <summary> /// 縮放基礎(chǔ)設(shè)置 /// </summary> public void BaseSettings() { //物體基礎(chǔ)縮放設(shè)置 base.needScaleGameObject = this.gameObject.transform; base.needScaleGameObject.gameObject.SetActive(false); base.needScaleGameObject.localScale = new Vector3(0, 0, 0); } /// <summary> /// 開啟縮放 /// </summary> public void StartScale() { this.gameObject.SetActive(true); //物體基礎(chǔ)縮放設(shè)置 base.ScaleMenu(); } /// <summary> /// 關(guān)閉按鈕的方法 /// </summary> /// <param name="go"></param> private void BtnCloseMethod(GameObject go) { if (go==btnClose.gameObject) { //開啟縮放 StartScale(); //延遲銷毀物體 Destroy(this.gameObject, Global_Parameter.INTERVAL_TIME_0DOT5); } } /// <summary> /// 顯示提示框的標(biāo)題、提示信息內(nèi)容 /// </summary> /// <param name="Tipstitle">提示的標(biāo)題</param> /// <param name="TipsContents">提示的內(nèi)容</param> public void DisplayTipsFrameTextContent(string TipsContents,string Tipstitle = "信息提示") { if (text_TipsTitle!=null&&text_TipsContent!=null) { text_TipsTitle.text = Tipstitle; text_TipsContent.text = TipsContents; } } }//class_end } /*** * Title:"智慧工廠" 項(xiàng)目 * 主題:全局層:信息提示框的啟用與隱藏 * Description: * 功能:實(shí)現(xiàn)提示信息框的加載、動(dòng)畫顯示與隱藏(單例模式) * Date:2018 * Version:0.1版本 * Author:Coffee * Modify Recoder: */ using System.Collections; using System.Collections.Generic; using UnityEngine; using kernal; using View; namespace Global { public class InfoTipsFrame { private static InfoTipsFrame _Instance; //本類實(shí)例 private Transform _InfoTipsFrame; //信息提示框 /// <summary> /// 本類實(shí)例 /// </summary> /// <returns></returns> public static InfoTipsFrame GetInstance() { if (_Instance==null) { _Instance = new InfoTipsFrame(); } return _Instance; } /// <summary> /// 顯示信息提示框與內(nèi)容 /// </summary> /// <param name="Tipstitle">提示的標(biāo)題</param> /// <param name="TipsContents">提示的內(nèi)容</param> public void DisplayTipsFrameAndContents(GameObject infoTipsFrameParent, string TipsTitle, string TipsContents) { //獲取到信息提示框且顯示 GetInfoTipFrame(infoTipsFrameParent, true); _InfoTipsFrame.GetComponent<InfoTipsFrameScale>().DisplayTipsFrameTextContent(TipsContents, TipsTitle); } /// <summary> /// 獲取到信息提示框 /// </summary> /// <param name="infoTipsFrameParent">信息提示框的父物體</param> /// <param name="IsEnable">是否啟用</param> private void GetInfoTipFrame(GameObject infoTipsFrameParent,bool IsEnable) { _InfoTipsFrame = LoadPrefabs.GetInstance().GetLoadPrefab("TipsFrame/TipsFrame").transform; _InfoTipsFrame.parent = infoTipsFrameParent.transform.parent; _InfoTipsFrame.localPosition = new Vector3(0, 0, 0); _InfoTipsFrame.localScale = new Vector3(1, 1, 1); _InfoTipsFrame.gameObject.SetActive(IsEnable); if (IsEnable == true) { _InfoTipsFrame.GetComponent<InfoTipsFrameScale>().BaseSettings(); } _InfoTipsFrame.GetComponent<InfoTipsFrameScale>().StartScale(); } }//class_end }
3、使用方法
/*** * Title:"XXX" 項(xiàng)目 * 主題:XXX * Description: * 功能:XXX * Date:2017 * Version:0.1版本 * Author:Coffee * Modify Recoder: */ using Global; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace SimpleUIFrame { public class Test_InfoTipsFrame : MonoBehaviour { public GameObject infoTipsFrameParent; void Start() { } private void Update() { if (Input.GetKeyDown(KeyCode.A)) { //顯示信息提示框及其內(nèi)容 InfoTipsFrame.GetInstance().DisplayTipsFrameAndContents(infoTipsFrameParent, "信息提示", "不存在上一頁數(shù)據(jù)"); } } } }
將該腳本添加到一個(gè)物體上(同時(shí)禁用做好的信息提示框),運(yùn)行點(diǎn)擊鍵盤A即可出現(xiàn)該信息提示框
備注:
1、資源加載方法
/*** * Title:"智慧工廠" 項(xiàng)目 * 主題:資源加載方法 * Description: * 功能:XXX * Date:2018 * Version:0.1版本 * Author:Coffee * Modify Recoder: */ using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace kernal { public class LoadPrefabs { private static LoadPrefabs _Instance; //本腳本實(shí)例 /// <summary> /// 本類實(shí)例 /// </summary> /// <returns></returns> public static LoadPrefabs GetInstance() { if (_Instance==null) { _Instance = new LoadPrefabs(); } return _Instance; } /// <summary> /// 加載預(yù)制體 /// </summary> /// <param name="prefbasName">預(yù)制體路徑和名稱</param> /// <returns></returns> public GameObject GetLoadPrefab(string prefabsPathAndName) { //把資源加載到內(nèi)存中 Object go = Resources.Load("Prefabs/" + prefabsPathAndName, typeof(GameObject)); //用加載得到的資源對象,實(shí)例化游戲?qū)ο螅瑢?shí)現(xiàn)游戲物體的動(dòng)態(tài)加載 GameObject LoadPrefab =UnityEngine.MonoBehaviour.Instantiate(go) as GameObject; //Debug.Log("加載的預(yù)制體="+LoadPrefab); return LoadPrefab; } }//class_end }
2、 通用縮放方法
/*** * Title:"醫(yī)藥自動(dòng)化" 項(xiàng)目 * 主題:實(shí)現(xiàn)通用的物體縮放效果(父類) * Description: * 功能:實(shí)現(xiàn)物體的整體縮放、上下壓縮展開、左右壓縮展開動(dòng)畫效果 * Date:2017 * Version:0.1版本 * Author:Coffee * Modify Recoder: */ using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using DG.Tweening; using kernal; namespace Global { public class Global_baseScalePopUp : MonoBehaviour { protected Transform needScaleGameObject; //需要縮放的物體 protected float scaleMenuSpeed = 0.5F; //縮放的移動(dòng)速度 private bool _IsScaleMark = false; //物體縮放的標(biāo)識 protected ScaleType scaleType = ScaleType.None; //默認(rèn)縮放的類型 public IEnumerator StartJudgeScaleType() { yield return new WaitForSeconds(Global_Parameter.INTERVAL_TIME_0DOT3); switch (scaleType) { case ScaleType.None: //_NeedScaleGameObject.localScale = new Vector3(1, 1, 1); break; case ScaleType.Scale: needScaleGameObject.localScale = new Vector3(0, 0, 0); break; case ScaleType.UpAndDown: needScaleGameObject.localScale = new Vector3(1, 0, 1); break; case ScaleType.LeftAndRight: needScaleGameObject.localScale = new Vector3(0, 1, 1); break; default: break; } } /// <summary> /// 放大與縮小彈出菜單 /// </summary> public void ScaleMenu() { if (needScaleGameObject.gameObject != null) { if (_IsScaleMark == false) { needScaleGameObject.DOScale(new Vector3(1, 1, 1), scaleMenuSpeed); _IsScaleMark = true; } else { needScaleGameObject.DOScale(new Vector3(0, 0, 0), scaleMenuSpeed); _IsScaleMark = false; StartCoroutine("HideGameObject"); } } else { Debug.LogError(GetType() + "/Start()/_NeedScaleGameObject " + needScaleGameObject.gameObject + " 物體不存在請檢查?。。?); } } /// <summary> /// 上下打開彈出菜單 /// </summary> public void UpAndDown() { if (needScaleGameObject.gameObject != null) { if (_IsScaleMark == false) { needScaleGameObject.DOScale(new Vector3(1, 1, 1), scaleMenuSpeed); _IsScaleMark = true; } else { needScaleGameObject.DOScale(new Vector3(1, 0, 1), scaleMenuSpeed); _IsScaleMark = false; StartCoroutine("HideGameObject"); } } else { Debug.LogError(GetType() + "/Start()/_NeedScaleGameObject " + needScaleGameObject.gameObject + " 物體不存在請檢查?。。?); } } /// <summary> /// 左右打開彈出菜單 /// </summary> public void leftAndRight() { if (needScaleGameObject.gameObject != null) { if (_IsScaleMark == false) { needScaleGameObject.DOScale(new Vector3(1, 1, 1), scaleMenuSpeed); _IsScaleMark = true; } else { needScaleGameObject.DOScale(new Vector3(0, 1, 1), scaleMenuSpeed); _IsScaleMark = false; StartCoroutine("HideGameObject"); } } else { Debug.LogError(GetType() + "/Start()/_NeedScaleGameObject " + needScaleGameObject.gameObject + " 物體不存在請檢查?。?!"); } } /// <summary> /// 隱藏游戲物體 /// </summary> IEnumerator HideGameObject() { yield return new WaitForSeconds(scaleMenuSpeed); needScaleGameObject.gameObject.SetActive(false); } /// <summary> /// 基礎(chǔ)面板設(shè)置 /// </summary> /// <param name="needScaleGo">需要縮放的物體</param> /// <param name="scaleType">物體縮放類型</param> /// <param name="scaleSpeed">縮放的速度</param> public void BasePanelSettings( GameObject needScaleGo,ScaleType scaleType, float scaleSpeed=0.3F) { //默認(rèn)隱藏右側(cè)內(nèi)容區(qū)域 if (needScaleGo != null) { needScaleGo.SetActive(false); //指定彈出菜單 needScaleGameObject = needScaleGo.transform; //指定需要彈出菜單執(zhí)行的動(dòng)畫類型 this.scaleType = scaleType; StartCoroutine(StartJudgeScaleType()); //物體縮放的速度 scaleMenuSpeed = scaleSpeed; } else { Log.Write(GetType() + "/BtnOnClickEvent()/使用手冊面板中按鈕點(diǎn)擊對應(yīng)的面板右側(cè)內(nèi)容不存在,請檢查" + needScaleGo + "物體"); } } }//class_end }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c# Form中的鍵盤響應(yīng)具體實(shí)現(xiàn)思路
在全屏Form中加上鍵盤ESC的響應(yīng),實(shí)現(xiàn)的效果就是:全屏中press鍵盤上的Escape鍵,程序結(jié)束,具體實(shí)現(xiàn)步驟如下,感興趣的朋友可以參考下哈2013-06-06C#實(shí)現(xiàn)封裝常用Redis工具類的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)封裝常用Redis工具類的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03C# PDF轉(zhuǎn)圖片(JPG,Png)的項(xiàng)目實(shí)踐
本文主要介紹了C# PDF轉(zhuǎn)圖片(JPG,Png)的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05c#定時(shí)器和global實(shí)現(xiàn)自動(dòng)job示例
這篇文章主要介紹了c#定時(shí)器和global實(shí)現(xiàn)自動(dòng)job示例,大家參考使用吧2014-01-01c#通過進(jìn)程調(diào)用cmd判斷登錄用戶權(quán)限代碼分享
最近自己開發(fā)軟件需要讀取本地配置文件,因?yàn)榈卿浻脩舻臋?quán)限不夠會(huì)導(dǎo)致無法讀取文件進(jìn)而導(dǎo)致程序崩潰,查了一些解決方法,代碼分享如下2013-12-12