歡迎您光臨本站 註冊首頁

Unity實現通用的信息提示框

←手機掃碼閱讀     techdo @ 2020-06-22 , reply:0

本文實例為大家分享了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();      }            //註冊按鈕      ////// 註冊相關按鈕      ///public void ResigterBtn()      {        if (btnClose != null)        {          EventTriggerListener.Get(btnClose.gameObject).onClick += BtnCloseMethod;        }      }         ////// 縮放基礎設置      ///public void BaseSettings()   {        //物體基礎縮放設置        base.needScaleGameObject = this.gameObject.transform;        base.needScaleGameObject.gameObject.SetActive(false);        base.needScaleGameObject.localScale = new Vector3(0, 0, 0);         }               ////// 開啟縮放      ///public void StartScale()      {        this.gameObject.SetActive(true);        //物體基礎縮放設置        base.ScaleMenu();      }         ////// 關閉按鈕的方法      //////private void BtnCloseMethod(GameObject go)      {        if (go==btnClose.gameObject)        {          //開啟縮放          StartScale();             //延遲銷燬物體          Destroy(this.gameObject, Global_Parameter.INTERVAL_TIME_0DOT5);        }      }         ////// 顯示提示框的標題、提示信息內容      //////提示的標題///提示的內容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;     //信息提示框            ////// 本類實例      //////public static InfoTipsFrame GetInstance()      {        if (_Instance==null)        {          _Instance = new InfoTipsFrame();        }        return _Instance;      }            ////// 顯示信息提示框與內容      //////提示的標題///提示的內容public void DisplayTipsFrameAndContents(GameObject infoTipsFrameParent, string TipsTitle, string TipsContents)      {        //獲取到信息提示框且顯示        GetInfoTipFrame(infoTipsFrameParent, true);        _InfoTipsFrame.GetComponent().DisplayTipsFrameTextContent(TipsContents, TipsTitle);      }            ////// 獲取到信息提示框      //////信息提示框的父物體///是否啟用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().BaseSettings();        }        _InfoTipsFrame.GetComponent().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;   //本腳本實例               ////// 本類實例      //////public static LoadPrefabs GetInstance()      {        if (_Instance==null)        {          _Instance = new LoadPrefabs();        }        return _Instance;      }         ////// 加載預製體      //////預製體路徑和名稱///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;        }         }         ////// 放大與縮小彈出菜單      ///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 + " 物體不存在請檢查!!!");        }      }         ////// 上下打開彈出菜單      ///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 + " 物體不存在請檢查!!!");        }         }         ////// 左右打開彈出菜單      ///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 + " 物體不存在請檢查!!!");        }         }            ////// 隱藏遊戲物體      ///IEnumerator HideGameObject()      {        yield return new WaitForSeconds(scaleMenuSpeed);        needScaleGameObject.gameObject.SetActive(false);      }         ////// 基礎面板設置      //////需要縮放的物體///物體縮放類型///縮放的速度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  }

  


[techdo ] Unity實現通用的信息提示框已經有249次圍觀

http://coctec.com/docs/program/show-post-239408.html