Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to get uv in sprite atlas

Discussion in 'Shader Graph' started by bucho2, Mar 3, 2020.

  1. bucho2

    bucho2

    Joined:
    Jan 3, 2016
    Posts:
    13
    Hi,

    is there a way to get the uv within a sprite from a sprite atlas?

    the UV node only gives me the uv within the sprite atlas itself.

    thanks
     
  2. bucho2

    bucho2

    Joined:
    Jan 3, 2016
    Posts:
    13
    hmm...

    what I need is the current normalized position on the sprite object... which I usually get via the current uv coords... but in a sprite atlas (without the start and end uvs of the object) how would I get the current normalized position...?

    thanks...
     
  3. BuffaloBadger

    BuffaloBadger

    Joined:
    Aug 17, 2017
    Posts:
    3
    I was looking to do something very similar, so I'll share my solution here as I didn't find any solutions elsewhere.

    I wanted to use a swirl node but I needed to know the UV position of the centre of the sprite for it to work. The solution I found was to add a public property Vector2. Then in code use Sprite.uv to returns an array of uv coords which I guess are the corners (Though I am confused why there would be 8 or sometimes 12 elements!)
    I took the average of all the uvs in the array and set the value in the ShaderGraph with material.SetVector( ) in the Awake() function of an attached GameObject. Works a treat!