- Home /
[Closed]Custom Image On Gui.Box
Hello, i want to ask about how to insert a custom image i create to the GUI.Box?
as far as i do, the code is like this
function OnGUI(){
var judul : String;
var asd : Texture;
GUI.Box(Rect((Screen.width-(Screen.width/4*3))/2,
(Screen.height-(Screen.height/4*3))/2,
Screen.width/4*3, Screen.height/4*3),asd);
}
i create a fair good amount of Gui Rect Box.. but i still dont understand how to change the Black background of the GUI.Box into the Images i want, i found out on Unity Documentation, it comes out like this..
static function Box(position: Rect, text: string): void;
static function Box(position: Rect, image: Texture): void;
static function Box(position: Rect, content: GUIContent): void;
static function Box(position: Rect, text: string, style: GUIStyle): void;
static function Box(position: Rect, image: Texture, style: GUIStyle): void;
static function Box(position: Rect, content: GUIContent, style: GUIStyle): void;
but there is no example to how inserting new image.. could anybody teach me how to do it?
EDIT : I've been searching about this, but some use GUI Skin, and i don't quite understand about that, do i need to use that? or i could do this by code instead?
Answer by JVaughan · Jan 08, 2014 at 03:59 PM
Where you see this:
Static function Box(position: Rect, image: Texture): void.
What this is saying that you can specify a screen space size and position with a Rect object and Texture image to fill the box instead of the default black transparent image.
So first you need to specify a Texture in the script that you can associate an image with. In C# that would look like this.
public Texture2D myGUITexture;
Assign your GUI texture in the Inspector. Then in the script where you call GUI.Box reference your texture. Assuming you have a 192X64 texture stuck in the upper right corner at 5 percent of the screen the code would look like this in C#:
void OnGUI (){
GUI.Box(new Rect( Screen.Width*0.05f, Screen.Height*0.05f, 192, 64), myGUITexture);
}
Or in short form - move the line
var asd : Texture;
outside of OnGUI function, and then assign the texture in the inspector.
aw, thanks, my code doesn't show my Texture on Inspector, because i put the var asd : Texture;
inside the function, now it's already show up..
but i have some problem, which i could not arrange the size of the picture shown on GUI.Box? is there any way, i want to just call that image and the size is like the calculation above?
@ArkaneX just see your post. thanks, it's right, it was my fault.. but for the next problem could u help to solve it?
GUI.Box is always scale to fit, so if you want it to be a specific size specify it in pixels. If you want to draw a a GUI item with more options you need to use GUI.drawTexture ins$$anonymous$$d. Here is an example:
GUI.DrawTexture(new Rect(screenPos.x,invertedCursorHeight,32,32),cursorTexture,Scale$$anonymous$$ode.ScaleToFit,true);
And if you could mark my first answer as correct it would be very appreciated!
yeah, i use GUI.DrawTexture, but why the images so blurry? is there anyway to make it better but not by changing the whole image into new larger image?
because, if i use Plane ins$$anonymous$$d, the images is so clear..
this is when i use Plane and, this is when i use GUI.DrawTexture