Facebook
From Xodius, 1 Month ago, written in C#.
Embed
Download Paste or View Raw
Hits: 112
  1. Endless Parallax Background
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. [ExecuteInEditMode]
  7. public class EndlessParallaxBackground : MonoBehaviour
  8. {
  9.     public ParallaxCamera parallaxCamera;
  10.     List<EndlessParallaxLayer> parallaxLayers = new List<EndlessParallaxLayer>();
  11.     public float viewZone = 5; // The distance at which the layers are repositioned
  12.     private float layerWidth;
  13.     private float totalWidth;
  14.  
  15.     void Start()
  16.     {
  17.         if (parallaxCamera == null)
  18.             parallaxCamera = Camera.main.GetComponent<ParallaxCamera>();
  19.  
  20.         if (parallaxCamera != null)
  21.             parallaxCamera.onCameraTranslate += Move;
  22.  
  23.         SetLayers();
  24.     }
  25.  
  26.     void SetLayers()
  27.     {
  28.         parallaxLayers.Clear();
  29.  
  30.         for (int i = 0; i < transform.childCount; i++)
  31.         {
  32.             EndlessParallaxLayer layer = transform.GetChild(i).GetComponent<EndlessParallaxLayer>();
  33.  
  34.             if (layer != null)
  35.             {
  36.                 layer.name = "Layer-" + i;
  37.                 parallaxLayers.Add(layer);
  38.             }
  39.         }
  40.  
  41.         if (parallaxLayers.Count > 0)
  42.         {
  43.             layerWidth = parallaxLayers[0].GetLayerWidth();
  44.             totalWidth = layerWidth * parallaxLayers.Count;
  45.         }
  46.     }
  47.  
  48.     void Move(float delta)
  49.     {
  50.         foreach (EndlessParallaxLayer layer in parallaxLayers)
  51.         {
  52.             layer.Move(delta);
  53.  
  54.             // Check if the layer needs to be repositioned
  55.             if (layer.transform.position.x < Camera.main.transform.position.x - viewZone)
  56.             {
  57.                 Vector3 newPos = layer.transform.position;
  58.                 newPos.x += totalWidth;
  59.                  layer.transform.positi
  60.  
  61.                 // Reorder layers to maintain continuity
  62.                 layer.transform.SetAsLastSibling();
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. EndlessParallaxLayer:
  69. using System.Collections;
  70. using System.Collections.Generic;
  71. using UnityEngine;
  72.  
  73. [ExecuteInEditMode]
  74. public class EndlessParallaxLayer : MonoBehaviour
  75. {
  76.     public float parallaxFactor;
  77.     private float layerWidth;
  78.  
  79.     private void Start()
  80.     {
  81.         // Assuming the layer is a sprite renderer
  82.         layerWidth = GetComponent<SpriteRenderer>().bounds.size.x;
  83.     }
  84.  
  85.     public void Move(float delta)
  86.     {
  87.         Vector3 newPos = transform.localPosition;
  88.         newPos.x -= delta * parallaxFactor;
  89.         transform.localPosition = newPos;
  90.    
  91.     }
  92.  
  93.     public float GetLayerWidth()
  94.     {
  95.         return layerWidth;
  96.     }
  97.  
  98.     public bool IsOutOfView(float viewZone)
  99.     {
  100.         return transform.position.x < Camera.main.transform.position.x - viewZone;
  101.     }
  102. }
  103.  
  104. ParallaxCamera:
  105. using System.Collections;
  106. using System.Collections.Generic;
  107. using UnityEngine;
  108.  
  109. [ExecuteInEditMode]
  110. public class ParallaxCamera : MonoBehaviour
  111. {
  112.     public delegate void ParallaxCameraDelegate(float deltaMovement);
  113.     public ParallaxCameraDelegate onCameraTranslate;
  114.  
  115.     private float oldPosition;
  116.  
  117.     void Start()
  118.     {
  119.          oldPositi
  120.     }
  121.  
  122.     void Update()
  123.     {
  124.         if (transform.position.x != oldPosition)
  125.         {
  126.             if (onCameraTranslate != null)
  127.             {
  128.                 float delta = oldPosition - transform.position.x;
  129.                 onCameraTranslate(delta);
  130.             }
  131.  
  132.              oldPositi
  133.         }
  134.     }
  135. }