Hiding the grid in the scene view through script.

Hi

Is there a way to hide the unity grid in the scene view through script?

I am writing a custom editor and the unity grid gets in the way, so I want to have a toggle for it but I can’t figure out how to actually disable the grid.

It know it is possible because in the scene view editor you can toggle the grid by unchecking the grid option in the gizmos drop down.

Any help is appreciated.

Thanks

Hi,

You can use Reflection to do it, like that:

using UnityEngine;
using System;
using System.Reflection;

public class ToggleGridUtility
{
    private static Type m_annotationUtility;
    private static PropertyInfo m_showGridProperty;
   
    private static PropertyInfo ShowGridProperty
    {
        get
        {
            if (m_showGridProperty == null)
            {
                m_annotationUtility = Type.GetType("UnityEditor.AnnotationUtility,UnityEditor.dll");
                m_showGridProperty = m_annotationUtility.GetProperty("showGrid", BindingFlags.Static | BindingFlags.NonPublic);
            }
            return m_showGridProperty;
        }
    }

    public static bool ShowGrid
    {
        get
        {
            return (bool)ShowGridProperty.GetValue(null, null);
        }
        set
        {

            ShowGridProperty.SetValue(null, value, null);
        }
    }
}

public class TestShowGrid : MonoBehaviour
{
    [ContextMenu("Toggle Grid")]
    public void ToggleGrid()
    {
        ToggleGridUtility.ShowGrid = !ToggleGridUtility.ShowGrid;
    }
}

Enjoy :wink:

1 Like

Thanks, it works perfectly!