0
\$\begingroup\$

I am making an FPS game in which when you are near an item a UI Text appears at the bottom center of the screen indicating the name of the item and the button to pick it up (let's say [E]).

It works fine but since I couldn't get a clear contrast between the text and the game area (like if the player's standing in front of an egg and the color of the text is white, you can't see the text), I decided to add a black background image to the text.

Now since the item names are of different lengths, I need the image length to match the text length. The background image should just be enough to fit the text How can I go about doing this? Or is there a better way to show a message to the player?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You first get the text component, then get the image component, and then apply the length of text to the image. Inside a function or Start or Update, whichever you prefer (I'm assuming you have these objects set and ready to be processed):

{
    RectTransform uiText = textObject.GetComponent<RectTransform>();
    RectTransform uiImage = imageObject.GetComponent<RectTransform>();
    uiImage.sizeDelta = uiText.sizeDelta;
}

Not really sure about the design of your project but that should give you the general idea.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .