zoukankan      html  css  js  c++  java
  • 2D UI 跟随3D 物体(自带缩放)

    在项目中会经常遇到血条跟随啊(要求做成2D血条),或者其他的2D UI跟随,那么小生现在在这里已经写好组件,可以供大家直接使用,直接拿到接口便可以使用了,别的不说先上代码

    //--------------------------------------------
    //            NGUI: HUD Text
    // Copyright 漏 2012 Tasharen Entertainment
    //--------------------------------------------
    
    using UnityEngine;
    
    /// <summary>
    /// Attaching this script to an object will make it visibly follow another object, even if the two are using different cameras to draw them.
    /// </summary>
    
    [AddComponentMenu("NGUI/Examples/Follow Target")]
    public class UIFollowTarget : MonoBehaviour
    {
        /// <summary>
        /// 3D target that this object will be positioned above.
        /// </summary>
    
        public Transform target;
    
        /// <summary>
        /// Game camera to use.
        /// </summary>
    
        public Camera gameCamera;
    
        /// <summary>
        /// UI camera to use.
        /// </summary>
    
        public Camera uiCamera;
    
        /// <summary>
        /// Whether the children will be disabled when this object is no longer visible.
        /// </summary>
    
        public bool disableIfInvisible = true;
    
        Transform mTrans;
        bool mIsVisible = false;
        Vector3 pos;
        /// <summary>
        /// Cache the transform;
        /// </summary>
    
        void Awake () { mTrans = transform; }
    
        /// <summary>
        /// Find both the UI camera and the game camera so they can be used for the position calculations
        /// </summary>
    
        void Start()
        {
            if (target != null)
            {
                if (gameCamera == null) gameCamera = NGUITools.FindCameraForLayer(target.gameObject.layer);
                if (uiCamera == null) uiCamera = NGUITools.FindCameraForLayer(gameObject.layer);
                SetVisible(false);
            }
            else
            {
                Debug.LogError("Expected to have 'target' set to a valid transform", this);
                enabled = false;
            }
        }
    
        /// <summary>
        /// Enable or disable child objects.
        /// </summary>
    
        void SetVisible (bool val)
        {
            mIsVisible = val;
    
            for (int i = 0, imax = mTrans.childCount; i < imax; ++i)
            {
                NGUITools.SetActive(mTrans.GetChild(i).gameObject, val);
            }
        }
    
        /// <summary>
        /// Update the position of the HUD object every frame such that is position correctly over top of its real world object.
        /// </summary>
    
        void Update()
        {
            if (target != null)
            {
                pos = gameCamera.WorldToViewportPoint(target.position);
            }
    
            // Determine the visibility and the target alpha
            bool isVisible = (gameCamera.orthographic || pos.z > 0f) && (!disableIfInvisible || (pos.x > 0f && pos.x < 1f && pos.y > 0f && pos.y < 1f));
    
            // Update the visibility flag
            if (mIsVisible != isVisible) SetVisible(isVisible);
    
            // If visible, update the position
            if (isVisible)
            {
                transform.position = uiCamera.ViewportToWorldPoint(pos);
                pos = mTrans.localPosition;
                pos.x = Mathf.FloorToInt(pos.x);
                pos.y = Mathf.FloorToInt(pos.y);
                pos.z = 0f;
                mTrans.localPosition = pos;
            }
            OnUpdate(isVisible);
        }
    
        /// <summary>
        /// Custom update function.
        /// </summary>
    
        protected virtual void OnUpdate (bool isVisible) { }
    }
    
    
    
    
    
    
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ArchUIInfo : MonoBehaviour
    {
     //上面的脚本是NGUI插件代码咯,小生我现在还没这么厉害写的这么流,这个脚本就是我写的组件咯
        private bool m_bInited = false;
        private UILabel m_info = null;
        private UISprite m_foreground = null;
        private Transform m_target = null;
        private const string m_strRes = "Final/UI/ArchUI/ArchUIInfo";//做好的预支体
        private static UIRoot m_root;
        private float m_Scale;//缩放比例
        void Start()
        {
            Init();
        }
        private void Init()
        {
            if (m_bInited)
                return;
            m_bInited = true;
            m_info = GetChild("Label").GetComponent<UILabel>();
            m_foreground = GetChild("Sprite").GetComponent<UISprite>();
    
            m_Scale = Vector3.Distance(m_target.position, Camera.main.transform.position);
        }
        void Update() 
        {
            if (m_target != null)
            {
                float newScale = m_Scale / Vector3.Distance(m_target.position, Camera.main.transform.position);
                transform.position = WorldToUI(m_target.position);
                transform.localScale = Vector3.one * (newScale * 0.3f);
            }
        }
        public void SetData(string ArchName,Transform target)
        {
            m_target = target;
            Init();
            m_info.text = ArchName;
           // m_root = GameObject.FindObjectOfType<UIRoot>();
            UIFollowTarget m_UIFollowTarget = AddComponent<UIFollowTarget>(gameObject);
            if (m_UIFollowTarget != null)
                m_UIFollowTarget.target = target;
            //Camera camera = m_root.GetComponentInChildren<Camera>();
            Camera camera = transform.parent.root.GetComponentInChildren<Camera>();
            m_UIFollowTarget.uiCamera = camera;
            m_UIFollowTarget.gameCamera = Camera.main;
    
        }
        public static void ShowUI(BWUI ui,string ArchName, Transform target)
        {
            GameObject parent = null;
            if (ui == null)
            {
                m_root = GameObject.FindObjectOfType<UIRoot>();
                parent = m_root.gameObject;
            }
            else 
            {
                parent = ui.gameObject;
            }
            GameObject instObj = Resources.Load(m_strRes) as GameObject;
            if (instObj == null)
            {
                BwTrace.LogError("ArchUIInfo::ShowUI()...instObj:为空");
                return;
            }
            GameObject inst = NGUITools.AddChild(parent, instObj);
            if (inst == null)
            {
                BwTrace.LogError("ArchUIInfo::ShowUI()...inst:为空");
                return;
            }
            inst.name = instObj.name;
    
            ArchUIInfo _ArchUIInfo = inst.GetComponent<ArchUIInfo>();
            if (_ArchUIInfo == null)
              _ArchUIInfo=inst.AddComponent<ArchUIInfo>();
            _ArchUIInfo.SetData(ArchName, target);
          
        }
    
        private Vector3 WorldToUI(Vector3 point)
        {
            Vector3 pt = Camera.main.WorldToScreenPoint(point);
            Vector3 ff = UICamera.currentCamera.ScreenToWorldPoint(pt);
            ff.z = 0;
            return ff;
        }
    
        private  GameObject GetChild(string name)
        {
            if (name == "this")
                return gameObject;
            if (name == null)
                return null;
            Transform trans = gameObject.transform.Find(name);
            if (trans == null)
            {
                //Debug.LogError("gameObject.name:" + gameObject.name + ";GetChild()trans == null");
                return null;
            }
            return trans.gameObject;
        }
    
        private  T AddComponent<T>(GameObject obj) where T : Component
        {
            T co = obj.GetComponent<T>();
            if (co == null)
                co = obj.AddComponent<T>();
            return co;
        }
    
    
    }
  • 相关阅读:
    「2019冬令营提高组」原样输出
    FJWC2019
    P2763 试题库问题
    P3979 遥远的国度
    P2754 [CTSC1999]家园
    P1251 餐巾计划问题
    P1382 楼房
    P1384 幸运数与排列
    P4294 [WC2008]游览计划
    P3345 [ZJOI2015]幻想乡战略游戏
  • 原文地址:https://www.cnblogs.com/WindMan/p/8831444.html
Copyright © 2011-2022 走看看