// Get SurfaceIndex ByHit public static int GetSurfaceIndexByHit(RaycastHit hit) { const int negativeIndex = -1; Collider hitCollider = hit.collider; if (hitCollider == null || hitCollider.isTrigger) { return(negativeIndex); } // if TerrainTexture != null, return it. Texture hitTexture = hit.GetTerrainTexture(); if (hitTexture != null) { for (int i = 0; i < GetCount; i++) { foreach (Texture tex in m_Instance.surfaces[i].textures) { if (tex == hitTexture) { return(i); } } } } // Get Object Material if TerrainTexture == null Material hitMaterial = hit.GetMaterial(); if (hitMaterial != null) { for (int i = 0; i < GetCount; i++) { foreach (Material mat in m_Instance.surfaces[i].materials) { if (mat == hitMaterial) { return(i); } } } } return(negativeIndex); }
// Get MaterialName ByHit public static string GetMaterialNameByHit(RaycastHit hit) { Material theMaterial = hit.GetMaterial(); return((theMaterial != null) ? theMaterial.name : "Unknown Material"); }