• DoTween结束后删除对象


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using DG.Tweening;
    
    public class UIDamage : MonoBehaviour {
    
        public Text numText;
        private Camera uiCamera;
        RectTransform rectTrans;
        List<Tween> numList = new List<Tween>();
        void Awake() {
            uiCamera = CameraController.Instance.transform.GetComponentInChildren<Camera>();
            rectTrans = transform.GetComponent<RectTransform>();
            EventCenter.AddListener<Vector3, int, int>(EGameEvent.ShowEffectNumber, ShowEffectNumber);
        }
    
        //public void ConnectButonOnClick() {
        //    UIManager.Instance.PushPanel(UIType.CONNECT_PANEL);
        //}
        private void OnDestroy()
        {
            EventCenter.AddListener<Vector3, int, int>(EGameEvent.ShowEffectNumber, ShowEffectNumber);
        }
    
        /// <summary>
        /// 根据EffectType不同显示不同颜色,或者不同内容
        /// </summary>
        /// <param name="worldPos"></param>
        /// <param name="effectNum"></param>
        /// <param name="effectType"></param>
        void ShowEffectNumber(Vector3 worldPos, int effectNum, int effectType)
        {
            Vector2 v2 = WorldToUgui(worldPos);
            Text num = Object.Instantiate(numText);
    
            num.transform.SetParent(this.transform);       
            num.transform.localPosition = new Vector3(v2.x, v2.y, 0);
            num.transform.localScale = Vector3.one;
            num.transform.localEulerAngles = Vector3.zero;
            num.gameObject.SetActive(true);
            num.text = "-" + effectNum;
            Tweener tween = num.transform.DOLocalMoveY(num.transform.localPosition.y + 100, 2f);
            tween.SetAutoKill(false);
            numList.Add(tween);
            tween.OnComplete(MyComplete);
        }
        void MyComplete()
        {
            for(int i= numList.Count - 1; i > -1; i--)
            {
                if (numList[i].IsComplete())
                {
                    numList[i].Kill();
                    Destroy((numList[i].target as Transform).gameObject);
                    numList.RemoveAt(i);
                }
            }
        }
        /// <summary>
        /// 将世界坐标转换为Ugui坐标
        /// 这种算法,cavas不能用填充,只能用居中
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public Vector2 WorldToUgui(Vector3 position)
        {
    
            Vector2 screenPoint = Camera.main.WorldToScreenPoint(position);//世界坐标转换为屏幕坐标
            Vector2 screenSize = new Vector2(Screen.width, Screen.height);
            screenPoint -= screenSize / 2;//将屏幕坐标变换为以屏幕中心为原点
            Vector2 anchorPos = screenPoint / screenSize * rectTrans.sizeDelta;//缩放得到UGUI坐标
            return anchorPos;
        }
    }

    这里的numList[i].target,是Tween.target。是调用DOLocalMoveY的对象。所以这里是个Transform。

    这里还有一个世界转UGUI的方法。

  • 相关阅读:
    24丨基础篇:Linux磁盘I-O是怎么工作的(上)
    23 | 基础篇:Linux 文件系统是怎么工作的?
    45 | 答疑(五):网络收发过程中,缓冲区位置在哪里?
    selenium相关:通过location 和 size 获取元素所在像素位置和尺寸,截取图片ROI
    python排序算法之冒泡,选择,插入
    pyspider和pyquery总结
    python之基于libsvm识别数字验证码
    selenium执行JavaScript语句:控制滚动条 聚焦元素 改变下拉选项
    python导入import
    【转】利用 selenium 的 webdrive 驱动 headless chrome
  • 原文地址:https://www.cnblogs.com/kuluodisi/p/13747102.html
Copyright © 2020-2023  润新知