Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GUITexture is deprecated, so what should I use instead of it?

I'm currently experiencing an issue with updated coding in C#. I'm working out of a textbook "3.x Game Development Essentials", and am currently attempting to make an array that will have textures assigned to it which will show the progression of a battery being charged to power a door. The textbook wants me to create a GUITexture, however this was made obsolete in a prior Unity update so I instead created a UI Image which created a Canvas and a child Game Object. While this fixed the problem on screen, it took a turn for the worse with coding. The book wants me to create an array of five textures (four states of charge, plus the original empty texture). The goal being whenever the character picks up a power cell, it reflects live in the UI Canvas. Now, here's where I get goobery:

This is the original coding the book specifies to implement on my Inventory script:

public Texture2D [] hudCharge;
public GUITexture charge HudGUI;

I ended up having to get squirrelly to work around old code, and tried this:

using UnityEngine.UI;

public class Inventory : MonoBehaviour{
public static int charge = 0
public UnityEngine.AudioClip collectSound;
public UnityEngine.Texture2D[] hudCharge;
public Image chargeHudGUI;

Now we get even weirder. Because the book is working with out of date code, it wants me to use the value of charge to choose a texture from the array. Thus, it wants me to type:

chargeHudGUI.texture = hudCharge[charge];

the goal being We're addressing the GUITexture object that is assigned to chargeHudGUI variable, specifically its texture property. Well, that'd be groovy but there's no GUITexture anymore so where does that leave me? It leaves me with this:

chargeHudGUI.Image = hudCharge[charge].

I have been looking through every thread I can to try and figure out what to do, but nothing seems to address this particular problem. Does anyone have any pointers for navigating around this nonsense? I've been working on this game for a while now, and I'm determined to finish it out. I want to learn how to use Visual Studio and Unity3D. I appreciate any help!

like image 903
L. Dostal Avatar asked Nov 27 '18 01:11

L. Dostal


1 Answers

GUITexture is indeed deprecated just like GUIText . Since your hudCharge variable is a type of Texture2D, make chargeHudGUI to be type of RawImage instead of Image so that you can assign it directly with the texture property.

Go to GameObject ---> UI ---> RawImage and Unity will create a Canvas with a GameObject as a child. That child GameObject will have a RawImage component. You can learn more about the new UI system here.

So, replace

public Image chargeHudGUI;

with

public RawImage chargeHudGUI;

Now, you can do this:

chargeHudGUI.texture = hudCharge[charge];

You can still use Image instead of RawImage but you have to convert the Texture2D to Sprite each time or cache them then change the Image.sprite property to display them.

public Image chargeHudGUI;

then you can do this:

Texture2D tex = hudCharge[charge];
//Create Sprite from the Texture2D
Sprite tempSprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
//Change the Sprite
chargeHudGUI.sprite = tempSprite;
like image 60
Programmer Avatar answered Nov 06 '22 15:11

Programmer