InfraredCameraHelper.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. namespace InfraredManager
  6. {
  7. public class InfraredCameraHelper
  8. {
  9. #region 单例
  10. private static InfraredCameraHelper _Instance;
  11. public static InfraredCameraHelper GetInstance()
  12. {
  13. if (_Instance == null) _Instance = new();
  14. return _Instance;
  15. }
  16. #endregion
  17. #region 创建销毁
  18. private GameObject _managerObject;
  19. private ScreenLocate _screenLocate;
  20. public void Create()
  21. {
  22. if (_managerObject) return;
  23. string prefabName = "WebCameraView";
  24. var o = UnityEngine.Object.Instantiate(Resources.Load<GameObject>(prefabName));
  25. UnityEngine.Object.DontDestroyOnLoad(o);
  26. o.name = prefabName;
  27. _screenLocate = o.GetComponent<ScreenLocate>();
  28. _screenLocate.InfraredCameraHelper = this;
  29. _managerObject = o;
  30. if (Application.platform == RuntimePlatform.Android) Application.targetFrameRate = 60;
  31. }
  32. public void Dispose()
  33. {
  34. if (_managerObject) UnityEngine.Object.DestroyImmediate(_managerObject);
  35. if (_Instance == this) _Instance = null;
  36. }
  37. #endregion
  38. #region 画面接口
  39. /// <summary>
  40. /// 获取相机画面贴图
  41. /// </summary>
  42. public Texture GetCameraTexture()
  43. {
  44. return _screenLocate.rawImage.texture;
  45. }
  46. /// <summary>
  47. /// 获取相机画面材质
  48. /// </summary>
  49. public Material GetCameraMaterial()
  50. {
  51. return _screenLocate.rawImage.material;
  52. }
  53. /// <summary>
  54. /// 设置亮度(0~10)
  55. /// </summary>
  56. public void SetBrightness(float value)
  57. {
  58. _screenLocate.rawImage.material.SetFloat("_Brightness", value);
  59. }
  60. /// <summary>
  61. /// 获取亮度(0~10)
  62. /// </summary>
  63. public float GetBrightness()
  64. {
  65. return _screenLocate.rawImage.material.GetFloat("_Brightness");
  66. }
  67. /// <summary>
  68. /// 设置饱和度(0~10)
  69. /// </summary>
  70. public void SetSaturation(float value)
  71. {
  72. _screenLocate.rawImage.material.SetFloat("_Saturation", value);
  73. }
  74. /// <summary>
  75. /// 获取饱和度(0~10)
  76. /// </summary>
  77. public float GetSaturation()
  78. {
  79. return _screenLocate.rawImage.material.GetFloat("_Saturation");
  80. }
  81. /// <summary>
  82. /// 设置对比度(0~10)
  83. /// </summary>
  84. public void SetContrast(float value)
  85. {
  86. _screenLocate.rawImage.material.SetFloat("_Contrast", value);
  87. }
  88. /// <summary>
  89. /// 获取对比度(0~10)
  90. /// </summary>
  91. public float GetContrast()
  92. {
  93. return _screenLocate.rawImage.material.GetFloat("_Contrast");
  94. }
  95. /// <summary>
  96. /// 设置相机画面分辨率
  97. /// </summary>
  98. public void SetCameraResolution(int width, int height)
  99. {
  100. _screenLocate.mUVCManager.DefaultWidth = width;
  101. _screenLocate.mUVCManager.DefaultHeight = height;
  102. _screenLocate.mUVCDrawer.DefaultWidth = width;
  103. _screenLocate.mUVCDrawer.DefaultHeight = height;
  104. }
  105. /// <summary>
  106. /// 获取相机画面分辨率
  107. /// </summary>
  108. public Vector2 GetCameraResolution()
  109. {
  110. return new Vector2(
  111. _screenLocate.mUVCDrawer.DefaultWidth,
  112. _screenLocate.mUVCDrawer.DefaultHeight
  113. );
  114. }
  115. #endregion
  116. #region 操作接口
  117. /// <summary>
  118. /// 单点模式开关
  119. /// </summary>
  120. public void SetSinglePoint(bool value)
  121. {
  122. _screenLocate.bSinglePoint = value;
  123. }
  124. /// <summary>
  125. /// 是否为单点模式
  126. /// </summary>
  127. public bool IsSinglePoint()
  128. {
  129. return _screenLocate.bSinglePoint;
  130. }
  131. /// <summary>
  132. /// 设置抖动过滤值
  133. /// </summary>
  134. public void SetShakeFilterValue(float value)
  135. {
  136. _screenLocate.filterDis = value;
  137. }
  138. /// <summary>
  139. /// 获取抖动过滤值
  140. /// </summary>
  141. public float GetShakeFilterValue()
  142. {
  143. return _screenLocate.filterDis;
  144. }
  145. /// <summary>
  146. /// 是否已经定位
  147. /// </summary>
  148. public bool IsScreenLoateOK()
  149. {
  150. return _screenLocate.IsScreenLoateOK();
  151. }
  152. /// <summary>
  153. /// 是否正在手动定位
  154. /// </summary>
  155. public bool IsScreenLocateManualDoing()
  156. {
  157. return _screenLocate.IsScreenLocateManualDoing();
  158. }
  159. /// <summary>
  160. /// 进入手动定位
  161. /// </summary>
  162. /// <returns>相机画面截图</returns>
  163. public Texture2D EnterScreenLocateManual()
  164. {
  165. return _screenLocate.EnterScreenLocateManual();
  166. }
  167. /// <summary>
  168. /// 退出/完成手动定位
  169. /// </summary>
  170. /// <param name="points">定位点(左下、右下、右上、左上)</param>
  171. public void QuitScreenLocateManual(List<Vector2> points)
  172. {
  173. _screenLocate.QuitScreenLocateManual(points);
  174. }
  175. #endregion
  176. #region 监听事件
  177. /// <summary>
  178. /// 单点更新事件
  179. /// </summary>
  180. public OnPositionUpdateEvent OnPositionUpdate;
  181. public delegate void OnPositionUpdateEvent(Vector2 position);
  182. public void InvokeOnPositionUpdate(Vector2 position)
  183. {
  184. try
  185. {
  186. OnPositionUpdate?.Invoke(position);
  187. }
  188. catch (Exception e)
  189. {
  190. Debug.LogError(e);
  191. }
  192. }
  193. /// <summary>
  194. /// 双点更新事件
  195. /// </summary>
  196. public OnPositionUpdate2Event OnPositionUpdate2;
  197. public delegate void OnPositionUpdate2Event(Vector2 position, int index);
  198. public void InvokeOnPositionUpdate2(Vector2 position, int index)
  199. {
  200. try
  201. {
  202. OnPositionUpdate2?.Invoke(position, index);
  203. }
  204. catch (Exception e)
  205. {
  206. Debug.LogError(e);
  207. }
  208. }
  209. #endregion
  210. }
  211. }