Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.6 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. using System.Collections;
  7. public class UI_Interactions : MonoBehaviour
  8. {
  9. private const int EXISTING_CANVAS_COUNT = 4;
  10. public void Button_Red()
  11. {
  12. VRTK_Logger.Info("Red Button Clicked");
  13. }
  14. public void Button_Pink()
  15. {
  16. VRTK_Logger.Info("Pink Button Clicked");
  17. }
  18. public void Toggle(bool state)
  19. {
  20. VRTK_Logger.Info("The toggle state is " + (state ? "on" : "off"));
  21. }
  22. public void Dropdown(int value)
  23. {
  24. VRTK_Logger.Info("Dropdown option selected was ID " + value);
  25. }
  26. public void SetDropText(BaseEventData data)
  27. {
  28. var pointerData = data as PointerEventData;
  29. var textObject = GameObject.Find("ActionText");
  30. if (textObject)
  31. {
  32. var text = textObject.GetComponent<Text>();
  33. text.text = pointerData.pointerDrag.name + " Dropped On " + pointerData.pointerEnter.name;
  34. }
  35. }
  36. public void CreateCanvas()
  37. {
  38. StartCoroutine(CreateCanvasOnNextFrame());
  39. }
  40. private IEnumerator CreateCanvasOnNextFrame()
  41. {
  42. yield return null;
  43. var canvasCount = FindObjectsOfType<Canvas>().Length - EXISTING_CANVAS_COUNT;
  44. var newCanvasGO = new GameObject("TempCanvas");
  45. newCanvasGO.layer = 5;
  46. var canvas = newCanvasGO.AddComponent<Canvas>();
  47. var canvasRT = canvas.GetComponent<RectTransform>();
  48. canvasRT.position = new Vector3(-4f, 2f, 3f + canvasCount);
  49. canvasRT.sizeDelta = new Vector2(300f, 400f);
  50. canvasRT.localScale = new Vector3(0.005f, 0.005f, 0.005f);
  51. canvasRT.eulerAngles = new Vector3(0f, 270f, 0f);
  52. var newButtonGO = new GameObject("TempButton", typeof(RectTransform));
  53. newButtonGO.transform.SetParent(newCanvasGO.transform);
  54. newButtonGO.layer = 5;
  55. var buttonRT = newButtonGO.GetComponent<RectTransform>();
  56. buttonRT.position = new Vector3(0f, 0f, 0f);
  57. buttonRT.anchoredPosition = new Vector3(0f, 0f, 0f);
  58. buttonRT.localPosition = new Vector3(0f, 0f, 0f);
  59. buttonRT.sizeDelta = new Vector2(180f, 60f);
  60. buttonRT.localScale = new Vector3(1f, 1f, 1f);
  61. buttonRT.localEulerAngles = new Vector3(0f, 0f, 0f);
  62. newButtonGO.AddComponent<Image>();
  63. var canvasButton = newButtonGO.AddComponent<Button>();
  64. var buttonColourBlock = canvasButton.colors;
  65. buttonColourBlock.highlightedColor = Color.red;
  66. canvasButton.colors = buttonColourBlock;
  67. var newTextGO = new GameObject("BtnText", typeof(RectTransform));
  68. newTextGO.transform.SetParent(newButtonGO.transform);
  69. newTextGO.layer = 5;
  70. var textRT = newTextGO.GetComponent<RectTransform>();
  71. textRT.position = new Vector3(0f, 0f, 0f);
  72. textRT.anchoredPosition = new Vector3(0f, 0f, 0f);
  73. textRT.localPosition = new Vector3(0f, 0f, 0f);
  74. textRT.sizeDelta = new Vector2(180f, 60f);
  75. textRT.localScale = new Vector3(1f, 1f, 1f);
  76. textRT.localEulerAngles = new Vector3(0f, 0f, 0f);
  77. var txt = newTextGO.AddComponent<Text>();
  78. txt.text = "New Button";
  79. txt.color = Color.black;
  80. txt.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
  81. newCanvasGO.AddComponent<VRTK_UICanvas>();
  82. }
  83. }
  84. }