Advertisement
maxhacker11

ParallaxBackground.cs

Jul 23rd, 2023
13,310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | Source Code | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. [ExecuteInEditMode]
  5. public class ParallaxBackground : MonoBehaviour
  6. {
  7.     public ParallaxCamera parallaxCamera;
  8.     List<ParallaxLayer> parallaxLayers = new List<ParallaxLayer>();
  9.  
  10.     void Start()
  11.     {
  12.         if (parallaxCamera == null)
  13.             parallaxCamera = Camera.main.GetComponent<ParallaxCamera>();
  14.  
  15.         if (parallaxCamera != null)
  16.             parallaxCamera.onCameraTranslate += Move;
  17.  
  18.         SetLayers();
  19.     }
  20.  
  21.     void SetLayers()
  22.     {
  23.         parallaxLayers.Clear();
  24.  
  25.         for (int i = 0; i < transform.childCount; i++)
  26.         {
  27.             ParallaxLayer layer = transform.GetChild(i).GetComponent<ParallaxLayer>();
  28.  
  29.             if (layer != null)
  30.             {
  31.                 layer.name = "Layer-" + i;
  32.                 parallaxLayers.Add(layer);
  33.             }
  34.         }
  35.     }
  36.  
  37.     void Move(float delta)
  38.     {
  39.         foreach (ParallaxLayer layer in parallaxLayers)
  40.         {
  41.             layer.Move(delta);
  42.         }
  43.     }
  44. }
Tags: Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement