Skip to content

Instantly share code, notes, and snippets.

@Biodam
Created October 8, 2018 20:32
Show Gist options
  • Save Biodam/17166b7c40982f0cf1aa0ccdf1bd99e8 to your computer and use it in GitHub Desktop.
Save Biodam/17166b7c40982f0cf1aa0ccdf1bd99e8 to your computer and use it in GitHub Desktop.
Unity NonDrawingGraphic to detect raycast in UI without extra draw call
using UnityEngine;
using UnityEngine.UI;
/// A concrete subclass of the Unity UI `Graphic` class that just skips drawing.
/// Useful for providing a raycast target without actually drawing anything.
public class NonDrawingGraphic : Graphic
{
public override void SetMaterialDirty() { return; }
public override void SetVerticesDirty() { return; }
/// Probably not necessary since the chain of calls `Rebuild()`->`UpdateGeometry()`->`DoMeshGeneration()`->`OnPopulateMesh()` won't happen; so here really just as a fail-safe.
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
return;
}
}
using UnityEngine;
using UnityEditor;
using UnityEditor.UI;
//Don't forget to put this file inside a 'Editor' folder
[CanEditMultipleObjects, CustomEditor(typeof(NonDrawingGraphic), false)]
public class NonDrawingGraphicEditor : GraphicEditor
{
public override void OnInspectorGUI()
{
base.serializedObject.Update();
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.PropertyField(base.m_Script, new GUILayoutOption[0]);
EditorGUI.EndDisabledGroup();
// skipping AppearanceControlsGUI
base.RaycastControlsGUI();
base.serializedObject.ApplyModifiedProperties();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment