C# Start例程功能不适用于android,但适用于Unity和Mac
我正在尝试使用下面的代码在Unity场景中的C# Start例程功能不适用于android,但适用于Unity和Mac,c#,android,unity3d,C#,Android,Unity3d,我正在尝试使用下面的代码在Unity场景中的InputField中弯曲文本 using UnityEngine; using System.Collections; using TMPro; public class TextWarper : MonoBehaviour { private TMP_Text m_TextComponent; public AnimationCurve VertexCurve = new AnimationCurve(new Keyframe
InputField
中弯曲文本
using UnityEngine;
using System.Collections;
using TMPro;
public class TextWarper : MonoBehaviour
{
private TMP_Text m_TextComponent;
public AnimationCurve VertexCurve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(0.25f, 2.0f), new Keyframe(0.5f, 0), new Keyframe(0.75f, 2.0f), new Keyframe(1, 0f));
public float AngleMultiplier = 1.0f;
public float SpeedMultiplier = 1.0f;
public float CurveScale = 1.0f;
void Awake()
{
m_TextComponent = gameObject.GetComponent<TMP_Text>();
}
void Start()
{
StartCoroutine(WarpText());
}
private AnimationCurve CopyAnimationCurve(AnimationCurve curve)
{
AnimationCurve newCurve = new AnimationCurve();
newCurve.keys = curve.keys;
return newCurve;
}
IEnumerator WarpText()
{
VertexCurve.preWrapMode = WrapMode.Clamp;
VertexCurve.postWrapMode = WrapMode.Clamp;
//Mesh mesh = m_TextComponent.textInfo.meshInfo[0].mesh;
Vector3[] vertices;
Matrix4x4 matrix;
m_TextComponent.havePropertiesChanged = true; // Need to force the TextMeshPro Object to be updated.
CurveScale *= 10;
float old_CurveScale = CurveScale;
AnimationCurve old_curve = CopyAnimationCurve(VertexCurve);
while (true)
{
if (!m_TextComponent.havePropertiesChanged && old_CurveScale == CurveScale && old_curve.keys[1].value == VertexCurve.keys[1].value)
{
yield return null;
continue;
}
old_CurveScale = CurveScale;
old_curve = CopyAnimationCurve(VertexCurve);
m_TextComponent.ForceMeshUpdate(); // Generate the mesh and populate the textInfo with data we can use and manipulate.
TMP_TextInfo textInfo = m_TextComponent.textInfo;
int characterCount = textInfo.characterCount;
if (characterCount == 0) continue;
//vertices = textInfo.meshInfo[0].vertices;
//int lastVertexIndex = textInfo.characterInfo[characterCount - 1].vertexIndex;
float boundsMinX = m_TextComponent.bounds.min.x; //textInfo.meshInfo[0].mesh.bounds.min.x;
float boundsMaxX = m_TextComponent.bounds.max.x; //textInfo.meshInfo[0].mesh.bounds.max.x;
for (int i = 0; i < characterCount; i++)
{
if (!textInfo.characterInfo[i].isVisible)
continue;
int vertexIndex = textInfo.characterInfo[i].vertexIndex;
// Get the index of the mesh used by this character.
int materialIndex = textInfo.characterInfo[i].materialReferenceIndex;
vertices = textInfo.meshInfo[materialIndex].vertices;
// Compute the baseline mid point for each character
Vector3 offsetToMidBaseline = new Vector2((vertices[vertexIndex + 0].x + vertices[vertexIndex + 2].x) / 2, textInfo.characterInfo[i].baseLine);
//float offsetY = VertexCurve.Evaluate((float)i / characterCount + loopCount / 50f); // Random.Range(-0.25f, 0.25f);
// Apply offset to adjust our pivot point.
vertices[vertexIndex + 0] += -offsetToMidBaseline;
vertices[vertexIndex + 1] += -offsetToMidBaseline;
vertices[vertexIndex + 2] += -offsetToMidBaseline;
vertices[vertexIndex + 3] += -offsetToMidBaseline;
// Compute the angle of rotation for each character based on the animation curve
float x0 = (offsetToMidBaseline.x - boundsMinX) / (boundsMaxX - boundsMinX); // Character's position relative to the bounds of the mesh.
float x1 = x0 + 0.0001f;
float y0 = VertexCurve.Evaluate(x0) * CurveScale;
float y1 = VertexCurve.Evaluate(x1) * CurveScale;
Vector3 horizontal = new Vector3(1, 0, 0);
//Vector3 normal = new Vector3(-(y1 - y0), (x1 * (boundsMaxX - boundsMinX) + boundsMinX) - offsetToMidBaseline.x, 0);
Vector3 tangent = new Vector3(x1 * (boundsMaxX - boundsMinX) + boundsMinX, y1) - new Vector3(offsetToMidBaseline.x, y0);
float dot = Mathf.Acos(Vector3.Dot(horizontal, tangent.normalized)) * 57.2957795f;
Vector3 cross = Vector3.Cross(horizontal, tangent);
float angle = cross.z > 0 ? dot : 360 - dot;
matrix = Matrix4x4.TRS(new Vector3(0, y0, 0), Quaternion.Euler(0, 0, angle), Vector3.one);
vertices[vertexIndex + 0] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 0]);
vertices[vertexIndex + 1] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 1]);
vertices[vertexIndex + 2] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 2]);
vertices[vertexIndex + 3] = matrix.MultiplyPoint3x4(vertices[vertexIndex + 3]);
vertices[vertexIndex + 0] += offsetToMidBaseline;
vertices[vertexIndex + 1] += offsetToMidBaseline;
vertices[vertexIndex + 2] += offsetToMidBaseline;
vertices[vertexIndex + 3] += offsetToMidBaseline;
}
// Upload the mesh with the revised information
m_TextComponent.UpdateVertexData();
yield return new WaitForSeconds(0.025f);
}
}
}
使用UnityEngine;
使用系统集合;
使用TMPro;
公共类TextWarper:MonoBehavior
{
专用TMP_文本m_文本组件;
公共动画曲线顶点曲线=新动画曲线(新关键帧(0,0)、新关键帧(0.25f,2.0f)、新关键帧(0.5f,0)、新关键帧(0.75f,2.0f)、新关键帧(1,0f));
公共浮动角度乘数=1.0f;
公共浮动速度乘数=1.0f;
公共浮动曲线比例=1.0f;
无效唤醒()
{
m_TextComponent=gameObject.GetComponent();
}
void Start()
{
start例程(WarpText());
}
私有动画曲线复制动画曲线(动画曲线曲线)
{
AnimationCurve newCurve=新AnimationCurve();
newCurve.keys=curve.keys;
返回新曲线;
}
IEnumerator WarpText()
{
VertexCurve.preWrapMode=WrapMode.Clamp;
VertexCurve.postWrapMode=WrapMode.Clamp;
//Mesh Mesh=m_TextComponent.textInfo.meshInfo[0].Mesh;
向量3[]个顶点;
矩阵x4x4矩阵;
m_TextComponent.havePropertiesChanged=true;//需要强制更新TextMeshPro对象。
曲线比例*=10;
float old_CurveScale=曲线比例;
AnimationCurve old_curve=复制AnimationCurve(顶点曲线);
while(true)
{
如果(!m_TextComponent.havePropertiesChanged&&old_CurveScale==CurveScale&&old_curve.keys[1]。值==VertexCurve.keys[1]。值)
{
收益返回空;
继续;
}
old_CurveScale=曲线比例;
旧曲线=复制动画曲线(顶点曲线);
m_TextComponent.ForceMeshUpdate();//生成网格,并用我们可以使用和操作的数据填充textInfo。
TMP_TextInfo TextInfo=m_TextComponent.TextInfo;
int characterCount=textInfo.characterCount;
如果(characterCount==0)继续;
//顶点=textInfo.meshInfo[0]。顶点;
//int lastVertexIndex=textInfo.characterInfo[characterCount-1].vertexIndex;
float boundsMinX=m_TextComponent.bounds.min.x;//textInfo.meshInfo[0].mesh.bounds.min.x;
float boundsmax=m_TextComponent.bounds.max.x;//textInfo.meshInfo[0].mesh.bounds.max.x;
for(int i=0;i0?点:360-点;
矩阵=Matrix4x4.TRS(新向量3(0,y0,0),四元数.Euler(0,0,角度),向量3.1);
顶点[vertexIndex+0]=矩阵。多点3x4(顶点[vertexIndex+0]);
顶点[vertexIndex+1]=矩阵。多点3x4(顶点[vertexIndex+1]);
顶点[vertexIndex+2]=矩阵。多点3x4(顶点[vertexIndex+2]);
顶点[vertexIndex+3]=矩阵。多点3x4(顶点[vertexIndex+3]);
顶点[vertexIndex+0]+=offsetToMidBaseline;
顶点[vertexIndex+1]+=offsetToMidBaseline;
顶点[vertexIndex+2]+=offsetToMidBaseline;
顶点[vertexIndex+3]+=offsetToMidBaseline;
}
//上传带有修改信息的网格
m_TextComponent.UpdateVertexData();
收益率返回新WaitForSeconds(0.025f);
}
}
}
在这些情况下,代码运行良好