- Home /
Question by
F3RILLA · Aug 21, 2014 at 07:57 PM ·
2dbackgroundoffsetscrolling
2D Background Generator
Hey guys,
I'm wondering if it's possible to implement a 2D Background Generator where the background image is randomly selected from an array. I could have just created a long texture for the background, but I want the background to be different each time the game is played. This is the script I have so far:
using UnityEngine;
using System.Collections;
public class BackgroundGenerator : MonoBehaviour
{
public float scrollSpeed = 0.5f;
private Vector2 savedOffset;
//Use this for initialization
void Start ()
{
savedOffset = renderer.sharedMaterial.GetTextureOffset ("_MainTex");
}
//Update is called once per frame
void Update ()
{
#region Scrolling Texture
float y = Mathf.Repeat (Time.time * scrollSpeed, 1);
Vector2 offset = new Vector2 (savedOffset.x, y);
renderer.sharedMaterial.SetTextureOffset ("_MainTex", offset);
#endregion
}
}
Comment
Answer by pinkhair · Aug 21, 2014 at 09:16 PM
If I am reading it right, you want to tile a series of separate textures in a random order, as opposed to picking a single one each time to play? You could try making a shader with two textures, and pick a new texture for whichever texture is offscreen at the time.