Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to get material by mouse if the object have multi materials?

Discussion in 'Scripting' started by Xhitman, Aug 4, 2019.

  1. Xhitman

    Xhitman

    Joined:
    Oct 30, 2015
    Posts:
    297
    I know how to do it if the object have only ONE material.

    But if there are multi materials, how can I know which material the mouse point to?
     
  2. Xhitman

    Xhitman

    Joined:
    Oct 30, 2015
    Posts:
    297
    Since no one reply, should I assume it is impossible?
     
  3. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    837
    If you're using raycasts to do your check there's a triangleIndex in the RaycastHit object. Assuming mesh equality between collider and renderer you could poerhaps take this index to figure out what submesh the triangle belongs to, which would give you the material.
    But it might just be easier to split the model per material.
     
  4. Xhitman

    Xhitman

    Joined:
    Oct 30, 2015
    Posts:
    297
    Thanks.
     
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,384
    This is complex task including mesh topology analysis. You don't want to do this. Make fake triggers for clicking around and that's it.
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    5,538
    maybe could put some info in the vertex color or some other (unused) attribute?
    so, if selected vertex has color.r==1, then this part is using material#1