您好,登錄后才能下訂單哦!
不懂Unity實現信息提示框的步驟?其實想解決這個問題也不難,下面讓小編帶著大家一起學習怎么去解決,希望大家閱讀完這篇文章后大所收獲。
1、創建一個信息提示框添加InfoTipsFrameScale腳本(然后將其制作為預制體)
2、編寫該信息提示框的控制腳本
/*** * Title:"智慧工廠" 項目 * 主題:全局層:提示框的動畫效果 * Description: * 功能:實現提示框的縮放功能 * 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; //關閉按鈕 public Text text_TipsTitle; //提示框的標題 public Text text_TipsContent; //提示框的內容 private void Start() { //注冊相關按鈕 ResigterBtn(); } //注冊按鈕 /// <summary> /// 注冊相關按鈕 /// </summary> public void ResigterBtn() { if (btnClose != null) { EventTriggerListener.Get(btnClose.gameObject).onClick += BtnCloseMethod; } } /// <summary> /// 縮放基礎設置 /// </summary> public void BaseSettings() { //物體基礎縮放設置 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); //物體基礎縮放設置 base.ScaleMenu(); } /// <summary> /// 關閉按鈕的方法 /// </summary> /// <param name="go"></param> private void BtnCloseMethod(GameObject go) { if (go==btnClose.gameObject) { //開啟縮放 StartScale(); //延遲銷毀物體 Destroy(this.gameObject, Global_Parameter.INTERVAL_TIME_0DOT5); } } /// <summary> /// 顯示提示框的標題、提示信息內容 /// </summary> /// <param name="Tipstitle">提示的標題</param> /// <param name="TipsContents">提示的內容</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:"智慧工廠" 項目 * 主題:全局層:信息提示框的啟用與隱藏 * Description: * 功能:實現提示信息框的加載、動畫顯示與隱藏(單例模式) * 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; //本類實例 private Transform _InfoTipsFrame; //信息提示框 /// <summary> /// 本類實例 /// </summary> /// <returns></returns> public static InfoTipsFrame GetInstance() { if (_Instance==null) { _Instance = new InfoTipsFrame(); } return _Instance; } /// <summary> /// 顯示信息提示框與內容 /// </summary> /// <param name="Tipstitle">提示的標題</param> /// <param name="TipsContents">提示的內容</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" 項目 * 主題: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)) { //顯示信息提示框及其內容 InfoTipsFrame.GetInstance().DisplayTipsFrameAndContents(infoTipsFrameParent, "信息提示", "不存在上一頁數據"); } } } }
將該腳本添加到一個物體上(同時禁用做好的信息提示框),運行點擊鍵盤A即可出現該信息提示框
備注:
1、資源加載方法
/*** * Title:"智慧工廠" 項目 * 主題:資源加載方法 * 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; //本腳本實例 /// <summary> /// 本類實例 /// </summary> /// <returns></returns> public static LoadPrefabs GetInstance() { if (_Instance==null) { _Instance = new LoadPrefabs(); } return _Instance; } /// <summary> /// 加載預制體 /// </summary> /// <param name="prefbasName">預制體路徑和名稱</param> /// <returns></returns> public GameObject GetLoadPrefab(string prefabsPathAndName) { //把資源加載到內存中 Object go = Resources.Load("Prefabs/" + prefabsPathAndName, typeof(GameObject)); //用加載得到的資源對象,實例化游戲對象,實現游戲物體的動態加載 GameObject LoadPrefab =UnityEngine.MonoBehaviour.Instantiate(go) as GameObject; //Debug.Log("加載的預制體="+LoadPrefab); return LoadPrefab; } }//class_end }
2、 通用縮放方法
/*** * Title:"醫藥自動化" 項目 * 主題:實現通用的物體縮放效果(父類) * Description: * 功能:實現物體的整體縮放、上下壓縮展開、左右壓縮展開動畫效果 * 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; //縮放的移動速度 private bool _IsScaleMark = false; //物體縮放的標識 protected ScaleType scaleType = ScaleType.None; //默認縮放的類型 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> /// 基礎面板設置 /// </summary> /// <param name="needScaleGo">需要縮放的物體</param> /// <param name="scaleType">物體縮放類型</param> /// <param name="scaleSpeed">縮放的速度</param> public void BasePanelSettings( GameObject needScaleGo,ScaleType scaleType, float scaleSpeed=0.3F) { //默認隱藏右側內容區域 if (needScaleGo != null) { needScaleGo.SetActive(false); //指定彈出菜單 needScaleGameObject = needScaleGo.transform; //指定需要彈出菜單執行的動畫類型 this.scaleType = scaleType; StartCoroutine(StartJudgeScaleType()); //物體縮放的速度 scaleMenuSpeed = scaleSpeed; } else { Log.Write(GetType() + "/BtnOnClickEvent()/使用手冊面板中按鈕點擊對應的面板右側內容不存在,請檢查" + needScaleGo + "物體"); } } }//class_end }
感謝你能夠認真閱讀完這篇文章,希望小編分享Unity實現信息提示框的步驟內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。