Backup your own data.
ugui-based health bar 1 controls the position (considering pixel adaptation) 2 implements a stereoscopic perspective (calculates the offset based on the lens angle.)
using XXXXXXX; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HeadInfo { //object public GameObject go;//blood bar object // public KPlayer player;//The player to which the health bar belongs public GameObject father;//Who is the parent of the health bar //UI component private Transform canvas;//UI画布 public Image blood;//Modify blood value through blood public RectTransform recTransform;//Transform by calling the matrix //data public float xOffset=0;//Health bar position offset x public float yOffset = 400f;//300f;//Health bar position offset y public int maxBlood;//Maximum blood volume public int curBlood;//Current blood volume public Text CrystalNumber; public GridLayoutGroup glg;//Element layout public Camera cm;//Camera public Text Name;//blood bar display name public Text Level;//Level public Transform myHeadInfo;//My position //public Transform otherHeadInfo;//Other positions /// <summary> /// /// </summary> /// <param name="f"></param> /// <param name="P">0 means self 1 means enemy 2 means friend</param> public HeadInfo( GameObject f , RPlayer rPlayer, int P=0 )//bool forMe, KPlayer play { ////// Debug.Log("PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP::::::::::"+P); // cm = KCamera.I.CamFx;//Effect camera ////Debug.Log("Create health bar!!!!!!!!!!!!!"); 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);///Add object 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> /// Set the number of crystals on the character's health bar /// </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()//Abolish { //PlayFxGameUI.I.ShowBuQuP(go.transform); PlayFxGameUI.I.ShowBuQuP(father.transform); } public void HideBuQuP()//Abolish { 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> /// Set the dividing line of the blood bar /// </summary> /// <param name="maxBlood"></param> public void SetBloodLine( int maxBlood) { //// Debug.Log("Set blood volume SetBloodLine"); float width = glg.GetComponent<RectTransform>().sizeDelta.x; float t= width / maxBlood;//Indicates how many pixels 1 blood volume t = 1000 * t;//How many pixels are 100 HP float lineWidth = 2f; ///// Debug.Log("!!!!!!!!!!!!!!!!!!!!! Blood bar pixels????"+t); Vector2 vec2=new Vector2(t- lineWidth , glg.spacing.y); glg.spacing= vec2;//This is the pixel spacing ///Currently set a grid to 100 HP } /// <summary> /// Initialize information such as blood volume /// </summary> /// <param name="max">Max HP</param> /// <param name="cur">Current HP</param> public void InitInfo(int max)//, int cur { maxBlood = max; //curBlood = cur; } /// <summary> /// Pass the blood volume modification display /// </summary> /// <param name="b">Current HP percentage</param> public void ChangeBlood(float b) { blood.fillAmount = b; } public void CalOffset(float x,float y) { xOffset = x; yOffset = y; } /// <summary> /// Update the health bar position /// </summary> public void UpdatePosition() { Vector2 player2DPosition = cm.WorldToScreenPoint(father.transform.position); //Camera.main.WorldToScreenPoint(father.transform.position); recTransform.localScale = Vector3.one;//The scaling here should be forced back to 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("yyyyyyyyyyyyy without considering distance:"+y); y = yOffset * di / xie * ( 7 / xie ); // Debug.Log("Considering distance yyyyyyyyyyyyy:" + 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); //The blood bar will not be displayed if it exceeds the screen /* if (player2DPosition.x > Screen.width || player2DPosition.x < 0 || player2DPosition.y > Screen.height || player2DPosition.y < 0) { recTransform.gameObject.SetActive(false); } else { recTransform.gameObject.SetActive(true); } */ } }