DesktopDuplication.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class DesktopDuplication : MonoBehaviour
  6. {
  7. public bool CreateDesktopDuplication(string display_device_name)
  8. {
  9. NDebug.Assert(this.class_pointer_ == IntPtr.Zero, "CreateDesktopDuplication二重に作成しようとしました。");
  10. if (!global::DesktopDuplication.class_pointer_cache_.TryGetValue(display_device_name, out this.class_pointer_))
  11. {
  12. this.class_pointer_ = DLLDesktopDuplication.CreateDesktopDuplication(display_device_name);
  13. if (this.class_pointer_ == IntPtr.Zero)
  14. {
  15. return false;
  16. }
  17. global::DesktopDuplication.class_pointer_cache_.Add(display_device_name, this.class_pointer_);
  18. }
  19. this.render_event_func_ptr_ = DLLDesktopDuplication.GetDesktopDuplicationRenderEventFunc();
  20. this.render_event_id_ = DLLDesktopDuplication.GetDesktopDuplicationRenderID(this.class_pointer_);
  21. this.routine_ = this.CallPluginAtEndOfFrames();
  22. NDebug.Assert(this.render_coroutine_ == null, "CreateDesktopDuplication Coroutine 二重に作成しようとしました。");
  23. this.render_coroutine_ = base.StartCoroutine(this.routine_);
  24. return true;
  25. }
  26. private void OnEnable()
  27. {
  28. if (this.routine_ != null && this.render_coroutine_ == null)
  29. {
  30. this.render_coroutine_ = base.StartCoroutine(this.routine_);
  31. }
  32. }
  33. private void OnDisable()
  34. {
  35. if (this.routine_ != null && this.render_coroutine_ != null)
  36. {
  37. base.StopCoroutine(this.render_coroutine_);
  38. this.render_coroutine_ = null;
  39. }
  40. }
  41. private void OnDestroy()
  42. {
  43. if (this.routine_ != null && this.render_coroutine_ != null)
  44. {
  45. base.StopCoroutine(this.render_coroutine_);
  46. this.render_coroutine_ = null;
  47. }
  48. if (this.is_valid)
  49. {
  50. }
  51. this.class_pointer_ = IntPtr.Zero;
  52. }
  53. public void OnApplicationQuit()
  54. {
  55. foreach (KeyValuePair<string, IntPtr> keyValuePair in global::DesktopDuplication.class_pointer_cache_)
  56. {
  57. if (keyValuePair.Value != IntPtr.Zero)
  58. {
  59. DLLDesktopDuplication.DeleteDesktopDuplication(keyValuePair.Value);
  60. }
  61. }
  62. global::DesktopDuplication.class_pointer_cache_.Clear();
  63. this.class_pointer_ = IntPtr.Zero;
  64. }
  65. public Texture2D render_texture
  66. {
  67. get
  68. {
  69. return this.render_tex_;
  70. }
  71. set
  72. {
  73. if (!this.is_valid)
  74. {
  75. return;
  76. }
  77. this.render_tex_ = value;
  78. if (this.render_tex_ != null)
  79. {
  80. DLLDesktopDuplication.SetDesktopDuplicationDrawTex(this.class_pointer_, this.render_tex_.GetNativeTexturePtr());
  81. }
  82. }
  83. }
  84. public int width
  85. {
  86. get
  87. {
  88. return (!this.is_valid) ? 0 : DLLDesktopDuplication.GetDesktopDuplicationRenderWidth(this.class_pointer_);
  89. }
  90. }
  91. public int height
  92. {
  93. get
  94. {
  95. return (!this.is_valid) ? 0 : DLLDesktopDuplication.GetDesktopDuplicationRenderHeight(this.class_pointer_);
  96. }
  97. }
  98. public bool is_mouse_pointer_visible
  99. {
  100. get
  101. {
  102. return this.is_valid && DLLDesktopDuplication.GetDesktopDuplicationIsMousePointerVisible(this.class_pointer_) != 0;
  103. }
  104. }
  105. public int mouse_pointer_x
  106. {
  107. get
  108. {
  109. return (!this.is_valid) ? 0 : DLLDesktopDuplication.GetDesktopDuplicationGetMousePointerX(this.class_pointer_);
  110. }
  111. }
  112. public int mouse_pointer_y
  113. {
  114. get
  115. {
  116. return (!this.is_valid) ? 0 : DLLDesktopDuplication.GetDesktopDuplicationGetMousePointerY(this.class_pointer_);
  117. }
  118. }
  119. public bool is_valid
  120. {
  121. get
  122. {
  123. return this.class_pointer_ != IntPtr.Zero;
  124. }
  125. }
  126. private IEnumerator CallPluginAtEndOfFrames()
  127. {
  128. for (;;)
  129. {
  130. yield return new WaitForEndOfFrame();
  131. if (this.is_valid && this.render_texture != null)
  132. {
  133. GL.IssuePluginEvent(this.render_event_func_ptr_, this.render_event_id_);
  134. }
  135. }
  136. yield break;
  137. }
  138. private static Dictionary<string, IntPtr> class_pointer_cache_ = new Dictionary<string, IntPtr>();
  139. private IntPtr class_pointer_ = IntPtr.Zero;
  140. private Texture2D render_tex_;
  141. private int render_event_id_ = -1;
  142. private IEnumerator routine_;
  143. private Coroutine render_coroutine_;
  144. private IntPtr render_event_func_ptr_;
  145. }