Search Unity

Using a custom mesh with a sprite?

Discussion in '2D' started by TylerPerry, Feb 10, 2014.

  1. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    Is it possible to apply a sprite to a custom mesh?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, the idea of sprites is that they create their own meshes.

    --Eric
     
  3. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    Ok thanks :D
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    You can, if you are using the whole "Sprite" texture, but not the individual sprites that you trim. First, create a new Material, now assign your Sprite as the texture input, it will accept it, but it won't display the transparency properly, to fix that, you need to click the Shader drop down list, and go to "Sprites>Default". It will now have your sprite, with the transparency, taking advantage of the Sprite shader's efficiency. Even the pixel-based selection of it will work, not allowing you to select the object if you click a transparent area of it.
     
    Last edited: Feb 10, 2014
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's not a sprite anymore if you do that. By "sprite" I mean an object using a SpriteRenderer component. An actual sprite has no mesh property and it's not possible to access the mesh. If you don't use a SpriteRenderer component then it's just a texture on a mesh, which has been possible forever, and won't have any of the specific sprite functionality. The pixel-based selection is standard for any object with a transparent shader.

    --Eric
     
  6. ofusion

    ofusion

    Joined:
    Dec 5, 2013
    Posts:
    27
    I also want to generate custom sprite mesh, but find that Unity does not support custom sprite mesh generation algorithm for SpriteRenderer. So I guess I have to use MeshRenderer instead of SpriteRenderer.
     
  7. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
  8. gaps

    gaps

    Joined:
    Jan 1, 2014
    Posts:
    15
    I keep coming back to this thread from time to time, so here are my two cents:
    @Invertex answer is perfect for advanced users that want to use shared meshes. Despite not having a sprite renderer, it will pretty much behave visually as a a sprite renderer would, but of course you need to handle it differently as it lacks some of the sprite renderer methods (and has other of its own).
    @pointcache the OverrideGeometry is an improvement, but with a slightly different workflow. It is likely preferable to having to deal with meshes, but might be easier to use with other tools/scripts that make mesh manipulations for example.
    • In the case of the mesh approach you should build the mesh (using local object coordinates) and then set it as the sharedMesh of the MeshFilter for each object (don't create multiple meshes if they are all the same!);
    • When using the override geometry, a new sprite must be created with a rect corresponding to its bounds (also using local object coordinates), and then set it as the sprite of the SpriteRenderer for each object (don't create multiple sprites if they are all the same!).
    EDIT: Forgot another good use case for the mesh approach: When you need multiple textures to reuse the same mesh, that is not possible with sprites. You would need to construct multiple sprites, thus not being able to share the geometry amongst several of them.
     
    Last edited: Feb 7, 2024