OVRGridCube.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. public class OVRGridCube : MonoBehaviour
  6. {
  7. private void Update()
  8. {
  9. this.UpdateCubeGrid();
  10. }
  11. public void SetOVRCameraController(ref OVRCameraRig cameraController)
  12. {
  13. this.CameraController = cameraController;
  14. }
  15. private void UpdateCubeGrid()
  16. {
  17. if (Input.GetKeyDown(this.GridKey))
  18. {
  19. if (!this.CubeGridOn)
  20. {
  21. this.CubeGridOn = true;
  22. Debug.LogWarning("CubeGrid ON");
  23. if (this.CubeGrid != null)
  24. {
  25. this.CubeGrid.SetActive(true);
  26. }
  27. else
  28. {
  29. this.CreateCubeGrid();
  30. }
  31. }
  32. else
  33. {
  34. this.CubeGridOn = false;
  35. Debug.LogWarning("CubeGrid OFF");
  36. if (this.CubeGrid != null)
  37. {
  38. this.CubeGrid.SetActive(false);
  39. }
  40. }
  41. }
  42. if (this.CubeGrid != null)
  43. {
  44. this.CubeSwitchColor = !OVRManager.tracker.isPositionTracked;
  45. if (this.CubeSwitchColor != this.CubeSwitchColorOld)
  46. {
  47. this.CubeGridSwitchColor(this.CubeSwitchColor);
  48. }
  49. this.CubeSwitchColorOld = this.CubeSwitchColor;
  50. }
  51. }
  52. private void CreateCubeGrid()
  53. {
  54. Debug.LogWarning("Create CubeGrid");
  55. this.CubeGrid = new GameObject("CubeGrid");
  56. this.CubeGrid.layer = this.CameraController.gameObject.layer;
  57. for (int i = -this.gridSizeX; i <= this.gridSizeX; i++)
  58. {
  59. for (int j = -this.gridSizeY; j <= this.gridSizeY; j++)
  60. {
  61. for (int k = -this.gridSizeZ; k <= this.gridSizeZ; k++)
  62. {
  63. int num = 0;
  64. if ((i == 0 && j == 0) || (i == 0 && k == 0) || (j == 0 && k == 0))
  65. {
  66. if (i == 0 && j == 0 && k == 0)
  67. {
  68. num = 2;
  69. }
  70. else
  71. {
  72. num = 1;
  73. }
  74. }
  75. GameObject gameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
  76. BoxCollider component = gameObject.GetComponent<BoxCollider>();
  77. component.enabled = false;
  78. gameObject.layer = this.CameraController.gameObject.layer;
  79. Renderer component2 = gameObject.GetComponent<Renderer>();
  80. component2.shadowCastingMode = ShadowCastingMode.Off;
  81. component2.receiveShadows = false;
  82. if (num == 0)
  83. {
  84. component2.material.color = Color.red;
  85. }
  86. else if (num == 1)
  87. {
  88. component2.material.color = Color.white;
  89. }
  90. else
  91. {
  92. component2.material.color = Color.yellow;
  93. }
  94. gameObject.transform.position = new Vector3((float)i * this.gridScale, (float)j * this.gridScale, (float)k * this.gridScale);
  95. float num2 = 0.7f;
  96. if (num == 1)
  97. {
  98. num2 = 1f;
  99. }
  100. if (num == 2)
  101. {
  102. num2 = 2f;
  103. }
  104. gameObject.transform.localScale = new Vector3(this.cubeScale * num2, this.cubeScale * num2, this.cubeScale * num2);
  105. gameObject.transform.parent = this.CubeGrid.transform;
  106. }
  107. }
  108. }
  109. }
  110. private void CubeGridSwitchColor(bool CubeSwitchColor)
  111. {
  112. Color color = Color.red;
  113. if (CubeSwitchColor)
  114. {
  115. color = Color.blue;
  116. }
  117. IEnumerator enumerator = this.CubeGrid.transform.GetEnumerator();
  118. try
  119. {
  120. while (enumerator.MoveNext())
  121. {
  122. object obj = enumerator.Current;
  123. Transform transform = (Transform)obj;
  124. Material material = transform.GetComponent<Renderer>().material;
  125. if (material.color == Color.red || material.color == Color.blue)
  126. {
  127. material.color = color;
  128. }
  129. }
  130. }
  131. finally
  132. {
  133. IDisposable disposable;
  134. if ((disposable = (enumerator as IDisposable)) != null)
  135. {
  136. disposable.Dispose();
  137. }
  138. }
  139. }
  140. public KeyCode GridKey = KeyCode.G;
  141. private GameObject CubeGrid;
  142. private bool CubeGridOn;
  143. private bool CubeSwitchColorOld;
  144. private bool CubeSwitchColor;
  145. private int gridSizeX = 6;
  146. private int gridSizeY = 4;
  147. private int gridSizeZ = 6;
  148. private float gridScale = 0.3f;
  149. private float cubeScale = 0.03f;
  150. private OVRCameraRig CameraController;
  151. }