Skip to content

Instantly share code, notes, and snippets.

@mutoo
Last active May 31, 2023 03:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mutoo/b1a1db7037ec32cb97fd to your computer and use it in GitHub Desktop.
Save mutoo/b1a1db7037ec32cb97fd to your computer and use it in GitHub Desktop.
the code is based on Unity3d 4.6.x, the api may changed.
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
}
@clifflinmao
Copy link

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <437ba245d8404784b9fbab9b439ac908>:0) System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <437ba245d8404784b9fbab9b439ac908>:0) System.Collections.Generic.List1[T].get_Item (System.Int32 index) (at <437ba245d8404784b9fbab9b439ac908>:0)
CircleText.ModifyMesh (UnityEngine.UI.VertexHelper vh) (at Assets/Scripts/CircleText.cs:51)
UnityEngine.UI.Graphic.DoMeshGeneration () (at /Applications/Unity/Hub/Editor/2019.4.0f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Graphic.cs:629)
UnityEngine.UI.Graphic.UpdateGeometry () (at /Applications/Unity/Hub/Editor/2019.4.0f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Graphic.cs:614)
UnityEngine.UI.Text.UpdateGeometry () (at /Applications/Unity/Hub/Editor/2019.4.0f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Text.cs:569)
UnityEngine.UI.Graphic.Rebuild (UnityEngine.UI.CanvasUpdate update) (at /Applications/Unity/Hub/Editor/2019.4.0f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Graphic.cs:572)
UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at /Applications/Unity/Hub/Editor/2019.4.0f1/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/CanvasUpdateRegistry.cs:210)
UnityEngine.Canvas:SendWillRenderCanvases() (at /Users/builduser/buildslave/unity/build/Modules/UI/ScriptBindings/UICanvas.bindings.cs:72)`

@clifflinmao
Copy link

一添加就报错

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment