- Home /
C# GUILayout.Box GUIContent
How do you setup a GUILayout.Box with GUIContent? I've tried moving GUIContent around within GUILayout.Box's parameters but I can't get it to work.
void OnGUI() {
//The Following below I have tried and do not work
//GUILayout.Box("SomeBox",new GUIContent("It is just a box"));
//GUILayout.Box("SomeBox"),new GUIContent("It is just a box");
}
}
Answer by numberkruncher · Oct 23, 2013 at 08:48 PM
Like @Loius pointed out, though I would recommend caching this somewhere to avoid allocation for every frame of your game! GUIContent
is a class not a struct!
Constant non-changing content:
private static GUIContent SomeBox = new GUIContent("SomeBox", "It is just a box");
void OnGUI() {
GUILayout.Box(SomeBox);
}
Variable content:
private GUIContent someBoxContent = new GUIContent("SomeBox", "It is just a box");
void OnGUI() {
GUILayout.Box(SomeBox);
if (GUILayout.Button("Change content!")) {
someBoxContent.text = "SomeSpecialBox";
someBoxContent.tooltip = "Okay, this is not just a box!";
}
}
I didn't even realise GUIContent was doing that. So in order to prevent this from happening I need to declare and use a GUIContent variable like in your two examples. Correct?
Yes, that's right, you can avoid allocating new GUIContent
instances by holding on to them ins$$anonymous$$d and then reusing them.
Answer by FPSworrior · Oct 23, 2013 at 08:52 PM
I'm not exactly sure how you want it to be but this is what I know.
//Place X and Y are put down so you can place the text wherever you want
//sizeX and sizeY are so you can make the text whatever size you want
//You can change the size and place in the editor instead of in the script
public float placeX;
public float placeY;
public float sizeX;
public float sizeY;
void OnGUI()
{
GUI.Box(new Rect(placeX, placeY, sizeX, sizeY), "Say something");
}
Hope this helps Vote yes if this is correct.
Your answer
Follow this Question
Related Questions
What is frame, How OnGuI is called every frame? 2 Answers
C# Applying Transparency to a Single GUI.Button 1 Answer
C# Boolean Doesn't Change Value 1 Answer
C# GUI Destroy or Disable? 1 Answer
FPS keep a loadout 0 Answers