Hash.cs
3.58 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/******************************************************************************
* 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;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Leap.Unity {
[Serializable]
public struct Hash : IEnumerable, IEquatable<Hash> {
private int _hash;
public Hash(int hash) {
_hash = hash;
}
public void Add<T>(T t) {
int code = t == null ? 0x2692d0f9 : t.GetHashCode();
_hash ^= code + 0x3e3779b9 + (_hash << 6) + (_hash >> 2);
}
public void AddRange<T>(List<T> sequence) {
for (int i = 0; i < sequence.Count; i++) {
Add(sequence[i]);
}
}
/// <summary>
/// Returns a hash code of the entire transform hierarchy, changing
/// any of the following should result in a different hash code:
/// - position/rotation/scale of any transform
/// - creation/deletion/reordering of any transform
/// - turning a transform into a rect transform or back
/// - enabling or disabling ANY behavior
/// </summary>
private static List<Behaviour> _behaviourCache = new List<Behaviour>();
public static Hash GetHierarchyHash(Transform root) {
var hash = Hash.GetDataHash(root);
int childCount = root.childCount;
for (int i = 0; i < childCount; i++) {
hash.Add(GetHierarchyHash(root.GetChild(i)));
}
root.GetComponents(_behaviourCache);
for (int i = 0; i < _behaviourCache.Count; i++) {
var behaviour = _behaviourCache[i];
//A behaviour returned from GetComponents can be null if it is an invalid
//script object or due to a compile error >.>
if (behaviour != null) {
hash.Add(behaviour.enabled);
}
}
return hash;
}
/// <summary>
/// Returns a hash of the Transform in addition to it's data.
/// Changing either the position, rotation, or scale of a
/// transform will result in a different hash. Two transforms
/// with the same position rotation and scale will not have
/// the same hash!
/// </summary>
public static Hash GetDataHash(Transform transform) {
var hash = new Hash() {
transform,
transform.gameObject.activeSelf,
transform.localPosition,
transform.localRotation,
transform.localScale
};
if (transform is RectTransform) {
RectTransform rectTransform = transform as RectTransform;
hash.Add(rectTransform.rect);
}
return hash;
}
public IEnumerator GetEnumerator() {
throw new NotImplementedException();
}
public override int GetHashCode() {
return _hash;
}
public override bool Equals(object obj) {
if (!(obj is Hash)) {
return false;
}
Hash hash = (Hash)obj;
return hash._hash == _hash;
}
public bool Equals(Hash other) {
return _hash == other._hash;
}
public static implicit operator Hash(int hash) {
return new Hash(hash);
}
public static implicit operator int(Hash hash) {
return hash._hash;
}
}
}