The Swartz-Manning’s first exhibit will provide a detailed history of Aaron Swartz Day. https://www.aaronswartzday.org/vr
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.

UdonManager.cs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using JetBrains.Annotations;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. using VRC.Udon.ClientBindings;
  7. using VRC.Udon.ClientBindings.Interfaces;
  8. using VRC.Udon.Common.Interfaces;
  9. namespace VRC.Udon
  10. {
  11. [AddComponentMenu("")]
  12. [ExecuteInEditMode]
  13. public class UdonManager : MonoBehaviour, IUdonClientInterface
  14. {
  15. private static UdonManager _instance;
  16. private static bool _isUdonEnabled = true;
  17. public UdonBehaviour currentlyExecuting;
  18. public static UdonManager Instance
  19. {
  20. get
  21. {
  22. if(_instance != null)
  23. {
  24. return _instance;
  25. }
  26. GameObject udonManagerGameObject = new GameObject("UdonManager");
  27. if(Application.isPlaying)
  28. {
  29. DontDestroyOnLoad(udonManagerGameObject);
  30. }
  31. _instance = udonManagerGameObject.AddComponent<UdonManager>();
  32. return _instance;
  33. }
  34. }
  35. private IUdonClientInterface _udonClientInterface;
  36. private IUdonClientInterface UdonClientInterface
  37. {
  38. get
  39. {
  40. if(_udonClientInterface != null)
  41. {
  42. return _udonClientInterface;
  43. }
  44. _udonClientInterface = new UdonClientInterface();
  45. #if !VRC_CLIENT
  46. _udonClientInterface.RegisterWrapperModule(new ExternVRCInstantiate());
  47. #endif
  48. return _udonClientInterface;
  49. }
  50. }
  51. private void OnEnable()
  52. {
  53. SceneManager.sceneLoaded += OnSceneLoaded;
  54. }
  55. private void OnDisable()
  56. {
  57. SceneManager.sceneLoaded -= OnSceneLoaded;
  58. }
  59. private static void OnSceneLoaded(Scene scene, LoadSceneMode _)
  60. {
  61. if(_isUdonEnabled)
  62. {
  63. return;
  64. }
  65. VRC.Core.Logger.LogWarning("Udon is disabled globally, Udon components will be removed from the scene.");
  66. GameObject[] sceneRootGameObjects = scene.GetRootGameObjects();
  67. List<UdonBehaviour> udonBehavioursWorkingList = new List<UdonBehaviour>();
  68. foreach(GameObject rootGameObject in sceneRootGameObjects)
  69. {
  70. rootGameObject.GetComponentsInChildren(true, udonBehavioursWorkingList);
  71. foreach(UdonBehaviour udonBehaviour in udonBehavioursWorkingList)
  72. {
  73. Destroy(udonBehaviour);
  74. }
  75. }
  76. }
  77. public void Awake()
  78. {
  79. if(_instance == null)
  80. {
  81. _instance = this;
  82. }
  83. DebugLogging = Application.isEditor;
  84. if(this == Instance)
  85. {
  86. return;
  87. }
  88. if(Application.isPlaying)
  89. {
  90. Destroy(this);
  91. }
  92. else
  93. {
  94. DestroyImmediate(this);
  95. }
  96. PrimitiveType[] primitiveTypes = (PrimitiveType[]) Enum.GetValues(typeof(PrimitiveType));
  97. foreach (PrimitiveType primitiveType in primitiveTypes)
  98. {
  99. GameObject go = GameObject.CreatePrimitive(primitiveType);
  100. Mesh primitiveMesh = go.GetComponent<MeshFilter>().sharedMesh;
  101. Destroy(go);
  102. Blacklist(primitiveMesh);
  103. }
  104. }
  105. [PublicAPI]
  106. public static void SetUdonEnabled(bool isEnabled)
  107. {
  108. _isUdonEnabled = isEnabled;
  109. }
  110. public IUdonVM ConstructUdonVM()
  111. {
  112. return !_isUdonEnabled ? null : UdonClientInterface.ConstructUdonVM();
  113. }
  114. public bool IsBlacklisted<T>(T objectToCheck)
  115. {
  116. return UdonClientInterface.IsBlacklisted(objectToCheck);
  117. }
  118. public void Blacklist(UnityEngine.Object objectToBlacklist)
  119. {
  120. UdonClientInterface.Blacklist(objectToBlacklist);
  121. }
  122. public void Blacklist(IEnumerable<UnityEngine.Object> objectsToBlacklist)
  123. {
  124. UdonClientInterface.Blacklist(objectsToBlacklist);
  125. }
  126. public bool IsBlacklisted(UnityEngine.Object objectToCheck)
  127. {
  128. return UdonClientInterface.IsBlacklisted(objectToCheck);
  129. }
  130. public void ClearBlacklist()
  131. {
  132. UdonClientInterface.ClearBlacklist();
  133. }
  134. public bool IsTypeSafe(Type type)
  135. {
  136. return UdonClientInterface.IsTypeSafe(type);
  137. }
  138. public IUdonWrapper GetWrapper()
  139. {
  140. return UdonClientInterface.GetWrapper();
  141. }
  142. public void RegisterWrapperModule(IUdonWrapperModule wrapperModule)
  143. {
  144. UdonClientInterface.RegisterWrapperModule(wrapperModule);
  145. }
  146. public bool DebugLogging
  147. {
  148. get => UdonClientInterface.DebugLogging;
  149. set => UdonClientInterface.DebugLogging = value;
  150. }
  151. }
  152. }