-
Notifications
You must be signed in to change notification settings - Fork 0
/
CXRayView.cs
63 lines (49 loc) · 1.64 KB
/
CXRayView.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
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class CXRayView : MonoBehaviour {
public Material alphaMaterial;
public LayerMask blockLayer;
public string blockTag;
private Dictionary<GameObject, Material> hiddenObjects;
private void Awake()
{
hiddenObjects = new Dictionary<GameObject, Material>();
}
private void LateUpdate()
{
var checkDir = PlayerController.ControlledCharacter.transform.position - transform.position;
var checkDistance = checkDir.magnitude;
checkDir.Normalize();
var blockers = Physics.RaycastAll(transform.position, checkDir, checkDistance, blockLayer);
if(blockers.Length > 0)
{
foreach (var t in blockers)
{
var hitObject = t.transform.gameObject;
if (hitObject.tag != blockTag)
continue;
if (hiddenObjects.ContainsKey(hitObject))
continue;
var renderer = hitObject.GetComponentInChildren<Renderer>();
if (!renderer) continue;
var oldMaterial = renderer.sharedMaterial;
hiddenObjects.Add(hitObject, oldMaterial);
renderer.material = alphaMaterial;
renderer.material.mainTexture = oldMaterial.mainTexture;
}
}
var keys = new GameObject[hiddenObjects.Keys.Count];
hiddenObjects.Keys.CopyTo(keys, 0);
foreach (var t in keys)
{
var bFound = blockers.Select(t1 => t1.transform.gameObject).Any(hitObject => hitObject == t);
if (bFound) continue;
Material restoredMaterial;
hiddenObjects.TryGetValue(t, out restoredMaterial);
var renderer = t.GetComponentInChildren<Renderer>();
renderer.material = restoredMaterial;
hiddenObjects.Remove(t);
}
}
}