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.

79 lines
1.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. namespace IllusionInjector
  7. {
  8. public class PluginComponent : MonoBehaviour
  9. {
  10. private CompositePlugin plugins;
  11. private bool quitting = false;
  12. public static PluginComponent Create()
  13. {
  14. return new GameObject("IPA_PluginManager").AddComponent<PluginComponent>();
  15. }
  16. void Awake()
  17. {
  18. DontDestroyOnLoad(gameObject);
  19. plugins = new CompositePlugin(PluginManager.Plugins);
  20. plugins.OnApplicationStart();
  21. SceneManager.activeSceneChanged += OnActiveSceneChanged;
  22. SceneManager.sceneLoaded += OnSceneLoaded;
  23. SceneManager.sceneUnloaded += OnSceneUnloaded;
  24. }
  25. void Update()
  26. {
  27. plugins.OnUpdate();
  28. }
  29. void LateUpdate()
  30. {
  31. plugins.OnLateUpdate();
  32. }
  33. void FixedUpdate()
  34. {
  35. plugins.OnFixedUpdate();
  36. }
  37. void OnDestroy()
  38. {
  39. if (!quitting)
  40. {
  41. Create();
  42. }
  43. }
  44. void OnApplicationQuit()
  45. {
  46. SceneManager.activeSceneChanged += OnActiveSceneChanged;
  47. SceneManager.sceneLoaded += OnSceneLoaded;
  48. SceneManager.sceneUnloaded += OnSceneUnloaded;
  49. plugins.OnApplicationQuit();
  50. quitting = true;
  51. }
  52. void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  53. {
  54. plugins.OnSceneLoaded(scene, sceneMode);
  55. }
  56. private void OnSceneUnloaded(Scene scene) {
  57. plugins.OnSceneUnloaded(scene);
  58. }
  59. private void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  60. plugins.OnActiveSceneChanged(prevScene, nextScene);
  61. }
  62. }
  63. }