Tanoda
Hash.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections;
11using System.Collections.Generic;
12using UnityEngine;
13
14namespace Leap.Unity {
15
16 [Serializable]
17 public struct Hash : IEnumerable, IEquatable<Hash> {
18 private int _hash;
19
20 public Hash(int hash) {
21 _hash = hash;
22 }
23
24 public void Add<T>(T t) {
25 int code = t == null ? 0x2692d0f9 : t.GetHashCode();
26 _hash ^= code + 0x3e3779b9 + (_hash << 6) + (_hash >> 2);
27 }
28
29 public void AddRange<T>(List<T> sequence) {
30 for (int i = 0; i < sequence.Count; i++) {
31 Add(sequence[i]);
32 }
33 }
34
43 private static List<Behaviour> _behaviourCache = new List<Behaviour>();
44 public static Hash GetHierarchyHash(Transform root) {
45 var hash = Hash.GetDataHash(root);
46
47 int childCount = root.childCount;
48 for (int i = 0; i < childCount; i++) {
49 hash.Add(GetHierarchyHash(root.GetChild(i)));
50 }
51
52 root.GetComponents(_behaviourCache);
53 for (int i = 0; i < _behaviourCache.Count; i++) {
54 var behaviour = _behaviourCache[i];
55
56 //A behaviour returned from GetComponents can be null if it is an invalid
57 //script object or due to a compile error >.>
58 if (behaviour != null) {
59 hash.Add(behaviour.enabled);
60 }
61 }
62
63 return hash;
64 }
65
73 public static Hash GetDataHash(Transform transform) {
74 var hash = new Hash() {
75 transform,
76 transform.gameObject.activeSelf,
77 transform.localPosition,
78 transform.localRotation,
79 transform.localScale
80 };
81
82 if (transform is RectTransform) {
83 RectTransform rectTransform = transform as RectTransform;
84 hash.Add(rectTransform.rect);
85 }
86
87 return hash;
88 }
89
90 public IEnumerator GetEnumerator() {
91 throw new NotImplementedException();
92 }
93
94 public override int GetHashCode() {
95 return _hash;
96 }
97
98 public override bool Equals(object obj) {
99 if (!(obj is Hash)) {
100 return false;
101 }
102 Hash hash = (Hash)obj;
103 return hash._hash == _hash;
104 }
105
106 public bool Equals(Hash other) {
107 return _hash == other._hash;
108 }
109
110 public static implicit operator Hash(int hash) {
111 return new Hash(hash);
112 }
113
114 public static implicit operator int(Hash hash) {
115 return hash._hash;
116 }
117 }
118}
static Hash GetHierarchyHash(Transform root)
Definition: Hash.cs:44
static Hash GetDataHash(Transform transform)
Returns a hash of the Transform in addition to it's data. Changing either the position,...
Definition: Hash.cs:73
void Add< T >(T t)
Definition: Hash.cs:24
bool Equals(Hash other)
Definition: Hash.cs:106
void AddRange< T >(List< T > sequence)
Definition: Hash.cs:29
override bool Equals(object obj)
Definition: Hash.cs:98
IEnumerator GetEnumerator()
Definition: Hash.cs:90
override int GetHashCode()
Definition: Hash.cs:94
Hash(int hash)
Definition: Hash.cs:20