ControllerProviderFactory.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. namespace NRKernal
  10. {
  11. using System;
  12. using System.Collections.Generic;
  13. /// <summary> A controller provider factory. </summary>
  14. internal static class ControllerProviderFactory
  15. {
  16. private static Dictionary<Type, ControllerProviderBase> m_ControllerProviderDict = new Dictionary<Type, ControllerProviderBase>();
  17. /// <summary> Type of the android controller provider. </summary>
  18. public static Type controllerProviderType
  19. {
  20. get
  21. {
  22. #if UNITY_EDITOR
  23. return typeof(EditorControllerProvider);
  24. #else
  25. return typeof(NRControllerProvider);
  26. #endif
  27. }
  28. }
  29. /// <summary> Creates controller provider. </summary>
  30. /// <param name="states"> The states.</param>
  31. /// <returns> The new controller provider. </returns>
  32. public static ControllerProviderBase CreateControllerProvider(ControllerState[] states)
  33. {
  34. ControllerProviderBase provider = GetOrCreateControllerProvider(controllerProviderType, states);
  35. return provider;
  36. }
  37. /// <summary> Creates controller provider. </summary>
  38. /// <param name="providerType"> Type of the provider.</param>
  39. /// <param name="states"> The states.</param>
  40. /// <returns> The new controller provider. </returns>
  41. internal static ControllerProviderBase GetOrCreateControllerProvider(Type providerType, ControllerState[] states)
  42. {
  43. if (providerType != null)
  44. {
  45. if (m_ControllerProviderDict.ContainsKey(providerType))
  46. {
  47. return m_ControllerProviderDict[providerType];
  48. }
  49. object parserObj = Activator.CreateInstance(providerType, new object[] { states });
  50. if (parserObj is ControllerProviderBase)
  51. {
  52. var controllerProvider = parserObj as ControllerProviderBase;
  53. m_ControllerProviderDict.Add(providerType, controllerProvider);
  54. return controllerProvider;
  55. }
  56. }
  57. return null;
  58. }
  59. }
  60. }