Author Topic: [Solved] UITexture offset - shader?  (Read 1971 times)

Mikkel

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 4
    • View Profile
[Solved] UITexture offset - shader?
« on: April 15, 2015, 03:20:25 AM »
Hi, I've been using NGUI for a couple of years now (paid version, of course!), but now I'm moving into uncharted waters (as it were), at least for me.

I'm in need of being able to "offset" the UVs of a UITexture, without clipping the texture. I can offset it just fine (see example), but it seems to clip the texture, then.

If I try manipulating the UVRect X or Y, parts of the texture gets cut - probably as intended, but it doesn't do what I need. What I would like is to be able to offset the uvs (I think), via code at runtime.

I'm not going to be doing it every frame, only after certain user-initiated events complete, whereupon I will need to update the texture. Moving or resizing the UITexture itself is not really an option, I just want to offset the image that appears on it (which is tileable of course!).

I've tried looking into customizing the shader, but honestly I'm in over my head on that subject.

Any chance anyone can shine some light on this subject?

Included is a (simplified) example of the actual vs desired results.
« Last Edit: April 15, 2015, 07:50:49 AM by Mikkel »

Mikkel

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 4
    • View Profile
Re: UITexture offset - shader?
« Reply #1 on: April 15, 2015, 04:47:44 AM »
Well... I may have found the answer myself (doesn't that always happen right after you ask for help?).

Solution 1:

If I do

  1.  
  2. //bgTex is the UITexture component in question
  3.  
  4. bgTex.mainTexture.wrapMode = TextureWrapMode.Repeat;
  5.  
  6. //and then do whatever offset is needed:
  7. Rect uvRect = bgTex.uvRect;
  8. uvRect.x = 0.25f;
  9. bgTex.uvRect = uvRect;
  10.  
  11.  

Solution 2, which is simpler, and just fine in case you don't need or want to do this in code:

Set the texture to be a.. texture (rather than sprite) :-[ in the editor, and set the wrapmode to repeat (which is exactly what I do above, in code). Then manipulate the UVRect either in code or editor as you prefer. Simple - and not really much of an NGUI question at all, but I leave the answer anyway, in case someone suffering from too little sleep (like yours truly) runs into the same thing.