Skip to content

Instantly share code, notes, and snippets.

@zerosalife
Created September 6, 2014 16:24
Show Gist options
  • Save zerosalife/743cf797112d62978bec to your computer and use it in GitHub Desktop.
Save zerosalife/743cf797112d62978bec to your computer and use it in GitHub Desktop.
#pragma strict
var size: float;
function Awake() {
var m: Mesh = CreateBoxMesh(size);
var obj: GameObject = CreateGameObject();
// Attach Mesh `m' to the GameObject `obj'.
obj.GetComponent(MeshFilter).mesh = m;
var texture: Texture2D = CreateTexture2D(size);
// Attach Texture2D `texture' to the GameObject `obj'.
obj.renderer.material.mainTexture = texture;
obj.renderer.material.color = RandomRGBColor();
// Set Shader to use the Default Sprite shader.
var spriteShader: Shader = Shader.Find("Sprites/Default");
obj.renderer.material.shader = spriteShader;
}
function CreateBoxMesh(size: float): Mesh {
// Creates a square planar Mesh `m' of dimensions `size'. The
// mesh is composed of two triangles.
var m: Mesh = new Mesh();
m.name = "Scripted_Plane_New_Mesh";
m.vertices = [Vector3(-size, -size, 0.01),
Vector3(size, -size, 0.01),
Vector3(size, size, 0.01),
Vector3(-size, size, 0.01)];
m.uv = [Vector2(0, 0),
Vector2(0, 1),
Vector2(1, 1),
Vector2(1, 0)];
m.triangles = [0, 1, 2,
0, 2, 3];
return m;
}
function CreateGameObject(): GameObject {
// Create a GameObject `obj'.
var obj: GameObject = new GameObject("New_Plane_From_Script",
MeshRenderer,
MeshFilter,
MeshCollider);
return obj;
}
function CreateTexture2D(size: float): Texture2D {
// Create a Texture2D `texture'.
var texture: Texture2D;
texture = new Texture2D(size, size, TextureFormat.ARGB4444, false);
return texture;
}
function RandomRGBColor(): Color {
// Create a Random RGB Color `color'.
var red: float = Random.Range(63, 255) / 255f;
var green: float = Random.Range(63, 255) / 255f;
var blue: float = Random.Range(63, 255) / 255f;
var color = Color(red, green, blue);
return color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment