-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItemLockCenter.cs
147 lines (137 loc) · 4.69 KB
/
ItemLockCenter.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
public class ItemLockCenter : UdonSharpBehaviour
{
[SerializeField] private String[] usernames;
[SerializeField] private GameObject[] targetObjects;
[Header("Modeに関する詳しい説明は、Githubおよび商品ページにあります。")]
[Header("Specific Inforamtion is on the github and booth page")]
[Header("Github: https://github.com/TamakiRuri/SimpleItemLock")]
[Header("Booth: https://saphir.booth.pm/items/6375850")]
[Header(" ")]
[SerializeField] private int actionMode = 0;
[SerializeField] private bool allowInstanceOwner = false;
[SerializeField] private bool wallMode = false;
private bool shouldOn = false;
void Start()
{
shouldOn = UserCheck();
// (shouldOn && !wallMode) || (!shouldOn && wallMode)
// in non wall mode, output == shouldOn
// in wall mode, output == !shouldOn
foreach (GameObject targetObject in targetObjects)
{
ScriptAction(targetObject, actionMode, (shouldOn && !wallMode) || (!shouldOn && wallMode));
}
}
private bool UserCheck()
{
String localPlayer = Networking.LocalPlayer.displayName;
if (Networking.LocalPlayer.isInstanceOwner && allowInstanceOwner)
{
return true;
}
else
{
for (int i = 0; i < usernames.Length; i++)
{
if (localPlayer == usernames[i])
{
return true;
}
}
}
return false;
}
private void ScriptAction(GameObject targetObject, int mode, bool targetState)
{
switch (mode)
{
case 0:
targetObject.SetActive(targetState);
break;
case 1:
Collider targetCollider = targetObject.GetComponent<Collider>();
if (targetCollider == null)
{
Debug.LogError("Item Lock: This object is in Action Mode 1 but the Collider can't be detected");
Debug.LogError("Item Lock: このオブジェクトの動作モードがAction Mode 1ですがコライダーを取得できません");
}
else
targetCollider.enabled = targetState;
break;
case 2:
ColliderRecursive(targetObject, targetState);
break;
case 3:
ColliderRecursive(targetObject, targetState);
MeshRendererRecursive(targetObject, targetState);
SkinnedMeshRendererRecursive(targetObject, targetState);
break;
default:
Debug.LogError("Item Lock: Action Mode Index Out Of Bound.");
Debug.LogError("Item Lock: Action Modeの入力にエラーを検出しました。");
break;
}
}
private void SkinnedMeshRendererRecursive(GameObject targetObject, bool targetState)
{
SkinnedMeshRenderer[] t_meshRenderers = targetObject.GetComponentsInChildren<SkinnedMeshRenderer>();
if (t_meshRenderers.Length != 0)
{
foreach (SkinnedMeshRenderer l_meshRenderer in t_meshRenderers)
{
l_meshRenderer.enabled = targetState;
}
}
else
{
Debug.LogError("Item Lock: No Skinned Mesh Renderer Found");
}
}
private void MeshRendererRecursive(GameObject targetObject, bool targetState)
{
MeshRenderer[] t_meshRenderers = targetObject.GetComponentsInChildren<MeshRenderer>();
if (t_meshRenderers.Length != 0)
{
foreach (MeshRenderer l_meshRenderer in t_meshRenderers)
{
l_meshRenderer.enabled = targetState;
}
}
else
{
Debug.LogError("Item Lock: No Mesh Renderer Found");
}
}
private void ColliderRecursive(GameObject targetObject, bool targetState)
{
Collider[] t_colliders = targetObject.GetComponentsInChildren<Collider>();
if (t_colliders.Length != 0)
{
foreach (Collider l_collider in t_colliders)
{
l_collider.enabled = targetState;
}
}
else
{
Debug.LogError("Item Lock: No colliders found");
}
}
#if UNITY_EDITOR && !COMPILER_UDONSHARP
public void ImportUsernames(String[] importedUsernames)
{
usernames = importedUsernames;
Debug.Log("Item Lock Center: Username Imported");
}
public String[] ExportUsernames()
{
return usernames;
}
#endif
}