- Home /
Repositioning remote GUI Texture
So that it will be viewed in any resolution, I want to move my Speedometer GUI Texture to the bottom left of the screen. My idea was to use pixleInset. Well, this worked:
var divideX : float = -2.03;
var divideY : float = -2.04;
function Update() {
guiTexture.pixelInset = Rect (Screen.width/divideX, Screen.height/divideY, 256, 256);
}
That script can be attached to any GUI Texture and edited in the inspector to compensate for multiple HUD elements. So that's probably the best way to do it, but for my first attempt, I was trying to reposition it remotely from the control script which housed the speed variable and switched the textures to compensate for how fast the player was going, like a speedometer should. The GUI texture game object was called GaugeGUI, and I defined a variable to represent it; var GaugeGUI : GameObject; which I assigned with .Find in the start function. then I tried to do this:
function Update() {
GaugeGUI.pixelInset = Rect (Screen.width/divideX, Screen.height/divideY, 256, 256);
}
It returned an error saying that .pixelInset is not a member of GameObject. Which makes sense, but is there a way to remotely access that GUI texture? I ask after already finding a better method simply for the sake of being a well rounded programmer.
Answer by SomethingStrangeStudios · Jul 13, 2014 at 04:24 AM
You can pass a variable into a function from the camera using "WorldtoViewPortPoint", that way you can position the gui texture in relation to the camera not the world.
http://docs.unity3d.com/ScriptReference/Camera.WorldToViewportPoint.html
var viewPos : Vector3 = camera.WorldToViewportPoint (MYtarget.position);
Thats passing your view from a camera to an object/texture and you can move the texture from the textures script.
this is using
var viewPosX:float=camera.WorldToViewportPoint(Mytarget.position.x);
MyGUITexture.SendMessage(myFunction,viewPosX);
on your camera, to your "myFunction" function in the Guitextures script.
as well as that you can handle the gui inside the camera using a translate method and calling your GUITexture in the cameras script.
MyGUITexture.transform.translate=Vector2(viewPosX,0)
PS. Also i've looked up pixel inset and it should be used with a guitexture, (i've never used this before) but assuming that and that you were using a guitexture why did it refer to it as a gameobject?
Your answer
Follow this Question
Related Questions
How can i show the texture in the array? 2 Answers
DrawTexture GUI iPhone 1 Answer
How do I change the texture of a Raw image? 0 Answers
Setting Scroll View Width GUILayout 1 Answer
pixelInset to Move a GUITexture C# 0 Answers