CustomVirtualControlerUI.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. namespace NRKernal.NRExamples
  12. {
  13. /// <summary> A custom virtual controler user interface. </summary>
  14. [HelpURL("https://developer.nreal.ai/develop/unity/customize-phone-controller")]
  15. public class CustomVirtualControlerUI : MonoBehaviour
  16. {
  17. /// <summary> The show control. </summary>
  18. public Button showBtn;
  19. /// <summary> The hide control. </summary>
  20. public Button hideBtn;
  21. /// <summary> The base controller panel. </summary>
  22. public GameObject baseControllerPanel;
  23. /// <summary> The color btns. </summary>
  24. public Button[] colorBtns;
  25. /// <summary> The reset control. </summary>
  26. public Button resetBtn;
  27. /// <summary> The scale slider. </summary>
  28. public Slider scaleSlider;
  29. /// <summary> The model control. </summary>
  30. private TargetModelDisplayCtrl m_ModelCtrl;
  31. /// <summary> Starts this object. </summary>
  32. private void Start()
  33. {
  34. Init();
  35. }
  36. /// <summary> Initializes this object. </summary>
  37. private void Init()
  38. {
  39. m_ModelCtrl = FindObjectOfType<TargetModelDisplayCtrl>();
  40. for (int i = 0; i < colorBtns.Length; i++)
  41. {
  42. int k = i;
  43. colorBtns[i].onClick.AddListener(() => { OnColorButtonClick(k); });
  44. }
  45. showBtn.onClick.AddListener(() => { SetBaseControllerEnabled(true); });
  46. hideBtn.onClick.AddListener(() => { SetBaseControllerEnabled(false); });
  47. resetBtn.onClick.AddListener(OnResetButtonClick);
  48. scaleSlider.onValueChanged.AddListener(OnScaleSliderValueChanged);
  49. }
  50. /// <summary> Executes the 'color button click' action. </summary>
  51. /// <param name="index"> Zero-based index of the.</param>
  52. private void OnColorButtonClick(int index)
  53. {
  54. m_ModelCtrl.ChangeModelColor(colorBtns[index].image.color);
  55. }
  56. /// <summary> Executes the 'scale slider value changed' action. </summary>
  57. /// <param name="val"> The value.</param>
  58. private void OnScaleSliderValueChanged(float val)
  59. {
  60. m_ModelCtrl.ChangeModelScale(val);
  61. }
  62. /// <summary> Executes the 'reset button click' action. </summary>
  63. private void OnResetButtonClick()
  64. {
  65. m_ModelCtrl.ResetModel();
  66. scaleSlider.value = 0f;
  67. }
  68. /// <summary> Sets base controller enabled. </summary>
  69. /// <param name="isEnabled"> True if is enabled, false if not.</param>
  70. private void SetBaseControllerEnabled(bool isEnabled)
  71. {
  72. baseControllerPanel.SetActive(isEnabled);
  73. }
  74. }
  75. }