forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenResolution.cs
146 lines (116 loc) · 4.97 KB
/
ScreenResolution.cs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
namespace UnityEngine
{
[Serializable]
public readonly partial struct ScreenResolution : IEquatableReadOnlyStruct<ScreenResolution>, ISerializable
{
public readonly int Width;
public readonly int Height;
public int this[int index]
{
get
{
if (index == 0) return this.Width;
if (index == 1) return this.Height;
throw new IndexOutOfRangeException();
}
}
public ScreenResolution(int width, int height)
{
this.Width = width;
this.Height = height;
}
public void Deconstruct(out float width, out float height)
{
width = this.Width;
height = this.Height;
}
public ScreenResolution With(int? Width = null, int? Height = null)
=> new ScreenResolution(
Width ?? this.Width,
Height ?? this.Height
);
/// <summary>
/// Returns a new resolution where <see cref="Width"/> and <see cref="Height"/> are swapped
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ScreenResolution ChangeOrientation()
=> new ScreenResolution(this.Height, this.Width);
public ScreenResolution ToLandscape()
=> IsLandscape() ? this : ChangeOrientation();
public ScreenResolution ToPortrait()
=> IsPortrait() ? this : ChangeOrientation();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsLandscape()
=> this.Width > this.Height;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsPortrait()
=> this.Height > this.Width;
public override bool Equals(object obj)
=> obj is ScreenResolution other &&
this.Width == other.Width &&
this.Height == other.Height;
public bool Equals(ScreenResolution other)
=> this.Width == other.Width && this.Height == other.Height;
public bool Equals(in ScreenResolution other)
=> this.Width == other.Width && this.Height == other.Height;
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.Width, this.Height);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = 859600377;
hashCode = hashCode * -1521134295 + this.Width.GetHashCode();
hashCode = hashCode * -1521134295 + this.Height.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public override string ToString()
=> $"{this.Width} × {this.Height}";
public void Apply(bool fullscreen = false)
{
var mode = fullscreen ? FullScreenMode.FullScreenWindow : FullScreenMode.Windowed;
#if !UNITY_EDITOR
Screen.SetResolution(this.Width, this.Height, mode);
#endif
}
public void Apply(FullScreenMode mode)
{
#if !UNITY_EDITOR
Screen.SetResolution(this.Width, this.Height, mode);
#endif
}
private ScreenResolution(SerializationInfo info, StreamingContext context)
{
this.Width = info.GetInt32OrDefault(nameof(this.Width));
this.Height = info.GetInt32OrDefault(nameof(this.Height));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(this.Width), this.Width);
info.AddValue(nameof(this.Height), this.Height);
}
public static implicit operator ScreenResolution(Resolution value)
=> new ScreenResolution(value.width, value.height);
public static implicit operator SizeInt(in ScreenResolution value)
=> new SizeInt(value.Width, value.Height);
public static implicit operator ScreenResolution(in SizeInt value)
=> new ScreenResolution(value.Width, value.Height);
public static implicit operator ScreenResolution(Vector2 value)
=> new ScreenResolution((int)value.x, (int)value.y);
public static implicit operator ScreenResolution(Vector2Int value)
=> new ScreenResolution(value.x, value.y);
public static implicit operator Vector2(in ScreenResolution value)
=> new Vector2(value.Width, value.Height);
public static implicit operator Vector2Int(in ScreenResolution value)
=> new Vector2Int(value.Width, value.Height);
public static bool operator ==(in ScreenResolution lhs, in ScreenResolution rhs)
=> lhs.Width == rhs.Width && lhs.Height == rhs.Height;
public static bool operator !=(in ScreenResolution lhs, in ScreenResolution rhs)
=> lhs.Width != rhs.Width || lhs.Height != rhs.Height;
}
}