Hi, I'm Adam. Email me about: CTOs, Mobile/iOS, Project Management, and Development

Template for #unity3d to test custom procedural game meshes

One of Unity’s dirty secrets is that (out of the box) it’s plain awful as a prototyping tool. You can fix that, but it requires quite a lot of work. I’m going to start publishing my fixes one by one. Here’s the first: a test-bed for making custom meshes.

A very common thing you need to during Unity gamedev – creating and testing algorithms for generating and manipulating meshes – is a pain. There’s custom classes and editors and stuff you have to write before you can create a single, basic, mesh.

One of Unity’s silly failings is that they don’t allow custom “new-project” templates, as if we were stuck in 1980 and hadn’t learned how to make IDE’s yet.

I’ve created a minimal example (should work with all versions of Unity 3.x through 5.x) which automatically creates a single mesh and scales it to fit your camera, and centers it on that camera’s screen, but positioned on Z=0, so that it doesn’t get clipped.

This solves 4 or 5 of the most common “dumb mistakes” I tend to make when trying to add new Mesh-generation code from scratch to an existing Unity project. Gives me a good starting point for new projects

Usage

The tri has center of base at the local origin of the camera, and the tip of the triangle precisely touching the top edge of the screen. This should make it very easy to visually check your meshes are generating at right position/orientation/location.

I’m assuming you’ll build your meshes in 2D; nearly all gamedev algorithms work in 2D first and are extrapolated to 3D, so that’s how I recommend writing and testing them. Once it works in 2D, move your code to a “real” game project and start doing the 3D version. This project is simply to get you up and running quickly when prototyping new Mesh-gen ideas.

I’m also assuming you’ll deal with material + texture + lighting yourself – many algorithms don’t even use textures, either because they’re using shaders (in which case you’d want to test UV too, but I’ve left that out here), or because they’re raw topology algorithms that don’t care about rendering.

So … you’ll get a pink triangle to start with. If you’re making custom mesh algorithms, you most certainly should already know how to create and attach materials in code!

Bonus: won’t delete data

There’s some code in there that each time you re-gen, it automatically gives a new name to next item, and doesn’t delete the old. You might want to remove that and have it automatically delete the old test data each time. But I erred on the side of caution.

Installation

  1. Create an empty Unity project. This will have a camera by default (required!)
  2. Select the camera and set it to “Orthogonal”
  3. Create a new folder “Editor” (required by Unity Corp to workaround absurd bugs in Unity’s build system)
  4. Create a file in that called “TestTriGen1.cs”
  5. Copy/Paste the source into that file
  6. Save the file, go back to Unity.
  7. Wait a few seconds
  8. A menu will appear “TriGen”, click the one item in that menu
  9. Click your camera to see the Unity preview: you should see a large pink triangle

Code

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

using UnityEditor;

public class TestTriGen1 : MonoBehaviour
{

private static TestTriGen1 _ttg;
[MenuItem("TriGen/TriGen")]
public static void GenTris()
{
if( _ttg == null)
_ttg = new TestTriGen1();

_ttg.gen();
}

public void gen()
{
Camera camera = Camera.main;
float MeshDiameter = camera.orthographicSize;

		string _parentName = "Gen'd tris";
GameObject parent = GameObject.Find( _parentName );
 if( parent == null ) parent = new GameObject( _parentName );

int numChildren = parent.transform.childCount;
GameObject area = new GameObject("Area "+(1+numChildren));
area.transform.parent = parent.transform;
		area.transform.position = new Vector3( camera.transform.position.x, camera.transform.position.y, 0 );

MeshRenderer mr = area.AddComponent<MeshRenderer>();
MeshFilter mf = area.AddComponent<MeshFilter>();
Mesh m = new Mesh();
mf.mesh = m;

Vector3[] vs = new Vector3[3];
vs[0] = new Vector3(0,1f * MeshDiameter,0);
		vs[1] = new Vector3(0.5f * MeshDiameter,0,0);
		vs[2] = new Vector3(-0.5f * MeshDiameter,0f,0);
		
		int[] tris = new int[3];
		tris[0] = 0;
		tris[1] = 1;
		tris[2] = 2;
		
		m.vertices = vs;
		m.triangles = tris;
}
}

6 thoughts on “Template for #unity3d to test custom procedural game meshes”

Comments are closed.