赞
踩
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; } } }
脚本
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; } }
首先创建一个 Image 添加上 CanvasGroup 组件 注意 子节点加上一个 Image 用于显示成就是否完成 需要显示的CanvasGroup 拖上去
把 Image隐藏 就是把 CanvasGroup 的Alpha调成0
最终效果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。