-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefabTester.cs
More file actions
104 lines (91 loc) · 2.8 KB
/
Copy pathPrefabTester.cs
File metadata and controls
104 lines (91 loc) · 2.8 KB
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
using TMPro;
using UnityEngine;
public class PrefabTester : MonoBehaviour
{
[SerializeField] GameObject[] prefabs;
GameObject currentInstance;
int currentIndex;
int PreviousIndex => currentIndex = (int) Mathf.Repeat(--currentIndex, prefabs.Length);
int NextIndex => currentIndex = (int) Mathf.Repeat(++currentIndex, prefabs.Length);
bool spawned;
Vector3 initialScale;
float scale = 1f;
float Scale
{
get => scale;
set => scale = Mathf.Clamp(value, 0f, float.MaxValue);
}
[SerializeField] TextMeshPro hud;
#if OCULUS
static bool DownNextButton => OVRInput.GetDown(OVRInput.RawButton.RThumbstickRight);
static bool DownPreviousButton => OVRInput.GetDown(OVRInput.RawButton.RThumbstickLeft);
static bool DownRespawnButton => OVRInput.GetDown(OVRInput.RawButton.A) || OVRInput.GetDown(OVRInput.RawButton.X);
static bool DownDeleteButton => OVRInput.GetDown(OVRInput.RawButton.B) || OVRInput.GetDown(OVRInput.RawButton.Y);
static bool DownIndexTrigger => OVRInput.GetDown(OVRInput.RawButton.LIndexTrigger) || OVRInput.GetDown(OVRInput.RawButton.RIndexTrigger);
static float ScaleChanger => OVRInput.Get(OVRInput.RawAxis2D.RThumbstick).y;
#else
static bool DownNextButton => false;
static bool DownPreviousButton => false;
static bool DownRespawnButton => false;
static bool DownDeleteButton => false;
static bool DownIndexTrigger => false;
static float ScaleChanger => 0f;
#endif
void Start()
{
SetTextOnHud(prefabs[currentIndex].name);
}
void Update()
{
if (DownNextButton)
{
SpawnPrefab(NextIndex);
}
else if (DownPreviousButton)
{
SpawnPrefab(PreviousIndex);
}
else if (DownRespawnButton || DownIndexTrigger)
{
SpawnPrefab(currentIndex);
}
else if (DownDeleteButton)
{
if (currentInstance) Destroy(currentInstance);
}
if (spawned)
{
PlayParticleSurely();
SetTextOnHud(prefabs[currentIndex].name);
spawned = false;
}
}
void FixedUpdate()
{
if (Mathf.Abs(ScaleChanger) > 0.2f)
{
Scale += ScaleChanger * 0.01f;
if (currentInstance) currentInstance.transform.localScale = initialScale * Scale;
}
}
void SpawnPrefab(int index)
{
if (prefabs.Length == 0) throw new UnityException("Set prefabs on the inspector.");
if (currentInstance) Destroy(currentInstance);
currentInstance = Instantiate(prefabs[index], transform.position, Quaternion.identity);
initialScale = currentInstance.transform.localScale;
currentInstance.transform.localScale = initialScale * Scale;
spawned = true;
}
void PlayParticleSurely()
{
var particle = currentInstance.GetComponent<ParticleSystem>();
if (particle && particle.main.playOnAwake == false) particle.Play();
}
void SetTextOnHud(string prefabName)
{
var index = (currentIndex + 1).ToString() + " / " + prefabs.Length.ToString();
var text = prefabName + "\n" + index;
if (hud) hud.text = text;
}
}