1层级结构如下
2在Image 上添加 以下组件就可以实现Image 与文字的自动大小匹配了
3 Text 不用修改其他组件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
///
/// 提示小窗口
///
public enum ToolTipType
{
error,//错误提示 红
normal,// 正常提示 蓝
none,// 没有 白
}
public class TooTipManager : MonoSingleton
public GameObject toolTipPrefab;
public Text content;
public Image bg;
public CanvasGroup canvasgRroup;
public float alpha;
public Vector3 prePos;
public bool haveLoad;
// Use this for initialization
void Start () {
if (!haveLoad) {
Init ();
}
if (GlobalConfig.currentDecutionModle == TypeOfEnums.DeductionModle.practice)
{
ShowTip ("欢迎来到练习模式", ToolTipType.normal);
}
}
// Update is called once per frame
void Update () {
canvasgRroup.alpha = alpha ;
}
//初始化
public void Init(){
toolTipPrefab = ResourcesMgr.Instance.Load (ResourcesMgr.ResourceType.UI, "toolTip", true);
toolTipPrefab.transform.SetParent (UI_Faced.Instance.gameObject.transform);
toolTipPrefab.transform.localPosition = new Vector3 (-226f,600,0);
toolTipPrefab.transform.SetAsFirstSibling();
content = toolTipPrefab.transform.GetChild(0).GetComponent
bg = toolTipPrefab.GetComponent
canvasgRroup = toolTipPrefab.GetComponent
alpha = 1;
}
// 保持提示大小------
public void KeepSameScale(){
}
public void ShowTip(string contentString,ToolTipType type,float speed = 2.5f)
{
if (content == null) {
Init ();
haveLoad = true;
}
content.text = contentString;
canvasgRroup.alpha = 1;
switch (type) {
case ToolTipType.error:
bg.color = Color.red;
break;
case ToolTipType.normal:
bg.color = new Color (14f/255,137f/255,178f/255,255f/255);
break;
case ToolTipType.none:
bg.color = Color.yellow;
break;
default:
break;
}
//toolTipPrefab.transform.DOKill (true);
toolTipPrefab.transform.DORestart ();
toolTipPrefab.transform.DOLocalMove (new Vector3 (-226f,400f,0f), speed, false).OnComplete(OnAnimationEnd).SetEase(Ease.Linear).OnStart(statrt);
}
Tweener t ;
bool isfadeing = false;
public void OnAnimationEnd()
{
isfadeing = true;
t = DOTween.To (() => alpha, x => alpha = x, 0, 1f).OnComplete (
() => {
toolTipPrefab.transform.localPosition = new Vector3 (-226f,600,0);
alpha = 1;
isfadeing = false ;
}
);
}
public void statrt(){
if (isfadeing) {
alpha = 1;
isfadeing = false ;
t.Kill ();
}
}
}