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的方法。

最新文章

  1. ReactNative 从环境和第一个demo说起,填坑教程
  2. bootstrapValidator.js 做表单验证
  3. codeforces 507B. Painting Pebbles 解题报告
  4. Android View坐标getLeft, getRight, getTop, getBottom
  5. 进程&amp;信号&amp;管道实践学习记录
  6. 时隔3年,再次折腾BlackBerry 8830!
  7. 【Hadoop代码笔记】Hadoop作业提交之JobTracker等相关功能模块初始化
  8. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: SELECT command denied to user’
  9. HDU-2149 Public Sale
  10. eclipse - tomcat 远程调试
  11. poj 1068 Parencodings(栈)
  12. myeclipse如何恢复已删除的文件和代码
  13. Arduino线程库ProtoThreads
  14. H5 音频
  15. Unity 游戏框架搭建 (三) MonoBehaviour单例的模板
  16. python并发编程之多线程二
  17. spring揭秘 读书笔记 一 IoC初探
  18. Handler学习小结
  19. 数据加密算法--详解DES加密算法原理与实现
  20. ONOS架构-系统组件

热门文章

  1. 记一次oracle单表改分区表 一波三折
  2. linux 内存泄漏检测原理及实现
  3. 新的学习历程-python4 input
  4. Google 官方 AppCrawles学习小结
  5. nginx转发tomcat之https不生效
  6. Docker部署netdata 性能实时监控
  7. Kubernetes--Pod生命周期中的重要行为
  8. 第九章:用Python处理省份城市编码数据
  9. 在百度云服务器上部署Django网站的经历
  10. 剑指 Offer 链表