一、创建一个实现模态窗体的脚本(遮罩面板参考:https://blog.csdn.net/xiaochenxihua/article/details/81272227)
/***
* Title:"智慧工厂" 项目
* 主题:公共层:UI遮罩管理器
* Description:
* 功能:实现弹出“模态窗体”
* Date:2018
* Version:0.1版本
* Author:Coffee
* Modify Recoder:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using kernal;
namespace Global
{
public class UIMaskManage : MonoBehaviour
{
public GameObject goTopPlane; //顶层面板
public GameObject goMaskPlane; //遮罩面板
private Camera _UICamera; //UI摄像机
private float _OriginalUICameraDepth; //原始UI摄像机的层深
private void Start()
{
//启用遮罩窗体
goMaskPlane.SetActive(true);
//得到UI摄像机的原始层深
_UICamera = GameObject.FindGameObjectWithTag("UICamera").GetComponent<Camera>();
if (_UICamera != null)
{
_OriginalUICameraDepth = _UICamera.depth;
}
else
{
Log.Write(GetType()+"/Start()/_UICamera is Null,Please Check!!!");
}
}
/// <summary>
/// 设置遮罩状态
/// </summary>
/// <param name="goDisplayPlane">需要显示的窗体</param>
public void SetMaskWindow(GameObject goDisplayPlane)
{
//顶层窗体下移
goDisplayPlane.transform.SetAsLastSibling();
//启用遮罩窗体
goMaskPlane.SetActive(true);
//淡出遮罩
goMaskPlane.GetComponent<FadeInAndOut>().SetPanelFadeOut();
//遮罩窗体下移
goMaskPlane.transform.SetAsLastSibling();
//显示窗体
goDisplayPlane.transform.SetAsLastSibling();
//增加当前UI摄像机的层深
if (_UICamera!=null)
{
_UICamera.depth = _OriginalUICameraDepth + 20;
}
}
/// <summary>
/// 取消遮罩窗体
/// </summary>
public void CancleMaskPlane()
{
//顶层窗体上移动
goTopPlane.transform.SetAsFirstSibling();
//淡入遮罩
goMaskPlane.GetComponent<FadeInAndOut>().SetPanelFadeIn();
//禁用遮罩窗体
goMaskPlane.SetActive(true);
//恢复UI摄像机的原始层深
_UICamera.depth = _OriginalUICameraDepth;
}
}//class_end
}
2、将该脚本添加给一个物体
3、使用方法