This is in relation to an earlier post (http://forum.unity3d.com/viewtopic.php?t=30090).
I got some great tips on how to do irregular shaped health bars using GUITexture. Before I decided to go that route, I came on an idea that would let me accomplish the task in UnityUI using GUI.DrawTexture calls. However, I’ve run into a problem that I’m hoping is obvious to someone.
Here’s my code (this is just a trial run to get the mask to be copied to a target texture and then displayed. Further work will have the target texture have different colors and such).
void SetHealthBarTex()
{
Texture2D playerHealthMaskTexture;
Color[] playerHealthColors;
Texture2D playerHealthTexture;
playerHealthMaskTexture = (Texture2D)Resources.Load("UI/Brawler/PlayerBarHealthMask");
playerHealthTexture = new Texture2D(playerHealthMaskTexture.width, playerHealthMaskTexture.height, TextureFormat.ARGB32, true);
playerHealthColors = playerHealthMaskTexture.GetPixels(0);
playerHealthTexture.SetPixels(playerHealthColors);
playerHealthTexture.Apply();
}
public override void OnGUI()
{
GUI.DrawTexture(new Rect(0, 0, 300, 120), playerHealthTexture);
}
What happens when I run this is that I get no image drawn. If I attempt to access properties of playerHealthTexture, I get this message:
MissingReferenceException: The object of type ‘Texture2D’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
This is mysterious to me. If I replace the playerHealthTexture with the playerHealthMaskTexture (only difference being the latter is loaded via Resources.Load, whereas the former is created dynamically in the initialization), the image will show up as I expect without the error message.
Further, I found that i could successfully get this to work by creating the texture inside the draw call:
void SetHealthBarTex()
{
Texture2D playerHealthMaskTexture;
Color[] playerHealthColors;
playerHealthMaskTexture = (Texture2D)Resources.Load("UI/Brawler/PlayerBarHealthMask");
playerHealthColors = playerHealthMaskTexture.GetPixels(0);
}
public override void OnGUI()
{
Texture2D playerHealthTexture;
playerHealthTexture = new Texture2D(playerHealthMaskTexture.width, playerHealthMaskTexture.height, TextureFormat.ARGB32, true);
playerHealthTexture.SetPixels(playerHealthColors);
playerHealthTexture.Apply();
GUI.DrawTexture(new Rect(0, 0, 300, 120), playerHealthTexture);
}
This code works beautifully exactly as I’d expect… for twenty seconds or so until Unity crashes with a message in the debug log that it’s out of memory. Clearly, creating a texture2d every frame is, as expected, a bad idea. Yet it does show that it can work.
So I’m confused as to what differences occur between “Texture2D t = new Texture2D(w,h)” and “Resources.Load(“ui/texture”)” that make one stick around and the other disappear.
If I can make this approach work of dynamically creating and modifying a texture2d based on a provided masked texture and some variables (how much power a player has), then I can accomplish what i need within UnityUI fairly efficiently (I’d only need to modify the dynamic texture when a health event occurs, so I wouldn’t be pixel tweaking every frame).
Thanks for any insight.