Unity 模拟系统提示功能,制作提示框定时消失,不同类型的提示效果

Unity 提示功能

效果:

请添加图片描述

分析:

  • 这里用到了对象池,时间一到便要回收提示框,再次使用要置顶显示内容
  • 每个提示框都可以修改颜色,内容,存在的时间等信息
  • Scroll View能够根据内容的多少进行自适应高度

代码:

提示框功能
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TFramework;

public class InfoBoxController : MonoBehaviourSimplify
{
    
    
    [HideInInspector]
    public MessageManager messageManager;
    public Image image;
    public Text infoText;
    /// <summary>
    /// 消息提示
    /// </summary>
    /// <param name="type">1:正常【绿框】、2:错误【黄框】、3:引导【蓝框】、4:系统错误【红框】</param>
    /// <param name="info">内容</param>
    /// <param name="delayTime">显示时间</param>
    public void ShowMassameBox(string type,string info,float delayTime)
    {
    
    
        //1:正常【绿框】、2:错误【黄框】、3:引导【蓝框】、4:系统错误【红框】
        switch (type)
        {
    
    
            case "1"://正常【绿框】
                image.color = Color.green;
                break;
            case "2"://错误【黄框】
                image.color = Color.yellow;
                break;
            case "3"://引导【蓝框】
                image.color = Color.blue;
                break;
            case "4"://系统错误【红框】
                image.color = Color.red;
                break;

        }
        infoText.text = info;
        Delay(delayTime, () => {
    
     messageManager.infoBoxPool.Recycle(gameObject); 
        gameObject.SetActive(false);//时间一到,进行回收并关闭自身
        });
    }

    protected override void OnBeforeDestroy()
    {
    
    

    }
}

提示管理器
using System.Collections;
using System.Collections.Generic;
using TFramework;
using UnityEngine;
using UnityEngine.UI;

public class MessageManager : MonoBehaviour
{
    
    
    public GameObject InfoBox;
    public RectTransform InfoBoxRect;
    public SimpleObjectPool<GameObject> infoBoxPool;
    private void Awake()
    {
    
    
        infoBoxPool = new SimpleObjectPool<GameObject>(addNewPoolFunc, null, 1);
    }
    GameObject addNewPoolFunc()
    {
    
    
        GameObject obj = Instantiate(InfoBox);
        obj.transform.SetParent(InfoBoxRect);
        return obj;
    }
    /// <summary>
    /// 弹出消息,1:正常【绿框】、2:错误【黄框】、3:引导【蓝框】、4:系统错误【红框】
    /// </summary>
    /// <param name="type">1:正常【绿框】、2:错误【黄框】、3:引导【蓝框】、4:系统错误【红框】</param>
    /// <param name="info">内容</param>
    /// <param name="delayTime">显示时间</param>
    public void ShowMassameContents(string type,string info,float delayTime)
    {
    
    
        GameObject fishOne = infoBoxPool.Allocate();
        fishOne.SetActive(true);
        InfoBoxController infoBoxController = fishOne.GetComponent<InfoBoxController>();
        infoBoxController.messageManager = this;
        infoBoxController.ShowMassameBox(type, info, delayTime);
        infoBoxController.transform.SetAsFirstSibling();//排到第一位
    }
}

测试代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HintTest : MonoBehaviour
{
    
    
    public MessageManager messageManager;
    public bool test1 = false;
    public bool test2 = false;
    public bool test3 = false;
    public bool test4 = false;
    void Start()
    {
    
    
        messageManager.ShowMassameContents("3", "测试开始时提示信息,显示时间10s", 10);
    }
    int index = 0;
    // Update is called once per frame
    void Update()
    {
    
    
    	//测试不同类型
        if (test1)
        {
    
    
            test1 = false;
            messageManager.ShowMassameContents("1", "提示信息"+ index + ",显示时间5s", 5);
            index++;
        }
        if (test2)
        {
    
    
            test2 = false;
            messageManager.ShowMassameContents("2", "提示信息" + index + ",显示时间5s", 5);
            index++;
        }
        if (test3)
        {
    
    
            test3 = false;
            messageManager.ShowMassameContents("3", "提示信息" + index + ",显示时间5s", 5);
            index++;
        }
        if (test4)
        {
    
    
            test4 = false;
            messageManager.ShowMassameContents("4", "提示信息" + index + ",显示时间5s", 5);
            index++;
        }
    }
}

其他:

发现一个可以利用Unity自身功能制作一个自适应文字内容的边框,原理非常简单,限制子物体的大小,再根据子物体限制自身大小

效果:

请添加图片描述

实现方法:

在这里插入图片描述

下载Demo

点击下载Demo

猜你喜欢

转载自blog.csdn.net/CTangZe/article/details/128480996
今日推荐