mutoo
10/27/2015 - 7:15 AM

CircleText.cs

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

public class CircleText : BaseVertexEffect
{
	
	public int radius = 50;
	public float spaceCoff = 1f;

	#region implemented abstract members of BaseVertexEffect
	public override void ModifyVertices (List<UIVertex> verts)
	{
		if (!IsActive ())
			return;

		if (radius == 0) {
			Debug.LogWarning ("radius could not be zero!");
			return;
		}

		Text text = GetComponent<Text> ();
		TextGenerator tg = text.cachedTextGenerator;

		float perimeter = Mathf.PI * radius * 2;
		float weight = text.fontSize / perimeter * spaceCoff;
		float radStep = Mathf.PI * 2 * weight;
		float charOffset = tg.characterCountVisible / 2f - 0.5f;

		for (int i = 0; i < tg.characterCountVisible; i++) {
			var lb = verts [i * 4];
			var lt = verts [i * 4 + 1];
			var rt = verts [i * 4 + 2];
			var rb = verts [i * 4 + 3];

			Vector3 center = Vector3.Lerp (lb.position, rt.position, 0.5f);
			Matrix4x4 move = Matrix4x4.TRS (center * -1, Quaternion.identity, Vector3.one);

			float rad = Mathf.PI / 2 + (charOffset - i) * radStep;
			Vector3 pos = new Vector3 (Mathf.Cos (rad), Mathf.Sin (rad), 0) * radius;

			Quaternion rotation = Quaternion.Euler (0, 0, rad * 180 / Mathf.PI - 90);
			Matrix4x4 rotate = Matrix4x4.TRS (Vector3.zero, rotation, Vector3.one);
			Matrix4x4 place = Matrix4x4.TRS (pos, Quaternion.identity, Vector3.one);
			Matrix4x4 transform = place * rotate * move;

			lb.position = transform.MultiplyPoint (lb.position);
			lt.position = transform.MultiplyPoint (lt.position);
			rt.position = transform.MultiplyPoint (rt.position);
			rb.position = transform.MultiplyPoint (rb.position);

			verts [i * 4] = lb;
			verts [i * 4 + 1] = lt;
			verts [i * 4 + 2] = rt;
			verts [i * 4 + 3] = rb;
		}
	}
	#endregion

}