forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleLayer.cs
93 lines (74 loc) · 2.96 KB
/
SingleLayer.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
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using System;
namespace UnityEngine
{
/// <summary>
/// An object you can use to represent a single Unity layer
/// as a dropdown in the inspector. Can be converted back and
/// forth between the integer representation Unity usually
/// uses in its own methods.
/// </summary>
[Serializable]
public struct SingleLayer : IEquatable<SingleLayer>
{
public int value;
public int mask
{
get
{
return 1 << this.value;
}
set
{
if (value == 0)
{
throw new ArgumentException("Single layer can only represent exactly one layer. The provided mask represents no layers (mask was zero).");
}
var newIndex = 0;
while ((value & 1) == 0)
{
value >>= 1;
newIndex++;
}
if (value != 1)
{
throw new ArgumentException("Single layer can only represent exactly one layer. The provided mask represents more than one layer.");
}
this.value = newIndex;
}
}
public SingleLayer(int value)
{
this.value = value;
}
public SingleLayer(string name)
{
this.value = LayerMask.NameToLayer(name);
}
public override int GetHashCode()
=> this.value.GetHashCode();
public override bool Equals(object obj)
=> obj is SingleLayer other && this.value == other.value;
public bool Equals(SingleLayer other)
=> this.value == other.value;
public override string ToString()
=> LayerMask.LayerToName(this.value);
public static implicit operator int(in SingleLayer value)
=> value.value;
public static implicit operator SingleLayer(int value)
=> new SingleLayer(value);
public static implicit operator SingleLayer(string name)
=> new SingleLayer(name);
public static bool operator ==(in SingleLayer lhs, in SingleLayer rhs)
=> lhs.value == rhs.value;
public static bool operator !=(in SingleLayer lhs, in SingleLayer rhs)
=> lhs.value != rhs.value;
}
}