- Home /
Aspect Ratio changes GUI Location Code in CSHARP
I have asked this question before but did not make it clear, so I shall try again. I have created a texture, that is for the background of my screen. On top of this background, with coding, not adding game objects, I have added 4 different gui textures of human faces that is to be the players characters. When I put them where they belong by coding the texture rec, I have tried using screen width height, adding their location through a variable of float, and I have tried declaring it directly within the rect, however my GUI faces move around the screen when the aspect ratio is changed. I tried building it and seeing if they would move around per different size of the screen and they do. They must STAY in PLACE!!! They cannot wander off and get lost. How in the world do you make this happen. Pixel inset did not work, adding game objects (not that I wanted to, just expiremening) of gui texture and putting them in the right place did not work. I have searched high and low to find out what is going wrong though everytime I think I have found it, it says to use screen width/heigt or pixel inset.... I tried and did not work. How do you keep gui in place when the aspect ratio is changed!?!?!?!?!?!?!?!?!?!?!?!?!
thanks in advance!
Did you follow the advice on the relevant documentation page and set the scale of the GUITexture's transform to (0,0,0)? (http://unity3d.com/support/documentation/Components/class-GuiTexture.html)
(and note that GuiTextures are game objects, just that they have the GuiTexture component attached to them)
Sir, When I tried the information you had given to me (And I really do appreciate your time and patience) I did take the transform down to zero for x,y,z, then I would move the portrait into place using the bit you told me about. When my first portrait was up at the top left side, and I shrunk my aspect ratio down, the image 100% vanished, I think it was still up in the imaginary top left corner that got destroyed when the aspect of the screen shrunk, I'm not sure, Perhaps I am doing it wrong, however I researched it across unity3d awsers, and also google, and found much of the same thing, I think there is like one REALLY small thing I am missing to this.. it has been a long 3 days of searching, trying.. and failure :(
oh yeah, and I do understand that they are 'game objects' but I want all of this I am working on to stay under one script, not make four different objects of GUITextures in to view of Unity, I want to script them into existance, I have an unnatural hunger to learn as much coding as I can before i head off to school :)
Answer by Spittin_fire · Jul 08, 2011 at 01:08 AM
using UnityEngine;
using System.Collections;
// Use this on a guiText or guiTexture object to automatically have them // adjust their aspect ratio when the game starts.
public class GuiRatioFixer : MonoBehaviour { public float m_NativeRatio = 1.3333333333333F;
void Start ()
{
float currentRatio = (float)Screen.width / (float)Screen.height;
Vector3 scale = transform.localScale;
scale.x *= m_NativeRatio / currentRatio;
transform.localScale = scale;
}
}
Your answer
Follow this Question
Related Questions
Texture inside GUI 1 Answer
How to GUI Resize and Reposition accordingly to AR and Resolution? 0 Answers
Apply Render Texture To GUI + Transparency? 2 Answers
GuiTexture (Touch button) unfollow camera! 1 Answer
GUITexture Button? 1 Answer