glOrtho-alike in Unity Indie?

Hiya folks!

I’ve been playing a lot with 2D stuff in Unity and I’m still not a happy place. There are solutions but I’m not sure I like any of them.

I’ve tried a number of different tricks, but after playing around with OpenGL programming on the side I now what I really want is to do a call like this:

glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f);

Essentially I’m trying to figure out how to texture atlas game map tiles. The reason is that I’m attempting to make a 2D game that will be for both Mac and iPhone, and I want to use as few separate textures as possible so that the iPhone can have a higher framerate.

The other option is to try to fudge the sizes with the regular Unity camera ortho settings, but I can’t for the life of me get things sized up exactly.

Any hints? Thanks!

The Camera class has a projectionMatrix property which you can use to set a custom matrix. Furthermore, the Matrix4x4 class has a method called Ortho which generates an orthographic matrix (I have only given this a quick test, but I’m fairly sure it gives the same result as glOrtho, according to the definition of the matrix in the Red Book). WIth these functions, the code is quite simple, something like:-

Matrix4x4 m = Matrix4x4.Ortho(l, r, b, t, n, f);
Camera.main.projectionMatrix = m;

Hi, and thanks for replying!

I’ve tried that function a few times this evening, but it doesn’t seem to actually do anything. Is there a specific time when it has to be used, IE, you have to set the projection matrix in LateUpdate, or it only works in Start, or …? Sorry for being so dense, but no matter what I change it to my test scene looks the same.

I’m sure I’m stupid, but why don’t you just click the camera, enable orthographic projection and configure the corresponding props? Is there anything specific you don’t like about that?

I was going to say the same as dreamora.
I’d also like to add, that I miss orthographic-style games, so I hope you make it :stuck_out_tongue:

Once you’ve set the matrix, it should remain the same until you change it again or reset it with ResetProjectionMatrix. If you want to keep it the same throughout the game, put the call in the Start function, if you want to change it each frame, put it in Update, etc…

Setting the matrix should definitely have an effect (although not necessarily the one you intended :wink: ) Are you sure the camera you are changing is the one that is actually rendering to screen? Are you using the Game view to check the result (Scene view isn’t affected by changes to the in-game camera matrix)?