unity 一直提示 无法将类型“string”隐式转换为“UnityEngine.UI.Text”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity 一直提示 无法将类型“string”隐式转换为“UnityEngine.UI.Text”相关的知识,希望对你有一定的参考价值。

unity 一直提示 无法将类型“string”隐式转换为“UnityEngine.UI.Text” Cannot implicitly convert type `string' to `UnityEngine.UI.Text'

没用过unity但肯定不能这样赋值的,你的text是个对象,你应该对它的属性赋值,比如是text.text="加载完成"; 参考技术A text.text="加载完成";

Unity---toast提示

目录

1.向上飘动渐隐提示条--示意动画

 2.创建飘动的预制体

3. Image组件上加自适应组件 与 向上渐隐的Animation动画

4.Manager控制类

5.预制体脚本


1.向上飘动渐隐提示条--示意动画

 

20221025_151834

 2.创建飘动的预制体

3. Image组件上加自适应组件 与 向上渐隐的Animation动画

4.Manager控制类

场景中创建空物体挂载manager控制类

using System.Collections.Generic;
using UnityEngine;

public class ToastManager : MonoBehaviour

    private static ToastManager _ins;
    public static ToastManager ins
    
        get
        
            return _ins;
        
    

    public int poolCount = 9;
    public GameObject toastFab;

    private Queue<FlyTip> toastPool;

    void Awake()
    
        _ins = this;
        toastPool = new Queue<FlyTip>();
    

    private FlyTip GetToast()
    
        if (toastPool.Count > 0)
        
            return toastPool.Dequeue();
        
        else
        
            FlyTip flyTip = Instantiate(toastFab).GetComponent<FlyTip>();
            flyTip.transform.SetParent(transform);
            return flyTip;
        
    

    public void CheckToast(FlyTip flyTip)
    
        if (toastPool.Count > poolCount)
        
            Destroy(flyTip.gameObject);
        
        else
        
            toastPool.Enqueue(flyTip);
        
    

    public FlyTip ShowToast(string str)
    
        return ShowToast(str, FlyTip.DURING_SHOR);
    

    public FlyTip ShowToast(string str, float duringTime)
    
        return ShowToast(str, Color.black, Color.white, duringTime);
    

    public FlyTip ShowToast(string str, Color backColor, Color textColor)
    
        return ShowToast(str, backColor, textColor, FlyTip.DURING_SHOR);
    

    public FlyTip ShowToast(string str, Color backColor, Color textColor, float duringTime)
    
        FlyTip result = GetToast();
        result.SetColor(backColor, textColor);
        result.SetText(str);
        result.SetDuring(duringTime);
        result.SetPositionAndSize(FlyTip.Position.Center);
        result.Show();
        return result;
    

5.预制体脚本

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

public class FlyTip : MonoBehaviour

    public const float DURING_LONG = 1f;
    public const float DURING_SHOR = 0.5f;

    public enum Position
    
        Bottom,
        Top,
        Center
    

    public Image backGround;
    public Text toastStr;

    #region Top位置固定参数
    private static Vector2 TopAnchorMin = new Vector2(0, 1);
    private static Vector2 TopAnchorMax = new Vector2(1, 1);
    private static Vector2 TopPivot = new Vector2(0.5f, 1);
    private static Vector3 TopAnchoredPosition3D = new Vector3(0, 0, 0);
    private static Vector2 TopSizeDelta = new Vector3(0, 50);
    #endregion

    #region Bottom位置固定参数
    private static Vector2 BottomAnchorMin = new Vector2(0, 0);
    private static Vector2 BottomAnchorMax = new Vector2(1, 0);
    private static Vector2 BottomPivot = new Vector2(0.5f, 0);
    private static Vector3 BottomAnchoredPosition3D = new Vector3(0, 0, 0);
    private static Vector2 BottomSizeDelta = new Vector3(0, 50);
    #endregion

    #region 自定义位置参数--居中
    private Vector2 customAnchorMin = new Vector2(0, 0);
    private Vector2 customAnchorMax = new Vector2(1, 1);
    private Vector2 customPivot = new Vector2(0.5f, 1);
    private Vector3 customAnchoredPosition3D = new Vector3(0, 0, 0);
    private Vector2 customSizeDelta = new Vector3(0, 0);
    #endregion

    private RectTransform rectTransform;
    private Animator mAnimator;
    private float duringTime = DURING_SHOR;
    private Position showPosition = Position.Center;

    private void Awake()
    
        rectTransform = this.GetComponent<RectTransform>();
        mAnimator = backGround.GetComponent<Animator>();
    

    public void Show()
    
        gameObject.SetActive(true);
        transform.localScale = Vector3.one;

        SetPositionAndSize();

        mAnimator.Play("FlyTextAni");
        StartCoroutine("EndAni");
    

    IEnumerator EndAni()
    
        yield return new WaitForSeconds(duringTime);
        gameObject.SetActive(false);
        ToastManager.ins.CheckToast(this);
    
    public void SetColor(Color backColor, Color textColor)
    
        backGround.color = backColor;
        toastStr.color = textColor;
    

    public void SetDuring(float duringTime)
    
        this.duringTime = duringTime;
    

    public void SetText(string str)
    
        toastStr.text = str;
    

    public void SetPositionAndSize(Position showPosition)
    
        this.showPosition = showPosition;
    

    public void SetPositionAndSize(Vector2 min, Vector2 max, Vector2 pivot, Vector3 anchoredPosition, Vector2 size)
    
        showPosition = Position.Center;
        customAnchorMin = min;
        customAnchorMax = max;
        customPivot = pivot;
        customAnchoredPosition3D = anchoredPosition;
        customSizeDelta = size;
    

    private void SetPositionAndSize()
    
        if (showPosition == Position.Top)
        
            rectTransform.anchorMin = TopAnchorMin;
            rectTransform.anchorMax = TopAnchorMax;
            rectTransform.pivot = TopPivot;
            rectTransform.anchoredPosition3D = TopAnchoredPosition3D;
            rectTransform.sizeDelta = TopSizeDelta;
        
        else if (showPosition == Position.Bottom)
        
            rectTransform.anchorMin = BottomAnchorMin;
            rectTransform.anchorMax = BottomAnchorMax;
            rectTransform.pivot = BottomPivot;
            rectTransform.anchoredPosition3D = BottomAnchoredPosition3D;
            rectTransform.sizeDelta = BottomSizeDelta;
        
        else if (showPosition == Position.Center)
        
            rectTransform.anchorMin = customAnchorMin;
            rectTransform.anchorMax = customAnchorMax;
            rectTransform.pivot = customPivot;
            rectTransform.anchoredPosition3D = customAnchoredPosition3D;
            rectTransform.sizeDelta = customSizeDelta;
        
        else
        
            rectTransform.anchorMin = customAnchorMin;
            rectTransform.anchorMax = customAnchorMax;
            rectTransform.pivot = customPivot;
            rectTransform.anchoredPosition3D = customAnchoredPosition3D;
            rectTransform.sizeDelta = customSizeDelta;
        

    

以上是关于unity 一直提示 无法将类型“string”隐式转换为“UnityEngine.UI.Text”的主要内容,如果未能解决你的问题,请参考以下文章

C#中提示无法将string隐式转换为bool类型,请问怎么办?

C#类继承与重载的问题,Mat继承于Matrix,但提示无法将类型隐式转换为 存在一个显式转换 是不是缺少强制转

windows编程问题 错误提示“int无法隐式转换为bool”

无法找到延迟格式化的递归 JSON 类型作为隐式值

Unity---toast提示

提示:无法将类型为“System.DateTime”的对象强制转换为类型“System.String”。