问题分析:
最近在搞软件底层开发,将一些工具或者底层脚本打成dll导入unity使用,有这样一需求,就是编辑功能,需要像Scene场景一样,实现那种编辑轴
实现方式:
创建Mesh,构建编辑轴,这个地方这么几步:
1.线(轴)
2.圆(旋转线)
3.正方形(轴面)
4.圆锥(轴方向)
具体步骤:
1.创建线Mesh:
代码:
/// <summary>
/// 创建线Mesh
/// </summary>
/// <param name="start">线起点</param>
/// <param name="end">线终点</param>
/// <returns>Mesh对象</returns>
private Mesh CreateLineMesh(Vector3 start, Vector3 end)
{
var vertices = new List<Vector3> { start, end };
var indices = new List<int> { 0, 1 };
Mesh mesh = new Mesh();
mesh.SetVertices(vertices);
mesh.SetIndices(indices.ToArray(), MeshTopology.Lines, 0);
return mesh;
}
这就创建一条起点为start,终点为end的线,是这样,这里在创建是通过Mesh的拓扑结构MeshTopology实现的,MeshTopology是一个枚举,
使用SetIndices去赋值索引,参数分别是索引数组,选择的拓扑结构,要修改的子网格,还有两种重载自己去查。
2.创建圆Mesh:
代码:
/// <summary>
/// 创建(旋转)圆圈Mesh
/// </summary>
/// <param name="radius">圆圈半径</param>
/// <returns></returns>
private Mesh CreateCircleMesh(float radius)
{
List<Vector3> vertexList = new List<Vector3>();
List<int> indexList = new List<int>();
for (float i = 0; i < 360.0f; i += 5.0f)
{
float rad = Mathf.Deg2Rad * i;
float cosA = Mathf.Cos(rad);
float sinA = Mathf.Sin(rad);
vertexList.Add(new Vector3(radius * cosA, radius * sinA, 0));
if (i != 0)
{
vertexList.Add(new Vector3(radius * cosA, radius * sinA, 0));
}
}
vertexList.Add(new Vector3(radius * Mathf.Cos(Mathf.Deg2Rad * 0), radius * Mathf.Sin(Mathf.Deg2Rad * 0), 0));
for (int i = 0; i < 144; i++)
{
indexList.Add(i);
}
Mesh mesh = new Mesh();
mesh.SetVertices(vertexList);
mesh.SetIndices(indexList.ToArray(), MeshTopology.Lines, 0);
return mesh;
}
代码中144=(360/5)*2(端点相连问题)
当时这里的实现思路我想了三种:
1.使用Mesh,自己创建圆面(俩圆面创建出圆圈)
2.LineRender画圆
3.使用Mesh,创建拓扑结构线画圆圈
说一下我为什么选择最后一种:
首先我选的第一种方式,画出来没问题,但是有一种情况,因为圆是在旋转时用的,旋转线需要旋转,当圆旋转到与你成90度时,你就看不到线了。因为这是一面啊,垂直于你指定接近看不着了。
其次呢,我在想让在任何角度看到他都是一根线一个线圆,所以我想到了LineRender画线,画圆,这次画的很好,实现了想要的效果,但是又出现了问题,就是这都是轴,我需要加碰撞器,我需要拾取处理相应操作,但是我查阅了一下,反正有说可以的,但是我试了一圈不行(我没加上碰撞器,加上有问题,自己踩吧你们),所以最后使用的是拓扑结构线插值出一个圆。我成功了完美实现。
这里加上我的另两种尝试代码:
//private LineRenderer line;
//private int r = 20;
//private int n = 360;
///lineRender画圆
//void Start()
//{
// line = this.GetComponent<LineRenderer>();
// line.positionCount = 360 + 1;
// for (int i = 0; i < n + 1; i++)
// {
// //划线的话2D坐标就行了,这里我们计算x和z坐标轴上的坐标,而y永远是0
// //计算x和z的长度,乘以半径r来得到最终长度
// float x = Mathf.Cos((360 * (i + 1) / n) * Mathf.Deg2Rad) * r;
// float z = Mathf.Sin((360 * (i + 1) / n) * Mathf.Deg2Rad) * r;
// //设置坐标画线
// line.SetPosition(i, new Vector3(0, x, z));
// }
//}
/// <summary>
/// 使用Mesh画两个圆面组成的圆环
/// </summary>
/// <param name="radius"></param>
/// <param name="innerradius"></param>
/// <param name="angledegree"></param>
/// <param name="segments"></param>
/// <returns></returns>
Mesh CreateMesh(float radius, float innerradius, float angledegree, int segments)
{
//vertices(顶点):
int vertices_count = segments * 2 + 2; //因为vertices(顶点)的个数与triangles(索引三角形顶点数)必须匹配
Vector3[] vertices = new Vector3[vertices_count];
float angleRad = Mathf.Deg2Rad * angledegree;
float angleCur = angleRad;
float angledelta = angleRad / segments;
for (int i = 0; i < vertices_count; i += 2)
{
float cosA = Mathf.Cos(angleCur);
float sinA = Mathf.Sin(angleCur);
vertices[i] = new Vector3(radius * cosA, 0, radius * sinA);
vertices[i + 1] = new Vector3(innerradius * cosA, 0, innerradius * sinA);
angleCur -= angledelta;
}
//triangles:
int triangle_count = segments * 6;
int[] triangles = new int[triangle_count];
for (int i = 0, vi = 0; i < triangle_count; i += 6, vi += 2)
{
triangles[i] = vi;
triangles[i + 1] = vi + 3;
triangles[i + 2] = vi + 1;
triangles[i + 3] = vi + 2;
triangles[i + 4] = vi + 3;
triangles[i + 5] = vi;
}
//负载属性与mesh
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
//mesh.uv = uvs;
mesh.RecalculateNormals();
return mesh;
}
3.创建正方形Mesh:
代码:
/// <summary>
/// 创建正方形面Mesh
/// </summary>
/// <param name="size">面尺寸</param>
/// <returns>Mesh对象</returns>
private Mesh CreatePlaneMesh(Vector2 size)
{
var vertices = new List<Vector3>();
var indices = new List<int>();
var x = size.x * 0.5f;
var z = size.y * 0.5f;
vertices.Add(new Vector3(x, 0.0f, z));
vertices.Add(new Vector3(-x, 0.0f, z));
vertices.Add(new Vector3(-x, 0.0f, -z));
vertices.Add(new Vector3(x, 0.0f, -z));
indices.Add(0);
indices.Add(1);
indices.Add(2);
indices.Add(0);
indices.Add(2);
indices.Add(3);
Mesh mesh = new Mesh();
mesh.SetVertices(vertices);
mesh.SetTriangles(indices, 0);
mesh.RecalculateNormals();
return mesh;
}
这个就不说啥了,前面说过创建立方体了。
4.创建圆锥Mesh
代码:
/// <summary>
/// 创建圆锥Mesh
/// </summary>
/// <param name="radius">圆锥底面半径</param>
/// <param name="height">圆锥高度</param>
/// <returns>Mesh对象</returns>
private Mesh CreateConeMesh(float radius, float height)
{
var vertices = new List<Vector3>();
var indices = new List<int>();
vertices.Add(Vector3.zero);
vertices.Add(Vector3.up * height);
var temp = new List<Vector3>();
//底圆面
for (var i = 0.0f; i < 360.0f; i += 30)
{
var rad = Mathf.Deg2Rad * i;
var x = radius * Mathf.Cos(rad);
var z = radius * Mathf.Sin(rad);
temp.Add(new Vector3(x, 0.0f, z));
}
vertices.AddRange(temp);
vertices.AddRange(temp);
for (var i = 2; i <= 13; i++)
{
indices.Add(i);
if (i < 13)
{
indices.Add(i + 1);
}
else
{
indices.Add(2);
}
indices.Add(0);
}
for (var i = 14; i <= 25; i++)
{
indices.Add(i);
indices.Add(1);
if (i < 25)
{
indices.Add(i + 1);
}
else
{
indices.Add(14);
}
}
Mesh mesh = new Mesh();
mesh.SetVertices(vertices);
mesh.SetTriangles(indices, 0);
mesh.RecalculateNormals();
return mesh;
}
到这就需要的编辑轴物件都创建完成了。
效果图: