- Home /
Applying offset to a texture via script
Hello guys, I tried to search something about this but I only found how to apply an offset in a Material, but I want to apply it in one Texture2D that I am using in a fade-in of a level. Can someone help me, thanks.
what do you mean by 'apply it in one Texture2D'? may be you need just edit this texture in iage edit software? or explain a little more what do you want to achieve
Well I have one texture that I use in the fadein of a level. But I don't want that it looks the same thing everytime, so I thought to move the offset of the texture, so every time that you go to the level, it'll be different. It is like the effect that you apply when you are doing a parallax effect.
Answer by Tommynator · Jul 31, 2012 at 03:28 AM
There are two ways to render Texture2Ds in Unity:
1) If you want to render a texture on the surface of a mesh you have no choice but to assign it to a material and tweak the tiling and offset values.
2) If you use it within the GUI system you can do the same by calling:
Rect position = new Rect(0, 0, width, height);
Rect texCoords = new Rect(offsetX, offsetY, 1, 1);
bool alpha = true;
GUI.DrawTextureWithTexCoords(position, texture, texCoords, alpha);
In both cases, make sure to set the wrap mode in the Texture2D import settings to 'Repeat' and not 'Clamp'.
Oh $$anonymous$$ thanks for the answer, but here I can't do this code works correctly and the function's documentation is horrible, can you help me, I can only stretch the texture but can't apply the offset, my code is:
fadeoutTexOffset = new Rect( Random.Range( -Screen.width, Screen.width ), Random.Range( -Screen.height, Screen.height ) , 1,1 ); GUI.DrawTextureWithTexCoords( Rect(0, 0,Screen.width, Screen.height), fadeOutTexture, fadeoutTexOffset, true );
The offset needs to be given in uv coordinates, which is defined in the interval [0,1]. If you apply a full integer offset you will never see any difference :) I don't quite understand what kind of image effect you want to achieve with these random numbers but try Random.Range(0f,1f) ins$$anonymous$$d.
Oh that worked, thank you. What I am wanting to do is something like I did in this video http://www.youtube.com/watch?v=1iVXvVFup9E&list=UUa1H$$anonymous$$-eRnglbWUaiOnSz4sA∈dex=0&feature=plcp at the background. The background are planes and I apply some offset at its materials to give the parallax background effect. For future references the line that I changed is:
fadeoutTexOffset = new Rect( Random.Range( 0f, 1f ), Random.Range( 0f, 1f ) , 1,1 );
Ah I see, but be aware of the fact that the GUI is always rendered ABOVE all 3D content. If you want to have this parallax background effect behind 3D content you should probably just use a simple background plane, tweak the material offset and render it with a second orthogonal camera in a higher depth.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074940im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Offset detail texture in c# 1 Answer
Texture / Material offset doesn't work 1 Answer
Is there a way to tell if a Material is visible? 1 Answer
Add material component to object script-wise 4 Answers
Problem with textures ? 1 Answer