- Home /
Scrolling Image with no renderer and no SetTextureOffset
Now I know this question is asked a lot but they tend to use the same method which in my case I can not use. For some reason when I use the SetTextureOffset method it effects all UI in my canvas. I would like if someone could point me towards a different method. Any help is appreciated. And I mean I dont care if you spam links as long as they are helpful because I have been stuck on this for a few days.
$$anonymous$$aybe give you UI element a different (uniqe) material? I assume that all your UI elements share the same material, and since they are not instances, the SetTextureOffset affects them all.
You could also use a shader which scrolls the texture, of course that'd need a different material as well.
Agreed. TextureOffset is a property of the $$anonymous$$aterial. All objects using the same material will have the same TextureOffset. You can create a new material at runtime, for this particular purpose and set THAT material's TextureOffset.
Alternatively, you can create a new material in the project (I'd select original mat then click Edit->duplicate), set it's TextureOffset manually, and assign that new material to the object in question.
" it screws with my sprite " separate issue.
First: does this resolve the OP issue of "it effects all UI in my canvas"
@Glurth not necessarily but it also it does because then I would be using SetTextureOffset. The material overlaps over my sprites and colors in the empty space of my sprite. I’d rather use script to move the pixels in the texture but I haven’t found anything to help me achieve that. So I’m at a loss
I've had this issue myself. Not with SetTextureOffset but just in general trying to set a property on a material per UI element. The problem is that Unity's CanvasRenderer doesn't have a Set/GetPropertyBlock methods. So even if you write a custom shader with [PerRenderer] tags it still wouldn't work. 'Color' was only property that they support changing per UI element. So my hack involved passing in the information I needed to change per UI element in the alpha channel. This might or might not work for you, but if it does and you need a x/y offset, you could pack them in the alpha channel and pass it along your color. Write a custom shader that unpacks the alpha into the texture offsets and apply them. The other approach of course is to use a different material per UI element, but that's a waste of draw calls.
I have been working hard at getting something working and this is all I got but it does not move the pixels at all. Any suggestions?
//Get the texture from the sprite
Texture2D texture = $$anonymous$$iPause$$anonymous$$enu.GetComponent<Image>().sprite.texture;
// Create a temporary RenderTexture of the same size as the texture
RenderTexture tmp = RenderTexture.GetTemporary(
texture.width,
texture.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
// Blit the pixels on texture to the RenderTexture
Graphics.Blit(texture, tmp);
// Backup the currently set RenderTexture
RenderTexture previous = RenderTexture.active;
// Set the current RenderTexture to the temporary one we created
RenderTexture.active = tmp;
// Create a new readable Texture2D to copy the pixels to it
Texture2D myTexture2D = new Texture2D(tmp.width, tmp.height);
// Copy the pixels from the RenderTexture to the new Texture
myTexture2D.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0);
//Apply pixel changes
myTexture2D.Apply();
// Reset the active RenderTexture
RenderTexture.active = previous;
// Release the temporary RenderTexture
RenderTexture.ReleaseTemporary(tmp);
for (int x = 0; x < myTexture2D.width; x++)
{
for (int y = 0; y < myTexture2D.height; y++)
{
Color colr = texture.GetPixel(x, y);
myTexture2D.SetPixel(x + 1, y + 1, colr);
}
}
myTexture2D.Apply();
Sprite sprite = Sprite.Create(myTexture2D, new Rect(0, 0, myTexture2D.width, myTexture2D.height), $$anonymous$$iPause$$anonymous$$enu.GetComponent<Image>().sprite.pivot);
$$anonymous$$iPause$$anonymous$$enu.GetComponent<Image>().overrideSprite = sprite;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to start Scroll Rect content off screen 0 Answers
Renderer on object disabled after level reload 1 Answer
Scroll rect, autoscroll content 1 Answer