Unity实现通用的信息提示框
本文实例为大家分享了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); n【本文由:日本服务器 欢迎转载】eedScaleGameObject.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 }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。
【本文来源:迪拜服务器 转载请说明出处】