-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXRayTarget.cs
94 lines (75 loc) · 2.31 KB
/
XRayTarget.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CXRayTarget : MonoBehaviour
{
public Material alphaMaterial;
private Dictionary<MeshRenderer, Material[]> _hiddenObjects;
private MeshRenderer[] _renderers;
private bool _hidden;
private HashSet<GameObject> _hiders;
private HashSet<GameObject> _toBeShown;
private void Awake()
{
_hiddenObjects = new Dictionary<MeshRenderer, Material[]>();
_hidden = false;
_hiders = new HashSet<GameObject>();
_toBeShown = new HashSet<GameObject>();
}
private void Start()
{
_renderers = GetComponentsInChildren< MeshRenderer >();
foreach (var childRenderer in _renderers)
{
_hiddenObjects.Add(childRenderer, childRenderer.materials);
}
}
public void Hide(GameObject hider)
{
if (_toBeShown.Contains(hider))
{
_toBeShown.Remove(hider);
}
if(_hiders.Contains(hider))
return;
_hiders.Add(hider);
if(_hidden)
return;
foreach (var childRenderer in _renderers)
{
var newMaterials = new Material[childRenderer.materials.Length];
for(var i=0; i<newMaterials.Length; i++)
{
newMaterials[i] = alphaMaterial;
}
childRenderer.materials = newMaterials;
}
_hidden = true;
}
private IEnumerator DelayedShow(GameObject reveler)
{
yield return new WaitForSeconds(2.0f);
if (!_hidden || !_toBeShown.Contains(reveler)) yield break;
_toBeShown.Remove(reveler);
if (!_hiders.Contains(reveler)) yield break;
_hiders.Remove(reveler);
foreach (var childRenderer in _renderers)
{
Material[] restoredMaterials;
_hiddenObjects.TryGetValue(childRenderer, out restoredMaterials);
if (childRenderer != null)
{
childRenderer.materials = restoredMaterials;
}
}
if (_hiders.Count == 0)
_hidden = false;
}
public void Show(GameObject reveler)
{
if (_toBeShown.Contains(reveler))
return;
_toBeShown.Add(reveler);
StartCoroutine(DelayedShow(reveler));
}
}