示意
显示提示信息.gif使用方式
在任意处调用
GameTipsSys.Instance.ShowLog(提示文字内容,显示时长);
使用说明
依赖代码:
需手动在场景中放置一个GameTips。
GameTipsSys.png
GameTip.png
相关代码
GameTipsSys.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 在游戏中显示提示文字,一段时间后自动消失。
// 需主动放置到场景中,只能放置1个。
public class GameTipsSys : MonoBehaviour
{
private static GameTipsSys _instance;
public static GameTipsSys Instance
{
get
{
if (_instance == null)
{
var allInstances = FindObjectsOfType<GameTipsSys>();
if (allInstances.Length > 0)
{
for (int i = allInstances.Length - 1; i >= 0; i--)
{
if (i == 0)
{
_instance = allInstances[i];
_instance.Init();
}
else
{
Destroy(allInstances[i]);
}
}
}
else
{
Debug.Log($"获取{nameof(GameTipsSys)}失败!需手动在场景中放置1个!!");
}
}
return _instance;
}
}
[SerializeField]
private GameTip _tipTemplate;
private void OnDestroy()
{
MyDestroy();
}
private void Init()
{
_tipTemplate.SetActive(false);
}
private void MyDestroy()
{
}
public void ShowLog(string content, float duration = 3)
{
var tip = GOPool.Instance.GetInstance(_tipTemplate.gameObject).GetComponent<GameTip>();
tip.transform.SetParent(transform);
tip.transform.localPosition = Vector3.zero;
tip.transform.localScale = Vector3.one;
tip.Init();
tip.ShowLog(content);
tip.SetActive(true);
MonoSys.Instance.DelayCall(duration, () =>
{
tip.MyDestroy();
GOPool.Instance.RecycleInstance(_tipTemplate.gameObject, tip.gameObject);
});
}
}
GameTip.cs
using System.Net.Mime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameTip : MonoBehaviour
{
[SerializeField]
private Text _text;
public void Init()
{
}
public void MyDestroy()
{
}
public void ShowLog(string content)
{
_text.text = content;
}
public void SetActive(bool isActive)
{
gameObject.SetActive(isActive);
}
}
网友评论