Splash Screen Code

Hi.
I don’t know if this topic belong to this forum, I am sorry if it is not.

I am looking for a splash screen code that is compatibly with Unity 5 and have auto scale features (For the image so it works on any screen resolution)
I have watched a few tutorials on youtube but all of them are vey basic (don’t have auto scale) and seems that they are not compatible with Unity 5 (I had errors about missing classes)

So anyone kind enough to give me such code or a link to it?
Thanks in advance.

Well, if you want auto-scaling for a splash screen/logo, you are going to want to retrieve the size of the current screen, and perform a test based on that information.

float aspectRatio = (float)Screen.width / (float)Screen.height;
if(aspectRatio > 1.0f) {
//The aspect ratio is wider than it is tall, scale your logo relative to the height
}
else if(aspectRatio < 1.0f) {
//The aspect ratio is taller than it is wide, scale your logo relative to the width
}
else {
//The aspect ratio is perfectly square, this doesn't happen often, scale your logo relative to either
}

It helps in this scenario if your splash screen/logo is relatively close to being square itself. Also, you would want it to be of a high enough resolution to compensate for the scaling with a minimal amount of pixelization. Base its resolution on the highest resolution displays you are expecting to support.

Thanks a lot… Gonna try it now.

You would need a little more proportional code in order to figure out what scaling needs to be applied to your screen/logo element relative to the size of the screen you’re dealing with. But this basic aspect ratio test should be a good place to start, especially if mobile/tablets are going to be part of your target platforms. (never quite certain which way they are going to be oriented) I just finished up an aspect-ratio focused plugin, so these sorts of considerations were recently on my mind.

As far as positioning/centering your screen/logo, that’s a bit easier.

yourLogoElement.x = ((float)Screen.width - yourLogoElement.width) / 2.0f;
yourLogoElement.y = ((float)Screen.height - yourLogoElement.height) / 2.0f;

This snippet works for me every time. No matter how large or small your screen/logo is, this will drop it right into the middle of whatever the current screen is. It even works if the element is larger than the screen. (just returns a negative value for the coordinates)

The only real challenge is getting the bounds of your screen/logo in pixels. If you’re not using something like a Sprite, it might be a bit of a challenge. (but not impossible)

Umm can’t you just make the Canvas scale to the size of any screen? That’s what I’ve always done lol.

If the logo isn’t the aspect ratio of the screen, it will stretch, and badly, if you scale it to the size of the screen. This is especially true if it’s oriented differently to the current screen. (a wide, horizontal logo on a vertically oriented screen)

With the broad selection of different displays these days, it is worthwhile to have a solution that works for the vast majority of screens.

I don’t see a problem using just a canvas as it comes with a canvas scaler by default which has several modes. A non-full screen logo would work fine horizontally or vertically.

The Canvas element is only available in 4.6+, earlier versions of Unity don’t have access to it. The scale factor of the Canvas element is also only available when using it in Screen Space mode. If your splash screen/logo requires Camera Space, the automatic scaling the Canvas element provides won’t work.

This is also true for animated elements using standard scene Assets. (such as 3D models and the like) The new Canvas element is specific to the new in-game GUI. If you’re making your splash screen using anything other than the in-game GUI, you won’t be able to rely on Canvas element scaling.

The new GUI Canvas element is super sweet. And the RectTransform elements it implements are super-great for exactly this kind of application. (relative scaling like a boss) If you are using 2D GUI elements on versions 4.6+, then yes, the Canvas approach is the easiest and fastest way to go. Do make sure that your graphical element is of adequate resolution though, that still applies.

Yeah well he said Unity 5 and was only talking about splash screen with an image so I only based my short reply on those :slight_smile: