Unity實現通用的資訊提示框
阿新 • • 發佈:2019-02-03
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
}