Skip to content

Instantly share code, notes, and snippets.

@asus4
Created March 28, 2012 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asus4/2223039 to your computer and use it in GitHub Desktop.
Save asus4/2223039 to your computer and use it in GitHub Desktop.
SystemFontRenderer utility for nGUI
using UnityEngine;
using System.Collections;
//[ExecuteInEditMode]
[RequireComponent(typeof(SystemFontRenderer))]
[AddComponentMenu("NGUI/Interaction/Ex/System Font")]
public class UISystemFont : MonoBehaviour {
SystemFontRenderer fontRenderer;
#if UNITY_EDITOR
void Awake () {
fontRenderer = GetComponent<SystemFontRenderer>();
makeSprite();
}
#endif
void Start () {
fontRenderer = GetComponent<SystemFontRenderer>();
fontRenderer.BindMaterial(renderer.material);
}
void makeSprite() {
float w = fontRenderer.textureWidth;
float h = fontRenderer.textureHeight;
Vector3[] vertices = new Vector3[] {new Vector3(0,0,0), new Vector3(0,h,0), new Vector3(w,h,0), new Vector3(w,0,0)};
Vector2[] uv = new Vector2[] {new Vector2(0,0), new Vector2(0,1), new Vector2(1,1), new Vector2(1,0)};
int[] triangles = new int[] {0, 1, 2, 0, 2, 3};
/* make sprite like..
0 - 3
| \ |
1 - 2
*/
Mesh mesh = GetComponent<MeshFilter>().sharedMesh;
mesh.Clear();
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment