50 - Unit Display
Alright got some good work done today! Enough work that I’m going to write up a dev blog again. So what did I do? Well more or less I got unit display working that shows health and who is selected. It’s progress, took me about two and half hours. Alright let’s just hop right into code shall we? Most of the code sits in a new class I made call CrewDisplay. I’m not going to go into every detail of where and how things are called. public GameObject CrewUI; private Text Name; private Image Selected; private Canvas HealthArea; private Image HealthSquare; private Image HealthSquare5; private List Health; void Awake () { Name = (Text)transform.FindChild("CrewName").GetComponent(typeof(Text)); Selected = (Image)GetComponent(typeof(Image)); HealthArea = (Canvas)transform.FindChild("HealthArea").GetComponent(typeof(Canvas)); } So at first we are just setting up some variables. We need to grab the Text object for the Name, Image object for if it's selected or not, and the Canvas object for health. Also I needed to call this on Awake instead of Start because Awake happens when it's first created where Start can have funky things happen if you call a method on the same frame. public void UpdateUI(string name, int currentHP, int maxHP, int pos) { Name.text = name; var tempY = ((RectTransform)transform).rect.height; var tempX = ((RectTransform)transform).rect.width; var tempPos = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, 0)); tempPos.y = Camera.main.orthographicSize; tempPos.y -= tempY - 15; tempPos.y -= (pos * (tempY + 5)); tempPos.x += tempX / 2 + 10; tempPos.z = 0; transform.position = tempPos; SetUpHP(maxHP); } Alright this is just getting everything set up in place. More or less we are just pushing it into the top left corner and then adjusting it based on what position of units it is. Nothing too crazy here, just took a bit of fine tuning to get it in the right place. As a note, for height I used Screen.height first, don't use that. It gives you the height in pixels so some crazy shit goes on if you aren't doing everything by pixels. private void SetUpHP(int maxHP) { var canvasWidth = ((RectTransform)HealthArea.transform).rect.width; var canvasHeight = ((RectTransform)HealthArea.transform).rect.height; Health = new List(); var healthWidth = canvasWidth / maxHP; for (int i = 0; i < maxHP; i++) { var tempGO = new GameObject(); var healthImage = tempGO.gameObject.AddComponent(); healthImage.rectTransform.sizeDelta = new Vector2(healthWidth, canvasHeight); healthImage.sprite = Resources.Load(@"Sprites/testHealth"); healthImage.color = Color.red; healthImage.transform.SetParent(HealthArea.transform); var pos = tempGO.transform.localPosition; pos.y = 0; pos.x = i * (healthWidth) + (healthWidth / 2); tempGO.transform.localPosition = pos; var temp = ((RectTransform)tempGO.transform); temp.anchorMin = new Vector2(0, .5f); temp.anchorMax = new Vector2(0, .5f); Health.Add(healthImage); } } Alright here is something a little more in depth. Basically this is just setting up the healthbar. I had an idea in my head of having each unit of health segmented but it didn't look to good cause it was so small. If you recall the picture before I ended up making it just a straight bar. One thing I could change is instead of doing this, make just two images and over lap them with one on top. Then whenever health changes, change the top image to be a percent of the width of the bottom image. But I'll keep things how they are incase I go back to wanting to make it segmented again. public void SetSelected(bool selected) { if (selected) Selected.sprite = Resources.Load(@"Sprites/Selected"); else Selected.sprite = Resources.Load(@"Sprites/UnSelected"); } I think this speaks for itself. So if you haven't noticed, I'm being a little less verbose than normal. Let me know if you want me to just keep summarizing things like this, or go into every line of code. Either way, see you guys tomorrow!
1/24/2017 6:23:29 PM





Add Comment Auther

Comment