前言
- 为了确定“手术刀”的网格上的边界点(切割线,用一个小面片来模拟),有两种途径,第一种:控制一个带颜色的小球的position来显示顶点的位置;第二种,利用OnDrawGizmos函数来做标记。
代码实现
-
由于前者实用性不高,不做过多讲解。采用这样样的方式进行操控就可以
public GameObject ball; ball.transform.position = transform.InverseTransformPoint( transform.GetComponent<MeshFilter>().sharedMesh.vertices[10]);//10为随便举的例子
OnDrawGizmos函数
- 该函数每帧运行 且不需要运行Unity函数 主要用途是作为调试,值得注意的是,一般情况该函数绘制点不会出现在Scene中,但可以在该处进行设置
选中该选项,Scene中也将可见!!! - 请注意,OnDrawGizmos将使用相对于“场景视图”的鼠标位置。如果组件在检查器中折叠,
则不会调用此函数。选择游戏对象后,使用OnDrawGizmosSelected绘制小控件。 - 实现代码
void OnDrawGizmos()
{
Debug.Log("beging to draw");
Gizmos.color = Color.red;//设置颜色
/*
* 在起初找点的范围时
* 采用for循环 确定某一个区域(不断缩小区域)
* 然后再精确的查找
*/
for (int i = 729; i < 730; i += 2)
{
Gizmos.DrawSphere(transform.TransformPoint(
gameObject.GetComponent<MeshFilter>().sharedMesh.vertices[i - 1]), 0.001f);
Gizmos.DrawSphere(transform.TransformPoint(
gameObject.GetComponent<MeshFilter>().sharedMesh.vertices[i]), 0.001f);
}
Gizmos.DrawSphere(transform.TransformPoint(
gameObject.GetComponent<MeshFilter>().sharedMesh.vertices[761]), 0.001f);
}
- 需要注意的是:标注的坐标位置应转化为世界坐标,效果如图:
OnDrawGizmosSelected函数
-
与OnDrawGizmos函数相似,但该函数仅在物体被选中时被执行,即:其挂载的物体被选中,执行该函数。
-
官方示例:
using UnityEngine; public class gizmoTest : MonoBehaviour { public float explosionRadius = 5.0f; void OnDrawGizmosSelected() { // Display the explosion radius when selected Gizmos.color = new Color(1, 1, 0, 0.75F); Gizmos.DrawSphere(transform.position, explosionRadius); } }
Gizmos类
-
静态方法
函数名 解释 DrawCube 绘制实心框. Draw a solid box with center and size. DrawFrustum 绘制摄影机视锥体小控件. Draw a camera frustum using the currently set Gizmos.matrix for it’s location and rotation. DrawGUITexture 绘制纹理 .Draw a texture in the Scene. DrawIcon 绘制小图标.Draw an icon at a position in the Scene view. DrawLine Draws a line starting at from towards to. DrawMesh Draws a mesh. DrawRay Draws a ray starting at from to from + direction. DrawSphere 球.Draws a solid sphere with center and radius. DrawWireCube 立方体.Draw a wireframe box with center and size. DrawWireMesh 空心网格.Draws a wireframe mesh. DrawWireSphere 空心球. Draws a wireframe sphere with center and radius.