ScreenLocate.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. #define ENABLE_LOG
  2. using Serenegiant.UVC;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using ZIM;
  9. using static Serenegiant.UVC.UVCManager;
  10. using Color = UnityEngine.Color;
  11. [RequireComponent(typeof(Canvas))]
  12. public class ScreenLocate : MonoBehaviour
  13. {
  14. private const string TAG = "ScreenLocate#";
  15. enum Mode
  16. {
  17. InfraredLocate,
  18. ScreenMap,
  19. ScreenLocateManual
  20. }
  21. enum InfraredCount
  22. {
  23. Single,
  24. Double
  25. }
  26. enum Platform
  27. {
  28. Window,
  29. Android
  30. }
  31. Platform mPlatform = Platform.Android;
  32. // 2个灯,顺序根据红外灯的大小 由大到小, 坐标通过 InfraredSpot.ScreenUV 和 InfraredSpot.CameraLocation 获得
  33. public InfraredSpot[] InfraredSpots
  34. {
  35. get
  36. {
  37. infraredCount = InfraredCount.Double;
  38. return infraredSpotBuffer;
  39. }
  40. }
  41. // 1个灯, 坐标通过 InfraredSpot.ScreenUV 和 InfraredSpot.CameraLocation 获得
  42. public InfraredSpot InfraredSpotSingle
  43. {
  44. get
  45. {
  46. infraredCount = InfraredCount.Single;
  47. return infraredSpotBuffer[0];
  48. }
  49. }
  50. InfraredSpot[] infraredSpotBuffer;
  51. InfraredCount infraredCount;
  52. #region UVC 处理的对象
  53. public UVCManager mUVCManager;
  54. public UVCDrawer mUVCDrawer;
  55. public CameraInfo mUVCCameraInfo;
  56. private Texture mUVCTexture;
  57. private Texture2D mUVCTexture2D;
  58. // [SerializeField] Texture2DArray mUVCOutArray;
  59. #endregion
  60. public Text Info;
  61. public List<RectTransform> CrosshairInCamera;
  62. public List<RectTransform> CrosshairInScreen;
  63. public RectTransform ScreenQuad;
  64. public Toggle SaveToggle;
  65. public bool ShowScreenQuad = false;
  66. public RawImage rawImage;
  67. public RawImage rawImage1;
  68. public RawImage rawImage2;
  69. public RawImage rawImage3;
  70. public RawImage rawImage4;
  71. public RawImage rawImage5;
  72. public RawImage FullScreenImage;
  73. public InfraredSpotSettings InfraredSpotSettings;
  74. public Texture2D DebugScreenImage;
  75. public Button startUVCBtn;
  76. public Button manualDebugBtn;
  77. public InfraredManager.UIManagerSingle mUIManagerSingle;
  78. // private SynchronizationContext mainContext;
  79. //是否单点显示
  80. bool bSinglePoint = true;//默认单点识别
  81. public Toggle SinglePointToggle;
  82. bool bIdentifyRed = true;//默认设备红色
  83. public Toggle SinglePointToggleColor;
  84. #region 性能检测相关
  85. public Text m_UITime;
  86. const float m_UIUpdateInterval = 0.1f;
  87. float m_UIUpdateTimer = 0.0f;
  88. List<float> m_History = new List<float>(100);
  89. int m_ValidHistoryFrames = 0;
  90. float m_AverageTime = float.NaN;
  91. float m_MedianTime = float.NaN;
  92. float m_MinTime = float.NaN;
  93. float m_MaxTime = float.NaN;
  94. public float updateInterval = 0.5F;
  95. private double lastInterval;
  96. private int frames = 0;
  97. private float fps;
  98. public Text m_FPS;
  99. #endregion
  100. InfraredLocate infraredLocate;
  101. RectTransform canvas;
  102. Mode mode;
  103. List<Vector2> pointManual = new List<Vector2>();
  104. //o0.Project.WebCam o0WebCam = null;
  105. o0.Project.ScreenIdentification screenIdentification;
  106. static public ScreenLocate Main;
  107. static public List<RawImage> DebugImage = new List<RawImage>();
  108. static public RectTransform BackQuad = null;
  109. static public void DebugTexture(int index, Texture texture)
  110. {
  111. Destroy(DebugImage[index].texture);
  112. DebugImage[index].texture = texture;
  113. }
  114. static public void SetScreen(UnityEngine.Color? color = null)
  115. {
  116. if (BackQuad == null)
  117. {
  118. var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
  119. var background = canvas.Find("Background");
  120. BackQuad = background.GetChild(0).GetComponent<RectTransform>();
  121. }
  122. BackQuad.parent.gameObject.SetActive(color != null);
  123. BackQuad.GetComponent<RawImage>().color = color ?? Color.black;
  124. }
  125. static public void SetScreen(Rect rect, UnityEngine.Color? color = null)
  126. {
  127. if (BackQuad == null)
  128. {
  129. var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
  130. var background = canvas.Find("Background");
  131. BackQuad = background.GetChild(0).GetComponent<RectTransform>();
  132. }
  133. BackQuad.parent.gameObject.SetActive(color != null);
  134. BackQuad.anchorMin = rect.min;
  135. BackQuad.anchorMax = rect.max;
  136. BackQuad.GetComponent<RawImage>().color = color ?? Color.black;
  137. }
  138. static void DebugBackQuad(Rect? rect = null)
  139. {
  140. if (BackQuad)
  141. {
  142. BackQuad.parent.GetComponent<RawImage>().enabled = false;
  143. BackQuad.GetComponent<RawImage>().color = Color.white;
  144. BackQuad.parent.gameObject.SetActive(!BackQuad.parent.gameObject.activeSelf);
  145. if (rect.HasValue)
  146. {
  147. BackQuad.anchorMin = rect.Value.min;
  148. BackQuad.anchorMax = rect.Value.max;
  149. }
  150. }
  151. }
  152. void Awake()
  153. {
  154. if (mUVCDrawer)
  155. mUVCDrawer.StartPreviewAction += UVCIsReady;
  156. }
  157. void OnDestroy()
  158. {
  159. if (mUVCDrawer)
  160. mUVCDrawer.StartPreviewAction -= UVCIsReady;
  161. }
  162. void Start()
  163. {
  164. //mainContext = SynchronizationContext.Current;
  165. Main = this;
  166. DebugImage.Add(rawImage);
  167. DebugImage.Add(rawImage1);
  168. DebugImage.Add(rawImage2);
  169. DebugImage.Add(rawImage3);
  170. DebugImage.Add(rawImage4);
  171. DebugImage.Add(rawImage5);
  172. DebugImage.Add(FullScreenImage);
  173. canvas = transform.GetComponent<RectTransform>();
  174. mode = Mode.InfraredLocate;
  175. infraredCount = InfraredCount.Single;
  176. if (SinglePointToggle)
  177. {
  178. //如果是单点显示
  179. bSinglePoint = PlayerPrefs.GetInt("bSinglePoint", 1) == 1;
  180. SinglePointToggle.isOn = bSinglePoint;
  181. }
  182. if (SinglePointToggleColor)
  183. {
  184. bIdentifyRed = PlayerPrefs.GetInt("bIdentifyRed", 1) == 1;
  185. SinglePointToggleColor.isOn = bIdentifyRed;
  186. }
  187. #region 性能检测相关
  188. for (var i = 0; i < m_History.Capacity; ++i)
  189. {
  190. m_History.Add(0.0f);
  191. }
  192. lastInterval = Time.realtimeSinceStartup;
  193. frames = 0;
  194. #endregion
  195. }
  196. //ZIMWebCamera场景使用
  197. public void WebCamIsReady(Texture texture)
  198. {
  199. mPlatform = Platform.Window;
  200. mUVCTexture = texture;
  201. mUVCCameraInfo = new CameraInfo(mUVCTexture);
  202. brightness = 0;
  203. }
  204. //手机端UVCCamra使用
  205. public void UVCIsReady(Texture texture)
  206. {
  207. mPlatform = Platform.Android;
  208. //this.startUVCBtn.interactable = true;
  209. mUVCTexture = texture;
  210. //ARGB32
  211. //mUVCTexture2D = Texture2D.CreateExternalTexture(texture.width, texture.height, TextureFormat.ARGB32, false, true, texture.GetNativeTexturePtr()); //TextureToTexture2D(texture);
  212. //Debug.Log("mUVCTexture2D isReable:" + mUVCTexture2D.isReadable);
  213. //Debug.Log("mUVCTexture2D mipmapCount:" + (mUVCTexture2D.mipmapCount > 1));
  214. List<CameraInfo> cameraInfos = mUVCManager.GetAttachedDevices();
  215. mUVCCameraInfo = cameraInfos[cameraInfos.Count - 1];
  216. manualDebugBtn.interactable = true;
  217. }
  218. public void OnChangeSinglePoint()
  219. {
  220. bSinglePoint = SinglePointToggle.isOn;
  221. PlayerPrefs.SetInt("bSinglePoint", bSinglePoint ? 1 : 0);
  222. }
  223. public void OnChangeSinglePointColor()
  224. {
  225. bIdentifyRed = SinglePointToggleColor.isOn;
  226. PlayerPrefs.SetInt("bIdentifyRed", bIdentifyRed ? 1 : 0);
  227. }
  228. public void startUVC()
  229. {
  230. }
  231. int brightness = 0;
  232. //public Text brightnessText;
  233. //public void SliderBrightness(Slider slider)
  234. //{
  235. // var _value = slider.value;
  236. // brightness = (int)_value;
  237. // brightnessText.text = (2 + brightness) + "";
  238. //}
  239. void Update()
  240. {
  241. ++frames;
  242. float timeNow = Time.realtimeSinceStartup;
  243. if (timeNow > lastInterval + updateInterval)
  244. {
  245. fps = (float)(frames / (timeNow - lastInterval));
  246. frames = 0;
  247. lastInterval = timeNow;
  248. }
  249. if (m_FPS != null)
  250. m_FPS.text = "FPS:" + fps.ToString("f2");
  251. if (mUVCCameraInfo == null) return;
  252. if (screenIdentification == null)
  253. screenIdentification = new o0.Project.ScreenIdentification(mUVCTexture);
  254. if (infraredLocate == null)
  255. infraredLocate = new InfraredLocate(mUVCCameraInfo, screenIdentification, InfraredSpotSettings);
  256. if (mode == Mode.ScreenLocateManual)
  257. {
  258. if (Input.GetMouseButtonDown(0))
  259. {
  260. var mouse = Input.mousePosition;
  261. var u = mouse.x / Screen.width;
  262. var v = mouse.y / Screen.height;
  263. u = Math.Clamp(u, 0, 1);
  264. v = Math.Clamp(v, 0, 1);
  265. pointManual.Add(new Vector2(u * mUVCTexture.width, v * mUVCTexture.height));
  266. var obj = Instantiate(Resources.Load("Point")) as GameObject;
  267. obj.transform.SetParent(FullScreenImage.transform);
  268. obj.transform.localPosition = new Vector2(u, v).pixelToLocalPosition_AnchorCenter(new Vector2(1, 1), FullScreenImage.rectTransform.rect);
  269. if (pointManual.Count == 1)
  270. Info.text = "左键单击屏幕 右下角";
  271. else if (pointManual.Count == 2)
  272. Info.text = "左键单击屏幕 右上角";
  273. else if (pointManual.Count == 3)
  274. Info.text = "左键单击屏幕 左上角";
  275. else if (pointManual.Count == 4)
  276. {
  277. screenIdentification.LocateScreenManual(new OrdinalQuadrilateral(pointManual[0].o0Vector(), pointManual[1].o0Vector(), pointManual[3].o0Vector(), pointManual[2].o0Vector()));
  278. pointManual.Clear();
  279. ShowScreen(screenIdentification.Screen.Quad);
  280. foreach (Transform i in FullScreenImage.transform)
  281. Destroy(i.gameObject);
  282. ToMode(Mode.InfraredLocate);
  283. }
  284. }
  285. return;
  286. }
  287. var t0 = Time.realtimeSinceStartup;
  288. /* New*/
  289. if (mUVCCameraInfo != null && mUVCCameraInfo.IsPreviewing && screenIdentification.Screen.Active) // 成功定位屏幕后才做红外识别
  290. {
  291. CreateUVCTexture2DIfNeeded();
  292. if (!screenIdentification.Update(mUVCTexture2D))
  293. {
  294. if (mode == Mode.InfraredLocate)
  295. {
  296. //0,0, cameraTexture2D.width, cameraTexture2D.height,0
  297. var pixels = mUVCTexture2D.GetPixels(); // 从左往右、从下往上
  298. //InfraredSpots = infraredLocate.Update(pixels);
  299. if (bSinglePoint)
  300. infraredSpotBuffer = infraredLocate.UpdateSingle(pixels);
  301. else
  302. infraredSpotBuffer = infraredLocate.Update(pixels);
  303. if (mPlatform == Platform.Window || mUIManagerSingle.bGetPanelActive) //渲染ui上面的点。进入游戏可以隐藏
  304. {
  305. for (int i = 0; i < infraredSpotBuffer.Length; i++)
  306. {
  307. if (infraredSpotBuffer[i].CameraLocation != null)
  308. {
  309. // 检测到光点
  310. var posInCanvas = infraredSpotBuffer[i].CameraLocation.Value.pixelToLocalPosition_AnchorCenter(mUVCCameraInfo.Size, rawImage.rectTransform.rect);
  311. CrosshairInCamera[i].gameObject.SetActive(true);
  312. CrosshairInCamera[i].anchoredPosition = posInCanvas;
  313. }
  314. else
  315. CrosshairInCamera[i].gameObject.SetActive(false);
  316. }
  317. }
  318. //手机端使用
  319. if (mPlatform == Platform.Android && infraredSpotBuffer.Length > 0)
  320. {
  321. int redIndex = 0;
  322. int greenIndex = 1;
  323. //仅仅第一个点显示(如果最大点出界了会闪烁)
  324. if (bSinglePoint)
  325. {
  326. redIndex = 0; //单点识别是,可以选择切换颜色
  327. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  328. {
  329. string str = "Single:";
  330. Info.text = str + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  331. InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  332. }
  333. }
  334. else
  335. {
  336. // 检测到光点
  337. if (infraredSpotBuffer[redIndex].ScreenUV != null)
  338. {
  339. Info.text = "Red:" + infraredSpotBuffer[redIndex].ScreenUV.Value.ToString("F4");
  340. InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[redIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[redIndex].ScreenUV.Value.y * Screen.height, 0));
  341. }
  342. else if (infraredSpotBuffer[greenIndex].ScreenUV != null)
  343. {
  344. Info.text = "Green:" + infraredSpotBuffer[greenIndex].ScreenUV.Value.ToString("F4");
  345. InfraredManager.ConnetDevicesSingle.ins.posAction?.Invoke(new Vector3(infraredSpotBuffer[greenIndex].ScreenUV.Value.x * Screen.width, infraredSpotBuffer[greenIndex].ScreenUV.Value.y * Screen.height, 0));
  346. }
  347. }
  348. }
  349. }
  350. }
  351. }
  352. var t1 = Time.realtimeSinceStartup;
  353. var dt = t1 - t0;
  354. m_History[m_ValidHistoryFrames % m_History.Count] = dt;
  355. ++m_ValidHistoryFrames;
  356. m_UIUpdateTimer += Time.deltaTime;
  357. if (m_UIUpdateTimer >= m_UIUpdateInterval)
  358. {
  359. m_UIUpdateTimer = 0.0f;
  360. if (m_ValidHistoryFrames >= m_History.Count)
  361. {
  362. m_ValidHistoryFrames = 0;
  363. m_AverageTime = 0.0f;
  364. m_MinTime = float.PositiveInfinity;
  365. m_MaxTime = float.NegativeInfinity;
  366. {
  367. for (var i = 0; i < m_History.Count; i++)
  368. {
  369. var time = m_History[i];
  370. m_AverageTime += time;
  371. m_MinTime = Mathf.Min(m_MinTime, time);
  372. m_MaxTime = Mathf.Max(m_MaxTime, time);
  373. }
  374. m_AverageTime /= m_History.Count;
  375. }
  376. {
  377. m_History.Sort();
  378. // Odd-length history?
  379. if ((m_History.Count & 1) != 0)
  380. {
  381. m_MedianTime = m_History[m_History.Count / 2];
  382. }
  383. else
  384. {
  385. m_MedianTime = (m_History[m_History.Count / 2] + m_History[m_History.Count / 2 - 1]) / 2.0f;
  386. }
  387. }
  388. }
  389. var statistics = $"{m_History.Count} 帧样本:\n average: {m_AverageTime * 1000.0f:F2}ms\n median: {m_MedianTime * 1000.0f:F2}ms\n min: {m_MinTime * 1000.0f:F2}ms\n max: {m_MaxTime * 1000.0f:F2}ms\n";
  390. //Method: {m_Method} {UnityEngine.SceneManagement.SceneManager.GetActiveScene().name} |
  391. if (m_UITime != null)
  392. m_UITime.text = $"camera size: {mUVCCameraInfo.CurrentWidth}x{mUVCCameraInfo.CurrentHeight} \n Last Frame: {dt * 1000.0f:F2}ms \n{statistics}";
  393. }
  394. UpdateInputs();
  395. }
  396. public void BtnScreenLocate()
  397. {
  398. screenIdentification.LocateScreen();
  399. }
  400. public void BtnScreenMap()
  401. {
  402. ToMode(Mode.ScreenMap);
  403. }
  404. //进入手动定位屏幕
  405. public void BtnScreenLocateManual()
  406. {
  407. ToMode(Mode.ScreenLocateManual);
  408. }
  409. // 标记屏幕的四个角
  410. public void ShowScreen(OrdinalQuadrilateral quad)
  411. {
  412. if (quad == null)
  413. {
  414. Info.text = "识别屏幕失败";
  415. return;
  416. }
  417. Info.text = "已识别到屏幕";
  418. if (ShowScreenQuad)
  419. {
  420. ScreenQuad.gameObject.SetActive(true);
  421. for (int i = 0; i < 4; i++)
  422. {
  423. RectTransform t = ScreenQuad.GetChild(i) as RectTransform;
  424. //mUVCCameraInfo.Size
  425. t.anchoredPosition = quad[i].UnityVector().pixelToLocalPosition_AnchorCenter(mUVCCameraInfo.Size, ScreenQuad.rect);
  426. }
  427. }
  428. }
  429. void ToMode(Mode mode)
  430. {
  431. if (this.mode == mode)
  432. return;
  433. if (mode == Mode.ScreenMap)
  434. {
  435. if (!screenIdentification.Screen.Active)
  436. {
  437. Info.text = "先定位屏幕";
  438. return;
  439. }
  440. Info.text = "按ESC退出";
  441. SetScreen(Color.black);
  442. Info.transform.SetAsLastSibling();
  443. this.mode = Mode.ScreenMap;
  444. }
  445. else if (mode == Mode.InfraredLocate)
  446. {
  447. Info.text = screenIdentification.Screen.Active ? "已定位屏幕" : "定位屏幕失败";
  448. //Info.text = "已识别到屏幕";
  449. SetScreen(null);
  450. foreach (var i in CrosshairInScreen)
  451. i.gameObject.SetActive(false);
  452. FullScreenImage.gameObject.SetActive(false);
  453. Info.transform.SetSiblingIndex(transform.childCount - 4);
  454. this.mode = Mode.InfraredLocate;
  455. DebugTexture(6, null);
  456. //DebugTexture(1, null); //null
  457. // rawImage1.texture = null;
  458. #if (!NDEBUG && DEBUG && ENABLE_LOG)
  459. Console.WriteLine($"{TAG} Mode.InfraredLocate:已识别到屏幕:{ screenIdentification.Screen.Active}");
  460. #endif
  461. }
  462. else if (mode == Mode.ScreenLocateManual)
  463. {
  464. Info.text = "左键单击屏幕 左下角";
  465. FullScreenImage.gameObject.SetActive(true);
  466. Info.transform.SetSiblingIndex(transform.childCount - 1);
  467. // var newTex = WebCamera.webCamTexture.AutoLight(10);
  468. //DebugTexture(1, TextureToTexture2D(rawImage.texture));
  469. CreateUVCTexture2DIfNeeded();
  470. DebugTexture(6, mUVCTexture2D.zimAutoLight(brightness));
  471. //mUVCTexture2DTemp = TextureToTexture2D(mUVCCameraInfo.previewTexture);
  472. //DebugTexture(6, mUVCTexture2DTemp.zimAutoLight(brightness));
  473. this.mode = Mode.ScreenLocateManual;
  474. }
  475. }
  476. private Texture2D TextureToTexture2D(Texture texture)
  477. {
  478. Texture2D _texture2D = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false, true);
  479. RenderTexture currentRT = RenderTexture.active;
  480. RenderTexture renderTexture = RenderTexture.GetTemporary(
  481. texture.width,
  482. texture.height,
  483. 0,
  484. RenderTextureFormat.ARGB32,
  485. RenderTextureReadWrite.Linear);
  486. Graphics.Blit(texture, renderTexture);
  487. RenderTexture.active = renderTexture;
  488. _texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
  489. _texture2D.Apply();
  490. RenderTexture.active = currentRT;
  491. RenderTexture.ReleaseTemporary(renderTexture);
  492. return _texture2D;
  493. }
  494. private void CreateUVCTexture2DIfNeeded()
  495. {
  496. if (mUVCTexture2D != null)
  497. Destroy(mUVCTexture2D);
  498. mUVCTexture2D = TextureToTexture2D(mUVCTexture);
  499. }
  500. #region DoubleButton
  501. private DateTime m_firstTime;
  502. private DateTime m_secondTime;
  503. private void Press()
  504. {
  505. Debug.Log("进入手动定位");
  506. BtnScreenLocateManual();
  507. resetTime();
  508. }
  509. public void OnDoubleClick()
  510. {
  511. //超时重置
  512. if (!m_firstTime.Equals(default(DateTime)))
  513. {
  514. var intervalTime = DateTime.Now - m_firstTime;
  515. float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
  516. if (milliSeconds >= 400)
  517. resetTime();
  518. }
  519. // 按下按钮时对两次的时间进行记录
  520. if (m_firstTime.Equals(default(DateTime)))
  521. m_firstTime = DateTime.Now;
  522. else
  523. m_secondTime = DateTime.Now;
  524. // 在第二次点击触发,时差小于400ms触发
  525. if (!m_firstTime.Equals(default(DateTime)) && !m_secondTime.Equals(default(DateTime)))
  526. {
  527. var intervalTime = m_secondTime - m_firstTime;
  528. float milliSeconds = intervalTime.Seconds * 1000 + intervalTime.Milliseconds;
  529. if (milliSeconds < 400)
  530. Press();
  531. else
  532. resetTime();
  533. }
  534. }
  535. private void resetTime()
  536. {
  537. m_firstTime = default(DateTime);
  538. m_secondTime = default(DateTime);
  539. }
  540. #endregion
  541. #region 性能检测相关
  542. void InvalidateTimings()
  543. {
  544. m_ValidHistoryFrames = 0;
  545. m_AverageTime = float.NaN;
  546. m_MedianTime = float.NaN;
  547. m_MinTime = float.NaN;
  548. m_MaxTime = float.NaN;
  549. }
  550. void UpdateInputs()
  551. {
  552. //重置
  553. if (Input.GetKeyDown(KeyCode.UpArrow))
  554. {
  555. InvalidateTimings();
  556. }
  557. }
  558. #endregion
  559. }