Here is an example how I do scrolling text, the downside of this method is that it is frame rate dependent. OnGUI() is called at least twice per Update(). But I guess depending on the functionality or appearance you are going for that won’t be a big deal.
I personally use a matrix to format to any aspect ratio, but this may not be the best approach to scale textures correctly. There has been some helpful answers to my post that make it seem like it could be possible to do it with the GUI.matrix, but you are probably better off using individual GUI Textures on empty game objects with a scaling script from:
Components > Rendering > GUI Texture
Seems excessive to do this for every single GUI component if you have a ton like I do. Hard to say what’s the best way to do this many suggest not even using OnGUI() and getting some 3rd party products from the Asset store such as 2D Toolkit.
I know some of this information isn’t related to your specific question I just kind of want to give others some info and a early warning if they are getting started with OnGUI(). I put in a ton of work into mine, and I’m not sure if I agree with using the GUI.matrix as the ultimate solution, it allows easy size and position for any screen, but not really that great with scaling it seems.
There are a few people who posted some useful answers so it may be possible to do it with the GUI.matrix. I have yet to try this one for example that looks like it could possibly work if you determined to go GUI.matrix:
Also scaling solutions like AspectRatioEnforcer seem like they would be good if you use GUI.Textures on individual game objects with the AspectRatioEnforcer script.
K anyways now that I gave my little speech since this stuff can be quite messy and people should be very careful before they commit to something if there could be better solutions for them.
Here’s an example of how I have GUI text float up the screen in my game, would work for labels or textures as well:
//easily edit GUI components
public GUIStyle potionStyle;
int GUIDepthInt = -1;
Vector3 matrixVector;
float native_width = 1280;
float native_height = 720;
float rx;
float ry;
int textUpCounter;
void Start()
{
//scale for your matrix
//this is where things get messy at different aspect ratios for your scale
//if you always use a specific native width and height like 1280, 720
rx = Screen.width/native_width;
ry = Screen.height/native_height;
matrixVector = new Vector3 (rx, ry, 1);
potionStyle.normal.textColor = Color.red;
textUpCounter = 0;
}
void OnGUI()
{
if (GUIButtonsScript.gamePausedBool == true)
{
return;
}
else if (potionObtainedBool == true)
{
GUI.depth = GUIDepthInt;
GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, matrixVector);
++textUpCounter;
//this will center your text if you use the GUIStyle with "Upper Center" allignment
//with the 1280, 720 formatting matrix
GUI.Label (new Rect (640, 140 - textUpCounter, 0, 0), "+1 Health Potions!", potionStyle);
}
}
So in this example you switch the healthPotionBool to true when you pick it up, it shows the text scroll up in center of screen.
I personally start a coroutine that destroys the health potion at the time the text should be disappearing off the screen.
I also enable this script when the player becomes fairly close to the potion.
Keep in mind that OnGUI() is VERY performance heavy calling twice per update, so if you are going to be using it on individual objects you will want to disable those scripts when out of range, and disable them again when the text display is not being used or just destroy the Game Object entirely.