2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
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.

93 lines
2.9 KiB

5 years ago
  1. //========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
  2. using HTC.UnityPlugin.VRModuleManagement;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using UnityEngine;
  8. namespace HTC.UnityPlugin.Vive
  9. {
  10. /// <summary>
  11. /// This componenet hooks up custom VR camera required component
  12. /// </summary>
  13. [AddComponentMenu("VIU/Hooks/VR Camera Hook", 10)]
  14. public class VRCameraHook : MonoBehaviour
  15. {
  16. [AttributeUsage(AttributeTargets.Class)]
  17. public class CreatorPriorityAttirbute : Attribute
  18. {
  19. public int priority { get; set; }
  20. public CreatorPriorityAttirbute(int priority = 0) { this.priority = priority; }
  21. }
  22. public abstract class CameraCreator
  23. {
  24. public abstract bool shouldActive { get; }
  25. public abstract void CreateCamera(VRCameraHook hook);
  26. }
  27. private static readonly Type[] s_creatorTypes;
  28. private CameraCreator[] m_creators;
  29. static VRCameraHook()
  30. {
  31. try
  32. {
  33. var creatorTypes = new List<Type>();
  34. foreach (var type in Assembly.GetAssembly(typeof(CameraCreator)).GetTypes().Where(t => t.IsClass && !t.IsAbstract && t.IsSubclassOf(typeof(CameraCreator))))
  35. {
  36. creatorTypes.Add(type);
  37. }
  38. s_creatorTypes = creatorTypes.OrderBy(t =>
  39. {
  40. foreach (var at in t.GetCustomAttributes(typeof(CreatorPriorityAttirbute), true))
  41. {
  42. return ((CreatorPriorityAttirbute)at).priority;
  43. }
  44. return 0;
  45. }).ToArray();
  46. }
  47. catch (Exception e)
  48. {
  49. s_creatorTypes = new Type[0];
  50. Debug.LogError(e);
  51. }
  52. }
  53. private void Awake()
  54. {
  55. m_creators = new CameraCreator[s_creatorTypes.Length];
  56. for (int i = s_creatorTypes.Length - 1; i >= 0; --i)
  57. {
  58. m_creators[i] = (CameraCreator)Activator.CreateInstance(s_creatorTypes[i]);
  59. }
  60. if (VRModule.activeModule == VRModuleActiveEnum.Uninitialized)
  61. {
  62. VRModule.onActiveModuleChanged += OnModuleActivated;
  63. }
  64. else
  65. {
  66. OnModuleActivated(VRModule.activeModule);
  67. }
  68. }
  69. private void OnModuleActivated(VRModuleActiveEnum activatedModule)
  70. {
  71. foreach (var creator in m_creators)
  72. {
  73. if (creator.shouldActive)
  74. {
  75. creator.CreateCamera(this);
  76. break;
  77. }
  78. }
  79. if (activatedModule != VRModuleActiveEnum.Uninitialized)
  80. {
  81. VRModule.onActiveModuleChanged -= OnModuleActivated;
  82. }
  83. }
  84. }
  85. }