- Home /
How to create 360 curved text?
Hello all
I need to have a sentence surrounding the player, how should I approach this?
Let me tell you what I've tried already: Text mesh pro - no results
Curvy - no results (good for 2d circle only)
several curved ui scripts - no results (good for 2d only)
Curved UI - no results (good for 180 degrees only)
for each of these something didn't work well
After reading the relevant scripts its seems to be related to UIVertex, but it's too much for me to tweek this type of code. I need more c# knowledge
EDIT: Like in this picture (the use is in the middle of the circle)
If after so much different approaches you still have no results, there must be some specific requirements you did nor mention in your question. "Surrounding the player" can mean many things. Can you provide more details...
Answer by maxoja · Sep 10, 2018 at 07:02 PM
If you can code, this is one method that I have used. The way is, inheriting Text and override OnPopulateMesh method. This code works for my settings but not sure if it will suit yours.
public class CurvedText : Text {
public float diameter = 200;
protected override void OnPopulateMesh(VertexHelper vh)
{
base.OnPopulateMesh(vh);
for (int i = 0; i < vh.currentVertCount; i++)
{
UIVertex vert = UIVertex.simpleVert;
vh.PopulateUIVertex(ref vert, i);
Vector3 position = vert.position;
//manipulate position
float ratio = (float)position.x / preferredWidth;
float mappedRatio = ratio * 2 * Mathf.PI;
float cos = Mathf.Cos(mappedRatio);
float sin = Mathf.Sin(mappedRatio);
position.x = -cos * diameter;
position.z = sin * diameter;
vert.position = position;
vh.SetUIVertex(vert, i);
}
}
}
I wish it helps.
@maxoja Thank you
It works for me! now I need to apply an animation and it's good to go! Great script
Answer by dan_wipf · Sep 10, 2018 at 05:44 PM
Ok first of all you have to create a Prefab.
Create an EmptyGameObject. / Zero the Position and Rotation
Add as Child TextMeshPro - Text. / Rotate the Rect Transform around 180 degrees.
In the Font Settings set the Alignment to Center and Middle
Add the Prefab to the Propertyfield Text Mesh Prefab.
this is just a basic Code which you might want to extend. The bools are just for debugging purpose.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
[ExecuteInEditMode]
public class TextArroundPlayer : MonoBehaviour {
public string RoundText = "";
public float Radius = 10;
public GameObject TextMeshPrefab;
public bool join,destroy;
List<GameObject> prefabs = new List<GameObject>();
void Update(){
if(join){
Vector3 center = transform.position;
float ang = 0;
for (int i = 0; i < RoundText.Length; i++){
Vector3 pos = RandomCircle(center, Radius,ang);
Quaternion rot = Quaternion.FromToRotation(Vector3.forward, center-pos);
prefabs.Add(Instantiate(TextMeshPrefab, pos, rot));
char c = RoundText[i];
prefabs[i].GetComponentInChildren<TextMeshPro>().text = c.ToString();
ang += 360 / RoundText.Length-1;
}
prefabs[0].transform.rotation = Quaternion.Euler(0,prefabs[0].transform.rotation.y,prefabs[0].transform.rotation.z);
join = false;
}
if(destroy){
for(int i = 0;i<prefabs.Count;i++){
DestroyImmediate(prefabs[i]);
}
prefabs = new List<GameObject>();
destroy = false;
}
}
Vector3 RandomCircle ( Vector3 center ,float radius,float ang){
Vector3 pos;
pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
pos.y = center.y;
pos.z = center.z + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
return pos;
}
}
EDIT: Added 2 Screenshot of the TextMesh variation..
thanks @dan_wipf I did well up until 4, and then I lost you....
I have two text mesh pro prefabs called: Text$$anonymous$$eshPro-Prefab 1, Text$$anonymous$$eshPro-Prefab 2
None of them as seems doen's have property field.
What am I missing? WELL OF COURSE the script you've attached!
Trying this now! update: I created the empty gameobject with a child text - T$$anonymous$$P I created another gameobject and called it TextAroundPlayer, but when attaching the script got this pop-up:
When tried to attach the script to other objects, same error
ok in inspector right click and select empty gameobject. find it in the scene. Rename it as you’d like to and set the transforms position and rotation to 0
right click in inspector on the created game objecte, and select in the submenu of 3d GameObjects text mesh no it should be a child of the above created gameobject. in the rect transform of the new text mesh make x&y position as well zero
in the text mesh component choose the alignment of the text center and middle
now drag and drop the gameobject to your asset folder. now you have the prefab. you can now delet the created object in the inspector.
now attach the script to your Player. in the inspector there should be now a field which is called Text $$anonymous$$esh Prefab. Drag and drop the created prefab from your asset folder to it. now you can type something in to the string field and hit join
Ok, I had a misspelled the scripts' name. that's why the pop up window appeared. It didn't work for me though Is it working in your scene?
Thanks
I edited my Answer.. it works for me perfectly =)