示意
顯示提示信息.gif
使用方式
在任意處調(diào)用
GameTipsSys.Instance.ShowLog(提示文字內(nèi)容,顯示時長);
使用說明
依賴代碼:
需手動在場景中放置一個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個T暝浮!");
}
}
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);
}
}