内容一:了解transform中Rotate和移动针对3D物体和2D物体的实现
3D:使用Rotate可是直接使用目标点【Rotate(vector3)】
2D:使用Rotate需要控制Recttransform中Rotate的角度【使用两个向量得计算夹角(vector.Angle),规定好UI的锚点(确定旋转分割线),】,每次修改角度都需要重置初始(否者会发生旋转)
内容二:了解向量Vector的操作函数
向量长度:Vector.magnitude
向量夹角:Vector.Angle
点乘叉乘:Vector.Dot【夹角和方向】 Vector.Cross
内容三:常用Mathf的操作函数
Mathf.Sin
Mathf.Cos
脚本实现:绘制圆环点
public Transform ConterPoint;
public int r = 5;
public int R = 5;
[Header("值越小越精确")]
public int multiple = 5;
private Vector3 VP1;
private Vector3 VP2;
private Vector3 VConter;
private List<GameObject> InstantiatePoint = new List<GameObject>();
private List<Transform> DrawPointDown = new List<Transform>();
private List<Transform> DrawPointUp = new List<Transform>();
public void OnDrawPath(Transform P1, Transform P2)
{
VP1 = P1.position;
VP2 = P2.position;
VConter = ConterPoint.position;
//记录P1/P2角度
Vector3 ConterP1 = VP1 - VConter;
Vector3 ConterP2 = VP2 - VConter;
float AngP1P2 = Vector3.Angle(ConterP1, ConterP2);
float AngRightConterP1 = Vector3.Angle(Vector3.right, ConterP1);
float AngRightConterP2 = Vector3.Angle(Vector3.right, ConterP2);
float Contrast = AngRightConterP1 > AngRightConterP2 ? AngRightConterP2 : AngRightConterP1;
for (int i = (int)Contrast; i < (AngP1P2 + Contrast); i += multiple, index++)
{
float x = VConter.x + r * Mathf.Cos(i * 3.14f / 180);
float y = VConter.y + r * Mathf.Sin(i * 3.14f / 180);
if (InstantiatePoint.Count > index)
{
InstantiatePoint[index].transform.position = new Vector3(x, y, VConter.z);
InstantiatePoint[index].SetActive(false);
DrawPointDown.Add(InstantiatePoint[index].transform);
}
else
{
GameObject obj = GameObject.Instantiate(DrowObj, new Vector3(x, y, VConter.z), Quaternion.identity);
obj.transform.SetParent(InstantiateParent);
obj.name = index.ToString();
obj.SetActive(false);
InstantiatePoint.Add(obj);
DrawPointDown.Add(obj.transform);
}
}
for (int i = (int)Contrast; i < (AngP1P2 + Contrast); i += multiple, index++)
{
float x = VConter.x + (r + R) * Mathf.Cos(i * 3.14f / 180);
float y = VConter.y + (r + R) * Mathf.Sin(i * 3.14f / 180);
if (InstantiatePoint.Count > index)
{
InstantiatePoint[index].transform.position = new Vector3(x, y, VConter.z);
InstantiatePoint[index].SetActive(false);
DrawPointUp.Add(InstantiatePoint[index].transform);
}
else
{
GameObject obj = GameObject.Instantiate(DrowObj, new Vector3(x, y, VConter.z), Quaternion.identity);
obj.transform.SetParent(InstantiateParent);
obj.name = index.ToString();
obj.SetActive(false);
InstantiatePoint.Add(obj);
DrawPointUp.Add(obj.transform);
}
}
Transform[] Meak = new Transform[DrawPointDown.Count + DrawPointUp.Count];
int j = 0;
bool ibol = true;
for (int i = 0; i < Meak.Length; i += 2, j += 1, ibol = !ibol)
{
if (ibol)
{
Meak[i + 0] = DrawPointDown[j];
Meak[i + 1] = DrawPointUp[j];
}
else
{
Meak[i + 0] = DrawPointUp[j];
Meak[i + 1] = DrawPointDown[j];
}
}
DrawPathScript.pos = Meak;
}
脚本实现:绘制圆环
[SerializeField]
public Transform[] pos;
// 自己手动刷新
void Update()
{
//SetNativeSize();
SetVerticesDirty();
}
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();//清理重置
vh.Dispose();//清理内容
if (pos == null) return;
if (pos.Length == 0) return;
Color32 color32 = color;
vh.Clear();
int j = 0;
try
{
for (int i = 0; i < pos.Length; i++)
{
if (pos[i] == null)
{
Debug.Log("存在空值" + i);
continue;
}
j = i;
vh.AddVert(pos[i].localPosition, color32, new Vector2(0f, 1f));
}
}
catch (System.Exception)
{
Debug.Log(j);
throw;
}
//四边形
UIVertex uI1 = new UnityEngine.UIVertex();
UIVertex uI2 = new UnityEngine.UIVertex();
UIVertex uI3 = new UnityEngine.UIVertex();
UIVertex uI4 = new UnityEngine.UIVertex();
//动态绘制
for (int i = 0; i < pos.Length; i += 2)
{
if (i + 3 >= pos.Length) continue;
vh.PopulateUIVertex(ref uI1, i);
vh.PopulateUIVertex(ref uI2, i + 1);
vh.PopulateUIVertex(ref uI3, i + 2);
vh.PopulateUIVertex(ref uI4, i + 3);
var UIVertex1 = new UIVertex[4] { uI1, uI2, uI3, uI4 };
vh.AddUIVertexQuad(UIVertex1);
}
}