本文实例为大家分享了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
}










