GizMos

Unity中使用Gizmos绘制动态圆环
 void  OnDrawGizmos (){


     if (m_Theta < 0.0001f)
      {
         m_Theta = 0.0001f;
      }


    // 设置矩阵
    Matrix4x4 defaultMatrix  = Gizmos.matrix;
    Gizmos.matrix =GetComponent<Transform>().localToWorldMatrix;


    // 设置颜色
    Color defaultColor  = Gizmos.color;
    Gizmos.color = m_Color;


    // 绘制圆环
    Vector3 beginPoint  = Vector3.zero;
    Vector3 firstPoint  = Vector3.zero;
    float theta  = 0;
    for (theta  = 0; theta < 2 * Mathf.PI; theta += m_Theta){
        float x = activeRange * Mathf.Cos(theta);
        float z = activeRange * Mathf.Sin(theta);
        Vector3 endPoint  = new Vector3(x, 0, z);
        if (theta == 0){
            firstPoint = endPoint;
        }
        else{
            
            Gizmos.DrawLine(beginPoint, endPoint);
        }
        beginPoint = endPoint;
    }


    // 绘制最后一条线段
    Gizmos.DrawLine(firstPoint, beginPoint);


    // 恢复默认颜色
    Gizmos.color = defaultColor;


    // 恢复默认矩阵
    Gizmos.matrix = defaultMatrix;


}
### 功能介绍 Unity GizmosUnity 引擎中用于在场景视图中提供可视化辅助的工具。它主要用于在开发过程中帮助开发者更好地理解和调试场景,不会在游戏运行时显示给玩家。 - **可视化场景元素**:Gizmos 可以将游戏对象的一些属性或隐藏的信息可视化。例如,对于一个导航点,使用 Gizmos 可以在场景中直观地看到其位置和范围,即便导航点本身可能没有可视化的模型。 - **调试辅助**:在调试过程中,Gizmos 能帮助开发者快速定位问题。比如,在检查 AI 角色的巡逻路径时,通过 Gizmos 绘制路径,能清晰地看到角色的移动轨迹,便于发现路径规划中的问题。 - **编辑辅助**:在编辑场景时,Gizmos 可以提供额外的参考信息。例如,在布置灯光时,使用 Gizmos 可以直观地看到灯光的照射范围和角度,方便调整灯光参数。 ### 使用方法 以下是 Unity Gizmos 的基本使用方法: #### 1. 在脚本中使用 Gizmos 要使用 Gizmos,需要在脚本中重写 `OnDrawGizmos` 或 `OnDrawGizmosSelected` 方法。 ```csharp using UnityEngine; public class GizmosExample : MonoBehaviour { // 当对象被选中时绘制 Gizmos void OnDrawGizmosSelected() { // 设置 Gizmos 的颜色 Gizmos.color = Color.red; // 绘制一个球体 Gizmo Gizmos.DrawSphere(transform.position, 1f); } // 始终绘制 Gizmos void OnDrawGizmos() { // 设置 Gizmos 的颜色 Gizmos.color = Color.green; // 绘制一个线框立方体 Gizmo Gizmos.DrawWireCube(transform.position, new Vector3(2f, 2f, 2f)); } } ``` #### 2. 常用的 Gizmos 绘制方法 - `Gizmos.DrawSphere(Vector3 center, float radius)`:在指定位置绘制一个实心球体。 - `Gizmos.DrawWireSphere(Vector3 center, float radius)`:在指定位置绘制一个线框球体。 - `Gizmos.DrawCube(Vector3 center, Vector3 size)`:在指定位置绘制一个实心立方体。 - `Gizmos.DrawWireCube(Vector3 center, Vector3 size)`:在指定位置绘制一个线框立方体。 - `Gizmos.DrawLine(Vector3 from, Vector3 to)`:绘制一条从 `from` 到 `to` 的直线。 #### 3. 使用自定义图标 可以使用自定义的图标作为 Gizmos。首先,将图标文件(通常为 `.png` 格式)放置在项目的 `Assets` 文件夹中,然后在脚本中使用 `Gizmos.DrawIcon` 方法。 ```csharp using UnityEngine; public class CustomIconGizmos : MonoBehaviour { public Texture2D customIcon; void OnDrawGizmos() { if (customIcon != null) { Gizmos.DrawIcon(transform.position, customIcon.name, true); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值