Is this the correct way to render 2D textures programatically?

I’ve been bashing my head trying to render tiles in 2D for my game. I’ve had issues with the sizes and coordindates not coming out as expected, but I’ve finally got some working code.

However; it seems like a lot of noise (three GL calls, creating Vector3s, converting them to move Vector3s, then finally drawing). Is this the correct/expected way to draw a 2D image at a given location?

(I can easily abstract this away; I just want to know if it’s the best way, or if I’m missing some 2D flag that might make some of this redundant… Googling for terms like ‘2D LoadPixelMatrix DrawTexture WorldToScreenPoint’ doesn’t return any results!)

using UnityEngine;

public class RenderGrass : MonoBehaviour
	public Texture texture;

	const int tileSizePixels = 70;
	const int pixelsPerUnit = 100;
	const float tileSizeUnits = (float)tileSizePixels / pixelsPerUnit;

	public void OnPostRender()
		// Not really sure why I need this?

		// Render 5 adjacent tiles from 0 going horizontally 
		for (int tileX = 0; tileX < 5; tileX++)
			// Create world coordindates from tile locations
			var topLeftWorld = new Vector3(tileX * tileSizeUnits, 0, 0);
			var bottomRightWorld = topLeftWorld + new Vector3(tileSizeUnits, -tileSizeUnits, 0);

			// Convert to screen coordinates
			// Not really sure why I need this; surely everyone is working in world coordindates?
			var topLeftScreen = camera.WorldToScreenPoint(topLeftWorld);
			var bottomRightScreen = camera.WorldToScreenPoint(bottomRightWorld);

			// Render to screen
			Graphics.DrawTexture(new Rect(topLeftScreen.x, topLeftScreen.y, bottomRightScreen.x - topLeftScreen.x, bottomRightScreen.y - topLeftScreen.y), texture);

		// Not really sure why I need this?

2 things that come to mind quickly.

1: Is this script attached to a camera? OnPostRender() only gets called if the script is attached to a camera.
2: Try Graphics.DrawTexture(new Rect(0,0,100,100),texture) and see if you get a 100 pixel square to show up on screen. if that works then your co-ordinates are off.

Hope this helps.