当前位置:   article > 正文

unity 用ugui制作成就提示框_unity如何做成就提示

unity如何做成就提示

2021.5.10 补充

using UnityEngine;

namespace PupilFramework
{
    /// <summary>
    /// 绝大多情况下,都不需要使用此单例类型.
    /// 请使用Singleton
    /// 不需要手动挂载
    /// </summary>
    public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
    {
        private static T _instance;

        /// <summary>
        /// 线程锁
        /// </summary>
        private static readonly object _lock = new object();

        /// <summary>
        /// 程序是否正在退出
        /// </summary>
        protected static bool ApplicationIsQuitting { get; private set; }

        /// <summary>
        /// 是否为全局单例
        /// </summary>
        protected static bool isGolbal = true;

        static MonoSingleton()
        {
            ApplicationIsQuitting = false;
        }

        public static T Instance
        {
            get
            {
                if (ApplicationIsQuitting)
                {
                    if (Debug.isDebugBuild)
                    {
                        Debug.LogWarning("[Singleton] "                            + typeof(T) +
                                         " already destroyed on application quit." +
                                         " Won't create again - returning null.");
                    }

                    return null;
                }

                lock (_lock)
                {
                    if (_instance == null)
                    {
                        // 先在场景中找寻
                        _instance = (T) FindObjectOfType(typeof(T));

                        if (FindObjectsOfType(typeof(T)).Length > 1)
                        {
                            if (Debug.isDebugBuild)
                            {
                                Debug.LogWarning("[Singleton] " + typeof(T).Name +
                                                 " should never be more than 1 in scene!");
                            }

                            return _instance;
                        }

                        // 场景中找不到就创建新物体挂载
                        if (_instance == null)
                        {
                            GameObject singletonObj = new GameObject();
                            _instance         = singletonObj.AddComponent<T>();
                            singletonObj.name = "(singleton) " + typeof(T);

                            if (isGolbal && Application.isPlaying)
                            {
                                DontDestroyOnLoad(singletonObj);
                            }

                            return _instance;
                        }
                    }

                    return _instance;
                }
            }
        }

        /// <summary>
        /// 当工程运行结束,在退出时,不允许访问单例
        /// </summary>
        public void OnApplicationQuit()
        {
            ApplicationIsQuitting = true;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97

脚本

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class animate : MonoSingleton<animate>
{
   
    public CanvasGroup tips1;
    public CanvasGroup tips2;

    public void ShowTips1(Sprite msg)
    {
       
        tips1.GetComponent<Image>().sprite = msg;

        tips1.alpha = 0;
        DOTween.Kill(tips1);
        Sequence sequence = tips1.DOSequence();
        sequence.Append(tips1.DOFade(1, 0.8f));
        sequence.AppendInterval(2.0f);
        sequence.Append(tips1.DOFade(0, 1.0f));
        
    }
    public void ShowTips2(Sprite msg)
    {
        tips2.GetComponent<Image>().sprite = msg;

        tips2.alpha = 0;
        DOTween.Kill(tips2);
        Sequence sequence = tips2.DOSequence();
        sequence.Append(tips2.DOFade(1, 0.8f));
        sequence.AppendInterval(2.0f);
        sequence.Append(tips2.DOFade(0, 1.0f));
    }
}

public static class GameObjExt
{
    public static Sequence DOSequence(this UnityEngine.Object seq)
    {
        Sequence sequence = DOTween.Sequence();
        sequence.target = seq;

        return sequence;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

首先创建一个 Image 添加上 CanvasGroup 组件 注意 子节点加上一个 Image 用于显示成就是否完成 需要显示的CanvasGroup 拖上去
在这里插入图片描述
把 Image隐藏 就是把 CanvasGroup 的Alpha调成0
最终效果
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/118382?site
推荐阅读
相关标签
  

闽ICP备14008679号