-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoOptionsManager.cs
More file actions
77 lines (68 loc) · 2.35 KB
/
VideoOptionsManager.cs
File metadata and controls
77 lines (68 loc) · 2.35 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
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class VideoOptionsManager : MonoBehaviour
{
[Header("Video")]
public TMP_Dropdown Resolution;
private Resolution[] resolutions;
private int currentResolutionIndex;
void Start()
{
EventSystem.current.SetSelectedGameObject(Resolution.gameObject);
resolutions = Screen.resolutions;
List<string> resolutionStrings = new();
foreach (var res in resolutions)
{
resolutionStrings.Add(res.ToString());
}
Resolution.ClearOptions();
Resolution.AddOptions(resolutionStrings);
currentResolutionIndex = System.Array.IndexOf(resolutions, Screen.currentResolution);
Resolution.value = currentResolutionIndex;
Scroll();
}
public void OnResolutionChange()
{
currentResolutionIndex = Resolution.value;
var res = resolutions[currentResolutionIndex];
var width = res.width;
var height = res.height;
Screen.SetResolution(width, height, true);
EventSystem.current.SetSelectedGameObject(Resolution.gameObject);
}
public void OnHover()
{
if (Resolution.interactable)
{
EventSystem.current.SetSelectedGameObject(Resolution.gameObject);
}
}
public void Scroll()
{
var items = Resolution.GetComponentsInChildren<Toggle>();
if (items.Length > 0)
{
var selectedItem = items[currentResolutionIndex].GetComponent<RectTransform>();
Scroll(selectedItem);
}
}
public void Scroll(Toggle toggle)
{
var selectedItem = toggle.GetComponent<RectTransform>();
Scroll(selectedItem);
}
private void Scroll(RectTransform selectedItem)
{
var scrollRect = Resolution.GetComponentInChildren<ScrollRect>();
Canvas.ForceUpdateCanvases();
var contentPos = (Vector2)scrollRect.transform.InverseTransformPoint(scrollRect.content.position);
var childPos = (Vector2)scrollRect.transform.InverseTransformPoint(selectedItem.position);
childPos.y += selectedItem.rect.height * 2;
var endPos = contentPos - childPos;
endPos.x = scrollRect.content.anchoredPosition.x;
scrollRect.content.anchoredPosition = endPos;
}
}