本文实例为大家分享了Unity实现信息提示框的具体代码,供大家参考,具体内容如下
1、创建一个信息提示框添加InfoTipsframeScale脚本(然后将其制作为预制体)
2、编写该信息提示框的控制脚本
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
}
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、使用方法
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、资源加载方法
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、 通用缩放方法
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
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



