LightToggler.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * Licensed under the Oculus SDK License Agreement (the "License");
  6. * you may not use the Oculus SDK except in compliance with the License,
  7. * which is provided at the time of installation or download, or which
  8. * otherwise accompanies this software in either electronic or hard copy form.
  9. *
  10. * You may obtain a copy of the License at
  11. *
  12. * https://developer.oculus.com/licenses/oculussdk/
  13. *
  14. * Unless required by applicable law or agreed to in writing, the Oculus SDK
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using System;
  21. using Meta.WitAi;
  22. using Meta.WitAi.Json;
  23. using UnityEngine;
  24. namespace Oculus.Voice.Demo.LightTraitsDemo
  25. {
  26. public class LightToggler : MonoBehaviour
  27. {
  28. public enum LightState
  29. {
  30. On,
  31. Off
  32. }
  33. private const string EMISSION_COLOR = "_EmissionColor";
  34. private const string EMISSION = "_EMISSION";
  35. private LightState _lightState;
  36. private Material _material;
  37. private Color _offColor = Color.black;
  38. private Color _onColor;
  39. // Start is called before the first frame update
  40. void Start()
  41. {
  42. _material = GetComponent<Renderer>().material;
  43. _onColor = _material.GetColor(EMISSION_COLOR);
  44. SetLightState((LightState.Off));
  45. }
  46. [MatchIntent("wit_change_state")]
  47. public void OnResponse(WitResponseNode commandResult)
  48. {
  49. var traitValue = commandResult.GetTraitValue("wit$on_off").Replace('o', 'O');
  50. SetLightState((LightState)Enum.Parse(typeof(LightState), traitValue));
  51. }
  52. public void SetLightState(LightState newState)
  53. {
  54. switch (newState)
  55. {
  56. case LightState.On:
  57. if (_lightState == LightState.On)
  58. break;
  59. _material.EnableKeyword(EMISSION);
  60. _material.SetColor(EMISSION_COLOR, _onColor);
  61. break;
  62. case LightState.Off:
  63. if (_lightState == LightState.Off)
  64. break;
  65. _material.DisableKeyword(EMISSION);
  66. _material.SetColor(EMISSION_COLOR, _offColor);
  67. break;
  68. }
  69. _lightState = newState;
  70. }
  71. }
  72. }