通过纹理偏移滚动2D/3D背景 [英] Scroll 2D/3D background via texture offset

查看:135
本文介绍了通过纹理偏移滚动2D/3D背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在Unity中使用四边形显示纹理来无限滚动2D背景.我的想法是根据玩家的位置更改四边形的偏移量.由于某些原因,当我更改偏移量时,我的图像无法正确重复,并且一旦偏移量达到2,就会完全消失.

I have been trying to make an infinite scrolling 2D background in Unity using a quad to display a Texture. My idea was to change the offset of the quad depending on the player's position. For some reason when I change the offset my image does not repeat properly and once an offset of 2 has been reached completely disappears.

在我的纹理上包含3个不同x偏移值的图像

如果您可以找我,如果有人知道如何解决此问题,将不胜感激.

If anyone knows how to fix this if you could get back to me it would be much appreciated.

推荐答案

选择原始的Texture而不是GameOBject.

Select the original Texture not the GameOBject.

1 .将纹理类型更改为纹理.

2 .将环绕模式更改为重复.

3 .单击应用".完成!

3. Click Apply. Done!

用于纹理的Unity菜单的最新版本已更改.见下图:

Latest version of Unity menu for Textures has changed. See the below image:

现在可以通过脚本对纹理进行动画处理,

Now to animate the texture from script,

1 .创建一个四边形GameObject -> 3D Object ->Quad.将Quad缩放到所需大小

1. Create a Quad GameObject -> 3D Object ->Quad. Scale the Quad to the size you want

2 .创建一个灯光. GameObject->Light->Directional Light.您可以根据自己的喜好调节光强度.

2. Create a light. GameObject->Light->Directional Light. You can adjust the light Intensity to whatever you like.

3 .将您的纹理/精灵拖到场景视图"中的四边形上.

3. Drag your Texture/Sprite to the Quad in the Scene View.

现在输入您的脚本:

public GameObject quadGameObject;
private Renderer quadRenderer;

float scrollSpeed = 0.5f;

void Start()
{
    quadRenderer = quadGameObject.GetComponent<Renderer>();
}

void Update()
{
    Vector2 textureOffset = new Vector2(Time.time*scrollSpeed,0);
    quadRenderer.material.mainTextureOffset = textureOffset;
}


对于2D,您还可以使用 GameObject ---> 3D对象中的 Plane Quad >菜单,上面的代码应该可以正常工作.


For 2D, you can also use a Plane or Quad from the GameObject ---> 3D Object menu and the code above should work fine.

这篇关于通过纹理偏移滚动2D/3D背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆