-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackgroundScrolling.cs
More file actions
43 lines (36 loc) · 1.33 KB
/
BackgroundScrolling.cs
File metadata and controls
43 lines (36 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: rimuru.dev@gmail.com
// - GitHub: https://github.com/RimuruDev
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub Organizations: https://github.com/Rimuru-Dev
//
// **************************************************************** //
using UnityEngine;
using UnityEngine.UI;
namespace RimuruDev
{
[SelectionBase]
[DisallowMultipleComponent]
[RequireComponent(typeof(RawImage))]
public sealed class BackgroundScrolling : MonoBehaviour
{
[SerializeField] private Vector2 scrollPositionXY;
private RawImage rawImage;
private void Start() =>
rawImage = GetComponent<RawImage>();
private void Update() =>
SetNewRawImageUV(Time.deltaTime);
private void SetNewRawImageUV(float delta) =>
rawImage.uvRect = CalculateRectPosition(delta);
private Rect CalculateRectPosition(float delta)
{
var offset = new Vector2(scrollPositionXY.x, scrollPositionXY.y);
var position = rawImage.uvRect.position + offset * delta;
var size = rawImage.uvRect.size;
return new Rect(position, size);
}
}
}