Skip to content

Instantly share code, notes, and snippets.

@AubreyF
Forked from MattRix/RXLookingGlass.cs
Created April 17, 2017 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AubreyF/4d2f0cc73045ad84457695433c6d3004 to your computer and use it in GitHub Desktop.
Save AubreyF/4d2f0cc73045ad84457695433c6d3004 to your computer and use it in GitHub Desktop.
This class wraps an internal Unity method that lets you to check if a ray intersects a mesh. Place it in an Editor folder.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
using System.Linq;
using System.Reflection;
[InitializeOnLoad]
public class RXLookingGlass
{
public static Type type_HandleUtility;
protected static MethodInfo meth_IntersectRayMesh;
static RXLookingGlass()
{
var editorTypes = typeof(Editor).Assembly.GetTypes();
type_HandleUtility = editorTypes.FirstOrDefault(t => t.Name == "HandleUtility");
meth_IntersectRayMesh = type_HandleUtility.GetMethod("IntersectRayMesh",(BindingFlags.Static | BindingFlags.NonPublic));
}
public static bool IntersectRayMesh(Ray ray, MeshFilter meshFilter, out RaycastHit hit)
{
return IntersectRayMesh(ray,meshFilter.mesh,meshFilter.transform.localToWorldMatrix,out hit);
}
public static bool IntersectRayMesh(Ray ray, Mesh mesh, Matrix4x4 matrix, out RaycastHit hit)
{
var parameters = new object[]{ray,mesh,matrix,null};
bool result = (bool)meth_IntersectRayMesh.Invoke(null,parameters);
hit = (RaycastHit)parameters[3];
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment