首页 > 其他分享 >Unity绘制参考网格

Unity绘制参考网格

时间:2023-06-17 22:36:04浏览次数:50  
标签:SerializeField lineRendererX Vector3 网格 Color Unity new 绘制 lineRendererZ


1.仿制Maya参考网格 

using UnityEngine;

public class ReferenceGridRenderer : MonoBehaviour
{
    //[SerializeField] Color color = new Color(1, 1, 1, 0.5f);
    [SerializeField] [Range(1, 1000)] float range = 20;
    [SerializeField] [Range(0.1f, 100)] float interval = 1;
    [SerializeField] float centerLineWidth = 0.05f;
    [SerializeField] float outerLineWidth = 0.01f;
    [SerializeField] Color centerLineColor = Color.white;
    [SerializeField] Color outerLineColor = new Color(1, 1, 1, 0.5f);

    void Start()
    {
        CreateReferenceGrid();
    }

    void CreateReferenceGrid()
    {
        GameObject referenceGrid = new GameObject("Reference Grid");

        for (float i = -range; i <= range; i += interval)
        {
            GameObject lineObjectX = new GameObject("Line X");
            lineObjectX.transform.SetParent(referenceGrid.transform);
            LineRenderer lineRendererX = lineObjectX.AddComponent<LineRenderer>();
            lineRendererX.material = new Material(Shader.Find("Sprites/Default"));
            lineRendererX.startColor = (i == 0) ? centerLineColor : outerLineColor;
            lineRendererX.endColor = (i == 0) ? centerLineColor : outerLineColor;
            lineRendererX.startWidth = (i == 0) ? centerLineWidth : outerLineWidth;
            lineRendererX.endWidth = (i == 0) ? centerLineWidth : outerLineWidth;

            GameObject lineObjectZ = new GameObject("Line Z");
            lineObjectZ.transform.SetParent(referenceGrid.transform);
            LineRenderer lineRendererZ = lineObjectZ.AddComponent<LineRenderer>();
            lineRendererZ.material = new Material(Shader.Find("Sprites/Default"));
            lineRendererZ.startColor = (i == 0) ? centerLineColor : outerLineColor;
            lineRendererZ.endColor = (i == 0) ? centerLineColor : outerLineColor;
            lineRendererZ.startWidth = (i == 0) ? centerLineWidth : outerLineWidth;
            lineRendererZ.endWidth = (i == 0) ? centerLineWidth : outerLineWidth;

            lineRendererX.SetPositions(new Vector3[] { new Vector3(-range, 0, i), new Vector3(range, 0, i) });
            lineRendererZ.SetPositions(new Vector3[] { new Vector3(i, 0, -range), new Vector3(i, 0, range) });
        }
    }
}

2.仿制 ThreeEditor 参考网格

using UnityEngine;

public class ReferenceGridRenderer : MonoBehaviour
{
    [SerializeField] [Range(1, 1000)] float range = 20;
    [SerializeField] [Range(0.1f, 100)] float interval = 1;
    [SerializeField] float centerLineWidth = 0.05f;
    [SerializeField] float outerLineWidth = 0.01f;
    [SerializeField] Color centerLineColor = Color.white;
    [SerializeField] Color outerLineColor = new Color(1, 1, 1, 0.5f);
    [SerializeField] float fifthLineWidth = 0.02f;
    [SerializeField] Color fifthLineColor = new Color(1, 0, 0, 0.5f);

    void Start()
    {
        CreateReferenceGrid();
    }

    void CreateReferenceGrid()
    {
        GameObject referenceGrid = new GameObject("Reference Grid");

        int count = 0;
        for (float i = -range; i <= range; i += interval)
        {
            GameObject lineObjectX = new GameObject("Line X");
            lineObjectX.transform.SetParent(referenceGrid.transform);
            LineRenderer lineRendererX = lineObjectX.AddComponent<LineRenderer>();
            lineRendererX.material = new Material(Shader.Find("Sprites/Default"));
            if (i == 0)
            {
                lineRendererX.startColor = centerLineColor;
                lineRendererX.endColor = centerLineColor;
                lineRendererX.startWidth = centerLineWidth;
                lineRendererX.endWidth = centerLineWidth;
            }
            else if (count % 5 == 0)
            {
                lineRendererX.startColor = fifthLineColor;
                lineRendererX.endColor = fifthLineColor;
                lineRendererX.startWidth = fifthLineWidth;
                lineRendererX.endWidth = fifthLineWidth;
            }
            else
            {
                lineRendererX.startColor = outerLineColor;
                lineRendererX.endColor = outerLineColor;
                lineRendererX.startWidth = outerLineWidth;
                lineRendererX.endWidth = outerLineWidth;
            }

            GameObject lineObjectZ = new GameObject("Line Z");
            lineObjectZ.transform.SetParent(referenceGrid.transform);
            LineRenderer lineRendererZ = lineObjectZ.AddComponent<LineRenderer>();
            lineRendererZ.material = new Material(Shader.Find("Sprites/Default"));
            if (i == 0)
            {
                lineRendererZ.startColor = centerLineColor;
                lineRendererZ.endColor = centerLineColor;
                lineRendererZ.startWidth = centerLineWidth;
                lineRendererZ.endWidth = centerLineWidth;
            }
            else if (count % 5 == 0)
            {
                lineRendererZ.startColor = fifthLineColor;
                lineRendererZ.endColor = fifthLineColor;
                lineRendererZ.startWidth = fifthLineWidth;
                lineRendererZ.endWidth = fifthLineWidth;
            }
            else
            {
                lineRendererZ.startColor = outerLineColor;
                lineRendererZ.endColor = outerLineColor;
                lineRendererZ.startWidth = outerLineWidth;
                lineRendererZ.endWidth = outerLineWidth;
            }

            lineRendererX.SetPositions(new Vector3[] { new Vector3(-range, 0, i), new Vector3(range, 0, i) });
            lineRendererZ.SetPositions(new Vector3[] { new Vector3(i, 0, -range), new Vector3(i, 0, range) });

            count++;
        }
    }
}

标签:SerializeField,lineRendererX,Vector3,网格,Color,Unity,new,绘制,lineRendererZ
From: https://blog.51cto.com/u_14359474/6506397

相关文章

  • 如何让UnityEditor后台运行
    最近在玩ml-agents,发现训练的时候点击别的窗口,UnityEditor就挂起不接着运行了。google了一下发现可以通过点击Edit->ProjectSettings->Player->ResolutionandPresentation,然后勾选RunInBackground即可解决该问题:......
  • 如何在 Unity 游戏中集成 AI 语音识别?
    简介语音识别是一项将语音转换为文本的技术,想象一下它如何在游戏中发挥作用?发出命令操纵控制面板或者游戏角色、直接与NPC对话、提升交互性等等,都有可能。本文将介绍如何使用HuggingFaceUnityAPI在Unity游戏中集成SOTA语音识别功能。您可以访问itch.io网站下载Un......
  • [数据分析与可视化] Python绘制数据地图3-GeoPandas使用要点
    本文主要介绍GeoPandas的使用要点。GeoPandas是一个Python开源项目,旨在提供丰富而简单的地理空间数据处理接口。GeoPandas扩展了Pandas的数据类型,并使用matplotlib进行绘图。GeoPandas官方仓库地址为:GeoPandas。GeoPandas的官方文档地址为:GeoPandas-doc。本文主要参考GeoPandasEx......
  • Unity 编辑器直接运行正常,打包后的程序运行异常问题
     打包后的程序运行报错:base="System.MissingMethodException:Defaultconstructornotfoundfortypelog4net.Repository.Hierarchy.Hierarchy\r\natSystem.RuntimeType.CreateInstanceMono(System.BooleannonPublic,System.BooleanwrapExceptions)[0x00076]in&l......
  • matplot画网格线分层级的用法
    网格线分级一般情况下,我们设置网格线都不会太在意密度和精度的问题,导致下图这种看的有些眼花,横坐标在比较密集的时候很容易看窜行。所以如果可以把精度设置一下,或者设置一下主级和副级网格线,就会条理很多MultiLocator首先引入模块frommatplotlib.tickerimportMultipleLoc......
  • Unity 打包程序调试
          ......
  • Compile Unity jslib in command
    CompileUnityjslibincommand(JinQing’sColumn,May.,2023)MyUnityprojecthasajslibfile,whichhasmanysyntaxerrors.ButUnitydoesnotreportthesejsliberrorslikecsfiles,untiltheWebGLbuildfailswithaerrorlikethis:Failedprocessstd......
  • 软件测试|教你用matplotlib绘制热力图
    前言热力图,是一种通过对色块着色来显示数据的统计图表。绘图时,需指定颜色映射的规则。例如,较大的值由较深的颜色表示,较小的值由较浅的颜色表示;较大的值由偏暖的颜色表示,较小的值由较冷的颜色表示,等等。当然,只要我们提供数据,Python就能够绘制出热力图。下面我们就来介绍一下如何......
  • Unity3D:场景视图视图选项
    推荐:将NSDT场景编辑器加入你的3D工具链3D工具集:NSDT简石数字孪生“场景视图视图选项”工具栏您可以使用“场景视图视图选项”工具栏“叠加”来选择用于查看场景以及启用/禁用照明和音频的各种选项。这些控件仅在开发期间影响场景视图,对构建的游戏没有影响。绘制模式(Drawmo......
  • Unity大场景优化
    在Unity中,你可以使用视距剔除(LOD)和对象池等技术来解决场景中远离摄像机的物体隐藏和性能优化的问题。视距剔除(LOD):视距剔除是一种根据摄像机与物体之间的距离,动态地调整物体的细节级别或替代模型的技术。这意味着当物体远离摄像机时,可以使用较低的细节模型替代它们,从而减少渲染......