基于ugui的血条(资料备份)

来源:互联网 发布:单片机一键信号发射 编辑:程序博客网 时间:2024/06/15 03:33

自己用的资料备份。

基于ugui的血条  1控制位置(考虑像素适配)2实现立体视角(基于镜头角度计算偏移量。)

using XXXXXXX;using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class   HeadInfo  {    //对象    public GameObject go;//血条对象                                             // public KPlayer player;//血条所属玩家    public GameObject father;//血条的父对象是谁    //UI组件    private Transform canvas;//UI画布    public Image blood;//通过blood修改血量值        public RectTransform recTransform;//通过调用该矩阵转换    //数据    public float xOffset=0;//血条位置偏移量x    public float yOffset = 400f;//300f;//血条位置偏移量y    public int maxBlood;//最大血量    public int curBlood;//当前血量    public Text CrystalNumber;    public GridLayoutGroup glg;//元素布局    public Camera cm;//相机    public Text Name;//血条显示名称    public Text Level;//等级    public Transform myHeadInfo;//我的位置   //public Transform otherHeadInfo;//其他的位置      /// <summary>    ///     /// </summary>    /// <param name="f"></param>    /// <param name="P">0表示自己   1表示敌人   2表示 好友</param>    public HeadInfo(  GameObject f ,  RPlayer rPlayer,  int P=0 )//bool forMe, KPlayer play    {        //////   Debug.Log("PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP::::::::::"+P);        // cm = KCamera.I.CamFx;//特效相机        ////Debug.Log("创建血条!!!!!!!!!!!!!!");         cm = KCamera.I.camMain.gameObject.GetComponent<Camera>();  ///CameraFx        // cm=Camera        father = f;        canvas = GameObject.Find("Canvas").transform;        myHeadInfo = KHelper.GetChildByName(canvas, "MyHeadInfo");        ///  otherHeadInfo  = KHelper.GetChildByName(canvas, "OtherHeadInfo");        //xuqi.Log("+++++++++++++++++++++++++++isFriend"+isFriend);        if (P == 0)        {            go = Resources.Load<GameObject>("UI/HeadInfo");            myHeadInfo = KHelper.GetChildByName(canvas, "MyHeadInfo");        }        else if (P == 1)        {            go = Resources.Load<GameObject>("UI/HeadInfoEnemy");            myHeadInfo = KHelper.GetChildByName(canvas, "OtherHeadInfo");        }        else        {            myHeadInfo = KHelper.GetChildByName(canvas, "OtherHeadInfo");            go = Resources.Load<GameObject>("UI/HeadInfoFriend");        }                   go = GameObject.Instantiate(go);        string str = go.name;        go.name = go.name.Substring(0,str.Length -7);        KUIManagerInGame.I.AllHeadInfo.Add(go);///添加对象        recTransform = go.GetComponent<RectTransform>();               go.transform.parent = canvas;//myHeadInfo;//         blood = KHelper.GetChildByName(go.transform,"b").GetComponent<Image>();  //go.GetComponent<Slider>();        blood.fillAmount = 1f;        glg = KHelper.GetChildByName(go.transform,"Grid").GetComponent<GridLayoutGroup>();              Name= KHelper.GetChildByName(go.transform, "Name").GetComponent<Text>();        Name.text = rPlayer.userInfo.name;//uid;///        Level = KHelper.GetChildByName(go.transform, "LevelText").GetComponent<Text>();        Level.text = rPlayer.userInfo.level.ToString();        CrystalNumber = KHelper.GetChildByName(go.transform, "Number").GetComponent<Text>();    }    /// <summary>    /// 设置人物血条上的水晶数量    /// </summary>    /// <param name="Number"></param>    public void  SetCrystalNumber(int Number)    {        CrystalNumber.text = Number.ToString();        if (Number >= 30f)        {            if(isShowBaoshiP==false)                ShowBaoShiP();        }        else        {            if (isShowBaoshiP ==true)                HideBaoShiP();        }    }    public void ShowSunderArmorP()    {        Debug.Log("go  name :"+go.transform.name );        PlayFxGameUI.I. ShowSunderArmorP(go.transform);    }    public void HideSunderArmorP()    {        PlayFxGameUI.I.HideSunderArmorP(go.transform);    }    public void ShowZhenShiZhiTongP()    {        Debug.Log("go  name :" + go.transform.name);        PlayFxGameUI.I.ShowZhenShiZhiTongP(go.transform);    }    public void HideZhenShiZhiTongP()    {        PlayFxGameUI.I.HideZhenShiZhiTongP(go.transform);    }    public void ShowBuQuP()//废除    {        //PlayFxGameUI.I.ShowBuQuP(go.transform);        PlayFxGameUI.I.ShowBuQuP(father.transform);    }    public void HideBuQuP()//废除    {        PlayFxGameUI.I.HideBuQuP(father.transform);        //   PlayFxGameUI.I.HideBuQuP(go.transform);    }    public void ShowChaoFengP()    {        //PlayFxGameUI.I.ShowBuQuP(go.transform);        PlayFxGameUI.I.PlayFxChaoFeng(go.transform);    }    public void HideChaoFengP()    {        PlayFxGameUI.I.HideFxChaoFeng(go.transform);        //   PlayFxGameUI.I.HideBuQuP(go.transform);    }    public bool isShowBaoshiP = false;    public void ShowBaoShiP()    {        isShowBaoshiP = true;        PlayFxGameUI.I.ShowBaoShiP(go.transform);    }    public void HideBaoShiP()    {        isShowBaoshiP = false;        PlayFxGameUI.I.HideBaoShiP(go.transform);    }    /// <summary>    /// 设置血条的分割线    /// </summary>    /// <param name="maxBlood"></param>    public void  SetBloodLine( int maxBlood)    {    ////    Debug.Log("设置血量 SetBloodLine");       float  width = glg.GetComponent<RectTransform>().sizeDelta.x;       float t=  width / maxBlood;//表示1血量多少像素        t = 1000 * t;//100血量多少像素        float lineWidth = 2f;     /////   Debug.Log("!!!!!!!!!!!!!!!!!!!!!血条像素????"+t);        Vector2 vec2=new Vector2(t- lineWidth , glg.spacing.y);        glg.spacing= vec2;//这就是像素间距        ///目前设置一格为100血量    }    /// <summary>    /// 初始化血量等信息    /// </summary>    /// <param name="max">最大血量</param>    /// <param name="cur">当前血量</param>    public void InitInfo(int max)//, int  cur    {        maxBlood = max;        //curBlood = cur;    }    /// <summary>    /// 传递血量修改显示    /// </summary>    /// <param name="b">当前血量百分比</param>    public void ChangeBlood(float b)    {        blood.fillAmount = b;    }            public void  CalOffset(float x,float y)    {        xOffset = x;        yOffset = y;    }    /// <summary>    /// 更新血条位置    /// </summary>    public void UpdatePosition()    {        Vector2 player2DPosition = cm.WorldToScreenPoint(father.transform.position);  //Camera.main.WorldToScreenPoint(father.transform.position);        recTransform.localScale = Vector3.one;//这里的缩放要强制恢复为1        float gao = cm.transform.position.y;        float fathergao = father.transform.position.y;        gao = Mathf.Abs(gao - fathergao);        float xie = Vector3.Magnitude(cm.transform.position - father.transform.position);        float di = xie * xie - gao * gao;        di = Mathf.Sqrt(di);        float y;   //     y= yOffset * di / xie;    //   Debug.Log("没有考虑距离的yyyyyyyyyyyyyy:"+y);        y = yOffset * di / xie * ( 7 /xie);   //   Debug.Log("考虑距离的yyyyyyyyyyyyyy:" + y);        float xxx= player2DPosition.x / Screen.width;///750        float yyy = player2DPosition.y/ Screen.height;        float zzz = Screen.height*1.0f/Screen.width;    recTransform.localPosition =   new Vector2(xOffset, y) - new Vector2(1334 / 2, 1334 * zzz / 2)+ new Vector2(1334 *xxx, 1334 *yyy * zzz);        /////Debug.Log("xxxyyy" + xxx + " " + yyy + "  " + zzz);        //recTransform.localPosition = player2DPosition + new Vector2(xOffset, yOffset) - new Vector2(Screen.width / 2, Screen.height / 2);// new Vector2(Screen.width/2, Screen.height/2)        ////Debug.Log("  recTransform.localPosition ???????????????????????????:" + recTransform.localPosition  +"   "+ new Vector2(Screen.width / 2, Screen.height / 2));        //position change to  localPosition        // Debug.Log("血条位置:" + player2DPosition.x + "  " + player2DPosition.y);        //血条超出屏幕就不显示        /*        if (player2DPosition.x > Screen.width || player2DPosition.x < 0 || player2DPosition.y > Screen.height || player2DPosition.y < 0)        {            recTransform.gameObject.SetActive(false);        }        else        {            recTransform.gameObject.SetActive(true);        }        */    }}



原创粉丝点击