• UI3D转2D平面显示UI3DTo2D血条显示


    UI3D转2D平面显示UI3DTo2D血条显示:

    using UnityEngine;

    using System.Collections;

    public class UI3DTo2D : MonoBehaviour

    {

        private GameObject _TargetEnemyObj;                //目标物体

        private Camera worldcamera;                        //世界相机

        private Camera guiCamera;                          //UI相机

        private Slider uiSlider;                           //敌人的血条

        public float FloHPPrefabsLength = 2F;              //血条预设长度

        public float FloHpPrefabsHeight = 1F;              //血条预设高度

        public float FloHpHeightPosset = 1.5F;             //血条高度偏移量

        //敌人生命数值

        private float _FloCurrentHP = -999;                //当前生命数值

        private float _FloMaxHP = -999;                    //最大生命数值

        // 设置目标敌人

        public void SetTargetEnemy(GameObject goEnemy)

        {

            _TargetEnemyObj = goEnemy;

        }

        //初始化目标对象变量

        void Start()

        {

            //本UI Label 对象尺寸

            this.transform.localScale = new Vector3(FloHPPrefabsLength, FloHpPrefabsHeight, 0);

            //得到UI Slider 控件

            uiSlider = this.gameObject.GetComponent<Slider>();

            //世界摄像机(主摄像机)

            worldcamera = Camera.main.gameObject.GetComponent<Camera>();

            //带标签Tag_UICamera的UI摄像机

            guiCamera = GameObject.FindGameObjectWithTag(Tag.Tag_UICamera).GetComponent<Camera>();

            //参数检查

            if (_TargetEnemyObj == null)

            {

                return;

            }

        }

        // 计算敌人血量

        void Update()

        {

            //判断帧数

            if (Time.frameCount % 2 == 0)

            {

                try

                {

                    //当前与最大生命值

                    _FloCurrentHP = _FloCurrentHP;  //变化的生命值(实时更新)

                    _FloMaxHP = _FloMaxHP;          //变化的生命值(实时更新)

                    //计算敌人血量

                    uiSlider.value = _FloCurrentHP / _FloMaxHP;

                    //本UI Label 对象尺寸

                    this.transform.localScale = new Vector3(FloHPPrefabsLength, FloHpPrefabsHeight, 0);

                    if (_FloCurrentHP <= (_FloMaxHP * 0.05))

                    {

                        //死亡销毁

                        Destroy(this.gameObject);

                    }

                }

                catch (System.Exception)

                {

                    throw;

                }

            }

        }

        // 敌人血量计算后,实时的把_TargetEnemyObj的世界坐标转换为UI世界坐标,通过世界摄像机,转屏幕坐标,再把3D敌人(世界摄像机)屏幕坐标,转UI摄像机的屏幕坐标

        void LateUpdate()

        {

            if (_TargetEnemyObj != null)

            {

                if (Time.frameCount % 2 == 0)

                {

                    //获取目标物体的屏幕坐标

                    Vector3 pos = worldcamera.WorldToScreenPoint(_TargetEnemyObj.transform.position);

                    //将屏幕坐标转换为UI的世界坐标

                    pos = guiCamera.ScreenToWorldPoint(pos);

                    //由于NGUI 2D界面的Z轴都为0,只取其X,Y坐标              

                    pos.z = 0;

                    //将修改过的坐标赋给UI界面               

                    transform.position = new Vector3(pos.x - 0.1f, pos.y + FloHpHeightPosset, pos.z);

                }

            }

        }

    }

    注:void LateUpdate()方法为摄像机转换坐标,是具体实现的过程,此方法为世界坐标和平面坐标之间的转换,可逆向操作实现

    支持个人观看使用,如商用或转载,请告知! -----萧朗(QQ:453929789 Email:xiaolang_xl@sina.com)
  • 相关阅读:
    16-hadoop-mapreduce简介
    centos7-windows10 双系统安装
    5.4 RDD编程---综合案例
    8.2 数据结构---字符串(查找)
    8.1 数据结构---字符串
    5.3 RDD编程---数据读写
    5.2 RDD编程---键值对RDD
    5.1 RDD编程
    4.Spark环境搭建和使用方法
    3.3 Spark的部署和应用方式
  • 原文地址:https://www.cnblogs.com/XiaoLang0/p/9613265.html
Copyright © 2020-2023  润新知