-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdaptiveSkyboxScaler.cs
More file actions
105 lines (82 loc) · 3.37 KB
/
AdaptiveSkyboxScaler.cs
File metadata and controls
105 lines (82 loc) · 3.37 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// For Project: "Murders Drones Endless Way".
// 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;
namespace RimuruDev
{
[SelectionBase]
[DisallowMultipleComponent]
[HelpURL("https://github.com/RimuruDev/AdaptiveSkyboxScaler")]
public sealed class AdaptiveSkyboxScaler : MonoBehaviour
{
private static readonly int FrontTex = Shader.PropertyToID("_FrontTex");
[SerializeField] private bool enableDebugLog = true;
[SerializeField] private SpriteRenderer backgroundSpriteRenderer;
private Camera mainCamera;
private Material skyboxMaterial;
private Texture2D skyboxTexture;
private Sprite skyboxSprite;
private void Awake()
{
CacheComponents();
ValidateComponents();
SetupBackground();
}
private void CacheComponents()
{
mainCamera = Camera.main;
skyboxMaterial = RenderSettings.skybox;
if (skyboxMaterial != null)
skyboxTexture = skyboxMaterial.GetTexture(FrontTex) as Texture2D;
}
private void ValidateComponents()
{
if (backgroundSpriteRenderer == null)
LogError("SpriteRenderer is not assigned.");
if (mainCamera == null)
LogError("Main Camera not found.");
if (skyboxMaterial == null)
LogError("No skybox material found in RenderSettings.");
if (skyboxTexture == null)
LogError("Skybox material does not have a texture named _FrontTex.");
}
private void SetupBackground()
{
if (backgroundSpriteRenderer == null || mainCamera == null || skyboxTexture == null)
return;
Rect spriteRect = new Rect(0, 0, skyboxTexture.width, skyboxTexture.height);
skyboxSprite = Sprite.Create(skyboxTexture, spriteRect, new Vector2(0.5f, 0.5f), 100f);
backgroundSpriteRenderer.sprite = skyboxSprite;
backgroundSpriteRenderer.sortingOrder = -999;
ScaleBackground();
}
private void LateUpdate() =>
ScaleBackground();
private void ScaleBackground()
{
if (mainCamera == null || backgroundSpriteRenderer.sprite == null)
return;
var targetHeight = mainCamera.orthographicSize * 2;
var targetWidth = targetHeight * Screen.width / Screen.height;
Vector2 backgroundSize = backgroundSpriteRenderer.sprite.bounds.size;
var widthRatio = targetWidth / backgroundSize.x;
var heightRatio = targetHeight / backgroundSize.y;
var targetScale = Mathf.Max(widthRatio, heightRatio);
backgroundSpriteRenderer.transform.localScale = Vector3.one * targetScale;
}
private void LogError(string message)
{
if (!enableDebugLog)
return;
Debug.LogError(message, gameObject);
}
}
}