Tanoda
HandInventory.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using System.Linq;
4using UnityEngine;
5#if !UNITY_WEBGL
6using Valve.VR.InteractionSystem;
7#endif
8
9[RequireComponent(typeof(SkinnedMeshRenderer))]
10public class HandInventory : MonoBehaviour
11{
12 public GameObject root;
13 public static HandInventory instance;
14#if !UNITY_WEBGL
15 public Hand[] hands;
16
17 [SerializeField]
18 private int holdItemNum = 0;
19 [SerializeField]
20 private List<ThrowableCanDisable> items = new List<ThrowableCanDisable>();
21 [SerializeField]
22 private List<Transform> origParents = new List<Transform>();
23
24 private float handAlpha = 0f;
25
26 private void Start()
27 {
28 instance = this;
29 if (System.IO.File.Exists("noExtraHand.bin"))
30 {
31 root?.SetActive(false);
32 }
33 }
34
36 {
37 return items.Contains(tcd);
38 }
39
40 private void Update()
41 {
42 bool hasSomethingInHand = false;
43 foreach (var hand in hands)
44 {
45 if (!hand.gameObject.activeInHierarchy) continue;
46
47 var cao = hand.currentAttachedObject;
48 if (cao)
49 {
50 foreach (var item in items)
51 {
52 if (item.gameObject == cao)
53 {
54 var i = items.IndexOf(cao.GetComponent<ThrowableCanDisable>());
55 var ao = hand.attachedObjects.First(x => x.attachedObject == cao);
56 ao.originalParent = origParents[i].gameObject;
57 hand.attachedObjects[0] = ao;
58 origParents.RemoveAt(i);
59 items.RemoveAt(i);
60 holdItemNum--;
61 break;
62 }
63 }
64 hasSomethingInHand = true;
65 }
66 }
67 if (hasSomethingInHand)
68 {
69 if (handAlpha < 0.3f)
70 {
71 handAlpha += Time.deltaTime;
72 }
73 else if (holdItemNum > 0 && handAlpha < 1)
74 {
75 handAlpha += Time.deltaTime;
76 }
77 }
78 else if(holdItemNum == 0)
79 {
80 if (handAlpha > 0)
81 handAlpha -= Time.deltaTime;
82 }
83
84 GetComponent<SkinnedMeshRenderer>().material.color = new Color(1, 1, 1, handAlpha);
85 }
86 private void OnTriggerEnter(Collider other)
87 {
88 if (SavedUser.instance.isEditor)
89 return;
90#if !UNITY_WEBGL
91 var tcd = other.gameObject.GetComponentInParent<ThrowableCanDisable>();
92 if (tcd && tcd.IsAttachedToHand())
93 {
94 tcd.ForceDrop();
95 other.gameObject.GetComponentInParent<Rigidbody>().isKinematic = true;
96 other.gameObject.GetComponentInParent<Rigidbody>().useGravity = false;
97 holdItemNum++;
98 items.Add(tcd);
99 origParents.Add(tcd.transform.parent);
100 tcd.gameObject.transform.SetParent(transform, true);
101 }
102#endif
103
104 }
105#endif
106}
UnityEngine.Color Color
Definition: TestScript.cs:32
static HandInventory instance
GameObject root
bool IsInInventory(ThrowableCanDisable tcd)