InfraredDemo.cs 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using InfraredManager;
  6. using ZIM;
  7. using System.Linq;
  8. using SLAMUVC;
  9. using System;
  10. using SmartBowSDK;
  11. using ZIM.Unity;
  12. public class InfraredDemo : JCUnityLib.ViewBase
  13. {
  14. public static bool DebugInEditor = true;
  15. public static InfraredDemo _ins;
  16. [SerializeField]
  17. RectTransform canvasRectTransform;
  18. public Button mBtnSee;
  19. public static void Create()
  20. {
  21. if (_ins) return;
  22. GameObject o = Instantiate(Resources.Load<GameObject>("InfraredDemo"));
  23. DontDestroyOnLoad(o);
  24. _ins = o.GetComponent<InfraredDemo>();
  25. //添加一个父物体
  26. o.transform.SetParent(ViewMgr.Instance.transform.Find("1").transform);
  27. CanvasScaler canvasScaler = o.GetComponent<CanvasScaler>();
  28. if (canvasScaler != null)
  29. {
  30. Destroy(canvasScaler);
  31. }
  32. RectTransform rectTransform = o.GetComponent<RectTransform>();
  33. rectTransform.anchorMin = Vector2.zero; // 左下角对齐父级
  34. rectTransform.anchorMax = Vector2.one; // 右上角对齐父级
  35. rectTransform.offsetMin = Vector2.zero; // 移除左下角偏移
  36. rectTransform.offsetMax = Vector2.zero; // 移除右上角偏移
  37. rectTransform.localScale = Vector3.one; // 确保缩放为 1
  38. //直接初始化一次
  39. _ins.InitInfraredCamera();
  40. //生成一个控制台
  41. UserPlayer.InitLogReporter();
  42. }
  43. void Start()
  44. {
  45. if (Application.platform != RuntimePlatform.WindowsEditor) DebugInEditor = false;
  46. SetVisiable(false);
  47. InitDebugScreenPoint();
  48. gameObject.AddComponent<FPSTester>();
  49. if (CommonConfig.StandaloneModeOrPlatformB)
  50. {
  51. mBtnSee.gameObject.SetActive(false);
  52. }
  53. else {
  54. mBtnSee.interactable = false;
  55. mBtnSee.transform.Find("Text").GetComponent<Text>().text = TextAutoLanguage2.GetTextByKey("WaitingForInitialization"); // "等待初始化..";
  56. }
  57. //直接初始化一次
  58. //InitInfraredCamera();
  59. Debug.Log("[InfraredDemo] Start Function!");
  60. initToggle();
  61. initExport();
  62. InitFullScreen();
  63. #if UNITY_STANDALONE_WIN
  64. _calibrationFixedText.transform.parent.gameObject.SetActive(false);
  65. #endif
  66. }
  67. void OnGUI()
  68. {
  69. ShowVersionCode();
  70. }
  71. private void ShowVersionCode()
  72. {
  73. GUIStyle labelFont = new GUIStyle();
  74. labelFont.normal.textColor = new Color(1, 0.6f, 0.6f);
  75. labelFont.fontSize = Mathf.CeilToInt(Screen.height * 0.03f);
  76. GUI.Label(new Rect(Screen.width / 100, Screen.height / 100, 100, 100), "game-20241204-1814-5-1", labelFont);
  77. }
  78. void Update()
  79. {
  80. UpdateInfraredCamera();
  81. //UpdateDebugScreenPoint();
  82. }
  83. public void OnClick_See()
  84. {
  85. SetVisiable(!_visiable);
  86. }
  87. //初始化一次 initScreenLocateManual
  88. bool hasCalled = false;
  89. bool _visiable;
  90. void SetVisiable(bool value)
  91. {
  92. _visiable = value;
  93. transform.Find("Background").gameObject.SetActive(value);
  94. transform.Find("InfraredCamera").gameObject.SetActive(value);
  95. //如果是b端,控制这里按钮显示
  96. if (CommonConfig.StandaloneModeOrPlatformB)
  97. {
  98. mBtnSee.gameObject.SetActive(value);
  99. }
  100. updateBtnSee();
  101. Action<Text> onApplyToNext = (t) => {
  102. updateBtnSee();
  103. };
  104. mBtnSee.GetComponentInChildren<TextAutoLanguage2>().onApplyToNext += onApplyToNext;
  105. //if (value) InitInfraredCamera();
  106. //刷新翻译的Dropdown
  107. if (value) {
  108. UpdateLanguage();
  109. updateDropdownResolution2();
  110. }
  111. //if (!hasCalled && value)
  112. //{
  113. // hasCalled = true;
  114. // //如果本地有记录,初始化一次
  115. // StartCoroutine(initScreenLocateManual());
  116. // Debug.Log("[InfraredDemo] SetVisiable Function!");
  117. //}
  118. }
  119. void updateBtnSee() {
  120. mBtnSee.GetComponentInChildren<Text>().text = _visiable ? TextAutoLanguage2.GetTextByKey("HideInterface") : TextAutoLanguage2.GetTextByKey("DebugInfrared");//"隐藏界面" : "调试红外";
  121. }
  122. Text _spText;
  123. Vector2 _screenPoint;
  124. void InitDebugScreenPoint()
  125. {
  126. _spText = transform.Find("SPText").GetComponent<Text>();
  127. //_spText.gameObject.SetActive(DebugInEditor);
  128. _spText.gameObject.SetActive(false);
  129. }
  130. void UpdateDebugScreenPoint()
  131. {
  132. if (!DebugInEditor) return;
  133. if (infraredCameraHelper == null) return;
  134. _screenPoint.x = Mathf.Clamp(
  135. _screenPoint.x + Input.GetAxis("Horizontal") * Screen.width / 400f,
  136. 0, Screen.width);
  137. _screenPoint.y = Mathf.Clamp(
  138. _screenPoint.y + Input.GetAxis("Vertical") * Screen.height / 400f,
  139. 0, Screen.height);
  140. infraredCameraHelper.InvokeOnPositionUpdate(_screenPoint);
  141. _spText.text = _screenPoint.ToString();
  142. }
  143. #region 红外摄像
  144. [SerializeField] RawImage _cameraRender;
  145. [SerializeField] RawImage _cameraRender2;
  146. /// <summary>
  147. /// 测试texture
  148. /// </summary>
  149. public RawImage MyCameraRender2
  150. {
  151. get { return _cameraRender2; }
  152. set { _cameraRender2 = value; }
  153. }
  154. //[SerializeField] MaintainAspectRatio _MaintainAspectRatio;
  155. [SerializeField] List<RectTransform> _crosshairsInCamera;
  156. [SerializeField] Slider _sliderShakeFilter;
  157. [SerializeField] Button _btnReset;
  158. [SerializeField] Button _btnScreenLocateManual;
  159. [SerializeField] Button _btnScreenLocateManualAuto;
  160. [SerializeField] Slider _sliderCapture;
  161. [SerializeField] Slider _sliderDelay;
  162. [SerializeField] Dropdown _dropdownResolution;
  163. [SerializeField] Dropdown _dropdownResolution2;
  164. [SerializeField] Slider _sliderLineWidth;
  165. [SerializeField] Slider _fanWidth;
  166. //红外线阈值
  167. [SerializeField] Slider _infraredFilter;
  168. //调试UVC参数
  169. [SerializeField] GameObject _cameraParameterPanel;
  170. [SerializeField] Button _btnAdjusting;
  171. //野鸭选择
  172. [SerializeField] Dropdown _duckLevelDropdown;
  173. //Dictionary<string, ParamFloatValue> dUVCParameters = new Dictionary<string, ParamFloatValue>();
  174. //public ParamFloatValue brightness = new ParamFloatValue("ic_brightness", 1.0f);
  175. //public ParamFloatValue saturation = new ParamFloatValue("ic_saturation", 1.0f);
  176. //public ParamFloatValue contrast = new ParamFloatValue("ic_contrast", 1.0f);
  177. #if UNITY_ANDROID
  178. //初始化纹理-1280*720,index = 0
  179. // public ParamFloatValue resoution = new ParamFloatValue("ic_resoution2", 0);
  180. //摄像机分辨率 -320*240,index = 10
  181. public ParamFloatValue resoutionNew = new ParamFloatValue("ic_resoutionNew", 10);
  182. //摄像机分辨率 -只处理高低分辨率情况
  183. public ParamFloatValue resoution = new ParamFloatValue("ic_resoution", 0);
  184. //string[] resolutions = { "高分辨率", "低分辨率" };
  185. #endif
  186. //抖动过滤值 - 6.0
  187. public ParamFloatValue shakeFilterValue = new ParamFloatValue("ic_shakeFilterValue2", 6.0f);
  188. //亮度过滤阈值 - 0.8
  189. public ParamFloatValue infraredFileterValue = new ParamFloatValue("ic_infraredFileterValue", 0.8f);
  190. //线段宽度阈值
  191. public ParamFloatValue lineWidth = new ParamFloatValue("ic_lineWidth", 4.0f);
  192. //扇形宽度
  193. public ParamFloatValue fanWidth = new ParamFloatValue("ic_fanWidth", 90.0f);
  194. public ParamFloatValue captureValue = new ParamFloatValue("ic_captureValue", 30.0f);
  195. public ParamFloatValue delayValue = new ParamFloatValue("ic_delayValue", 30.0f);
  196. //红外准心,默认打开吧
  197. public ParamFloatValue crosshairValue = new ParamFloatValue("ic_crosshairValue", 1);
  198. //野鸭测试存储
  199. public ParamFloatValue duckHunterLevel = new ParamFloatValue("test_duckHunterLevelValue", 1);
  200. //存储滑块信息。camera
  201. public Dictionary<string ,ParamFloatValue> ParamFloatValueCameraSlider = new Dictionary<string, ParamFloatValue>();
  202. public static InfraredCameraHelper infraredCameraHelper;
  203. public static bool running { get => infraredCameraHelper != null; }
  204. private bool _inited;
  205. #region 参数控制
  206. public GameObject togglePrefab; // 拖入一个Toggle预设体
  207. public GameObject sliderPrefab; // 拖入一个Slider预设体
  208. public GameObject spawnPoint;
  209. //自动曝光
  210. bool bAutoAE = false;
  211. Toggle CTRLAEToggle;
  212. Slider CTRLAEABSSlider;
  213. string[] sliderNameArray = new string[]{
  214. "自动曝光模式",
  215. "曝光时间(绝对)",
  216. "亮度", //
  217. "对比度",
  218. "色调",
  219. "饱和度",
  220. "锐度",
  221. "伽玛",
  222. //"白平衡温度",
  223. //"白平衡分量",
  224. "背光补偿",
  225. "增益" };
  226. string[] sliderStrArray = new string[]{
  227. "CTRL_AE",
  228. "CTRL_AE_ABS",
  229. "PU_BRIGHTNESS",
  230. "PU_CONTRAST",
  231. "PU_HUE",
  232. "PU_SATURATION",
  233. "PU_SHARPNESS",
  234. "PU_GAMMA",
  235. //"PU_WB_TEMP",
  236. //"PU_WB_COMPO",
  237. "PU_BACKLIGHT",
  238. "PU_GAIN" };
  239. UVCManager.CameraInfo currentCameraInfo;
  240. //初始化相机参数
  241. public void initSlider(UVCManager.CameraInfo cameraInfo)
  242. {
  243. if (currentCameraInfo != null) return;
  244. currentCameraInfo = cameraInfo;
  245. string[] sliderNameArrayKey = new string[] {
  246. "AutoExposureMode", // "自动曝光模式"
  247. "ExposureTimeAbsolute", // "曝光时间(绝对)"
  248. "Brightness", // "亮度"
  249. "Contrast", // "对比度"
  250. "Hue", // "色调"
  251. "Saturation", // "饱和度"
  252. "Sharpness", // "锐度"
  253. "Gamma", // "伽玛"
  254. "BacklightCompensation", // "背光补偿"
  255. "Gain" // "增益"
  256. };
  257. for (int i = 0; i < sliderStrArray.Length; i++)
  258. {
  259. string typeStr = sliderStrArray[i];
  260. // UInt64 _VALUE = cameraInfo.GetTypeByName(typeStr);
  261. //不支持的,跳过
  262. bool bContains = cameraInfo.ContainsKey(typeStr);
  263. if (!bContains) continue;
  264. if (typeStr == "CTRL_AE")
  265. {
  266. //曝光Toggle
  267. GameObject toggleObject = Instantiate(togglePrefab, spawnPoint.transform);
  268. toggleObject.SetActive(true);
  269. toggleObject.name = typeStr;
  270. Text labelObj = toggleObject.transform.Find("Label").GetComponent<Text>();
  271. //labelObj.text = sliderNameArray[i];
  272. labelObj.gameObject.AddComponent<TextAutoLanguage2>().SetTextKey(sliderNameArrayKey[i]);
  273. Toggle toggle = toggleObject.GetComponent<Toggle>();
  274. CTRLAEToggle = toggle;
  275. UVCCtrlInfo _AEInfo = cameraInfo.GetInfo(typeStr);
  276. Debug.Log("UVCCtrlInfo:" + _AEInfo.ToString());
  277. int _currentValue = cameraInfo.GetValue(typeStr);
  278. bAutoAE = _currentValue == 8 ? true : false; //如果当前值是 8,则当前摄像机是自动曝光模式
  279. toggle.isOn = bAutoAE;
  280. //cameraInfo.SetValue(_VALUE, _currentValue!=8? 8:1);
  281. toggle.onValueChanged.AddListener((bool bValue) =>
  282. {
  283. //Debug.Log("Toggle value changed to: " + bValue + " from " + _VALUE);
  284. if (typeStr == "CTRL_AE")
  285. {
  286. //开关控制是否自动曝光
  287. bAutoAE = bValue;
  288. if (CTRLAEABSSlider) CTRLAEABSSlider.interactable = !bAutoAE;
  289. int _value = bValue ? 8 : 1;
  290. //Debug.Log("_value " + _value);
  291. cameraInfo.SetValue(typeStr, _value);
  292. }
  293. });
  294. }
  295. else
  296. {
  297. //其余使用slider
  298. GameObject sliderObject = Instantiate(sliderPrefab, spawnPoint.transform);
  299. sliderObject.SetActive(true);
  300. sliderObject.name = typeStr;
  301. Slider slider = sliderObject.GetComponent<Slider>();
  302. Text textObj = sliderObject.transform.Find("text").GetComponent<Text>();
  303. Text titleTextObj = sliderObject.transform.Find("title").GetComponent<Text>();
  304. //titleTextObj.text = sliderNameArray[i];//类型名字
  305. titleTextObj.gameObject.AddComponent<TextAutoLanguage2>().SetTextKey(sliderNameArrayKey[i]);
  306. if (slider != null)
  307. {
  308. UVCCtrlInfo _UVCCtrlInfo = cameraInfo.GetInfo(typeStr);
  309. slider.minValue = _UVCCtrlInfo.min;
  310. slider.maxValue = _UVCCtrlInfo.max;
  311. slider.wholeNumbers = true;
  312. int _defValue = _UVCCtrlInfo.def;
  313. //指定默认值
  314. //5、UVC亮度 - 50
  315. //6、UVC对比度 - 50
  316. if (typeStr == "PU_BRIGHTNESS") {
  317. _defValue = 50;
  318. } else if (typeStr == "PU_CONTRAST") {
  319. _defValue = 50;
  320. }
  321. //记录一个typeStr类型的数据存储操作对象
  322. ParamFloatValue paramFloatValue = new ParamFloatValue("ic_uvc_" + typeStr, _defValue);
  323. ParamFloatValueCameraSlider.Add(typeStr, paramFloatValue);
  324. if (typeStr == "PU_BRIGHTNESS" || typeStr == "PU_CONTRAST")
  325. {
  326. //获取设置默认存储的值
  327. int _Value = (int)paramFloatValue.Get();
  328. cameraInfo.SetValue(typeStr, _Value);
  329. Debug.Log("[InfraredDemo] UVC 设置" + typeStr + "默认值:" + _Value);
  330. slider.value = _Value;
  331. textObj.text = _Value + "";
  332. }
  333. else {
  334. //获取设置当前值
  335. int _currentValue = cameraInfo.GetValue(typeStr);
  336. slider.value = _currentValue;
  337. textObj.text = _currentValue + "";
  338. }
  339. //如果是曝光slider
  340. if (typeStr == "CTRL_AE_ABS")
  341. {
  342. CTRLAEABSSlider = slider;
  343. slider.interactable = !bAutoAE;
  344. }
  345. slider.onValueChanged.AddListener((newValue) =>
  346. {
  347. var _value = Mathf.FloorToInt(newValue);
  348. textObj.text = _value + "";
  349. //Debug.Log("Slider value changed to: " + newValue + " from " + _VALUE);
  350. cameraInfo.SetValue(typeStr, _value);
  351. paramFloatValue.Set(_value);
  352. });
  353. }
  354. }
  355. }
  356. }
  357. public void OpenUVCPanel()
  358. {
  359. _cameraParameterPanel.SetActive(true);
  360. }
  361. public void CloseUVCPanel()
  362. {
  363. _cameraParameterPanel.SetActive(false);
  364. }
  365. public void onResetUVCData()
  366. {
  367. #if UNITY_ANDROID
  368. resetUVCData();
  369. #endif
  370. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  371. resetPCData();
  372. #endif
  373. }
  374. void resetUVCData() {
  375. if (currentCameraInfo == null) return;
  376. for (int i = 0; i < sliderStrArray.Length; i++)
  377. {
  378. string typeStr = sliderStrArray[i];
  379. // UInt64 _VALUE = currentCameraInfo.GetTypeByName(typeStr);
  380. bool bContains = currentCameraInfo.ContainsKey(typeStr);
  381. if (!bContains) continue;
  382. if (typeStr == "CTRL_AE")
  383. {
  384. //toggle值不进行重置
  385. Debug.Log("CTRL_AE 不需要重置");
  386. }
  387. else if (typeStr == "CTRL_AE_ABS")
  388. {
  389. if (!bAutoAE)
  390. {
  391. //如果是手动曝光,重置值
  392. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  393. Slider slider = trans.GetComponent<Slider>();
  394. Text textObj = trans.Find("text").GetComponent<Text>();
  395. UVCCtrlInfo _AEInfo = currentCameraInfo.GetInfo(typeStr);
  396. //获取当前值
  397. int _currentValue = currentCameraInfo.GetValue(typeStr);
  398. Debug.Log("CTRL_AE_ABS:" + _currentValue + " = " + _AEInfo.def + " = " + ",bAutoAE:" + bAutoAE);
  399. textObj.text = _AEInfo.def + "";
  400. slider.value = _AEInfo.def;
  401. if (_AEInfo.def != _currentValue) currentCameraInfo.SetValue(typeStr, _AEInfo.def);
  402. }
  403. }
  404. else
  405. {
  406. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  407. Slider slider = trans.GetComponent<Slider>();
  408. Text textObj = trans.Find("text").GetComponent<Text>();
  409. UVCCtrlInfo _UVCCtrlInfo = currentCameraInfo.GetInfo(typeStr);
  410. ParamFloatValue paramFloatValue = ParamFloatValueCameraSlider.GetValueOrDefault(typeStr);
  411. //重置的值
  412. int _defValue = _UVCCtrlInfo.def;
  413. if (typeStr == "PU_BRIGHTNESS")
  414. {
  415. _defValue = (int)paramFloatValue.GetDefault();
  416. }
  417. else if (typeStr == "PU_CONTRAST")
  418. {
  419. _defValue = (int)paramFloatValue.GetDefault();
  420. }
  421. textObj.text = _defValue + "";
  422. slider.value = _defValue;
  423. //获取当前值
  424. int _currentValue = currentCameraInfo.GetValue(typeStr);
  425. //重置存储值
  426. paramFloatValue.Reset();
  427. //存储初始值,设置一次到UVC参数
  428. if (_defValue != _currentValue) currentCameraInfo.SetValue(typeStr, _defValue);
  429. }
  430. }
  431. }
  432. //windows 相机
  433. string[] sliderNameArrayPC = new string[]{
  434. "亮度",
  435. "对比度",};
  436. string[] sliderNameArrayPCKey = new string[]{
  437. "Brightness",
  438. "Contrast",};
  439. string[] sliderStrArrayPC = new string[]{
  440. "PU_BRIGHTNESS",
  441. "PU_CONTRAST"};
  442. //记录一个typeStr类型的数据存储操作对象
  443. ParamFloatValue pcBRIGHTNESS = new ParamFloatValue("ic_pc_PU_BRIGHTNESS", 0);
  444. ParamFloatValue pcCONTRAST = new ParamFloatValue("ic_pc_PU_CONTRAST", 0);
  445. public void initSliderPC()
  446. {
  447. for (int i = 0; i < sliderStrArrayPC.Length; i++)
  448. {
  449. string typeStr = sliderStrArrayPC[i];
  450. GameObject sliderObject = Instantiate(sliderPrefab, spawnPoint.transform);
  451. sliderObject.SetActive(true);
  452. sliderObject.name = typeStr;
  453. Slider slider = sliderObject.GetComponent<Slider>();
  454. Text textObj = sliderObject.transform.Find("text").GetComponent<Text>();
  455. Text titleTextObj = sliderObject.transform.Find("title").GetComponent<Text>();
  456. //titleTextObj.text = TextAutoLanguage2.GetTextByKey(sliderNameArrayPCKey[i]);// sliderNameArrayPC[i];//类型名字
  457. titleTextObj.gameObject.AddComponent<TextAutoLanguage2>().SetTextKey(sliderNameArrayPCKey[i]);
  458. if (slider != null)
  459. {
  460. slider.minValue = -1;
  461. slider.maxValue = 1;
  462. //记录一个typeStr类型的数据存储操作对象
  463. if (typeStr == "PU_BRIGHTNESS")
  464. {
  465. //获取设置默认存储的值
  466. float _Value = pcBRIGHTNESS.Get();
  467. infraredCameraHelper.SetBrightness(_Value);
  468. Debug.Log("[InfraredDemo] PC 设置 PU_BRIGHTNESS:" + typeStr + "默认值:" + _Value);
  469. slider.value = _Value;
  470. textObj.text = _Value + "";
  471. slider.onValueChanged.AddListener((newValue) =>
  472. {
  473. var _value = newValue;
  474. textObj.text = _value + "";
  475. infraredCameraHelper.SetBrightness(_value);
  476. pcBRIGHTNESS.Set(_value);
  477. });
  478. }
  479. else if (typeStr == "PU_CONTRAST")
  480. {
  481. //获取设置默认存储的值
  482. float _Value = pcCONTRAST.Get();
  483. infraredCameraHelper.SetContrast(_Value);
  484. Debug.Log("[InfraredDemo] PC 设置 PU_CONTRAST:" + typeStr + "默认值:" + _Value);
  485. slider.value = _Value;
  486. textObj.text = _Value + "";
  487. slider.onValueChanged.AddListener((newValue) =>
  488. {
  489. var _value = newValue;
  490. textObj.text = _value + "";
  491. infraredCameraHelper.SetContrast(_value);
  492. pcCONTRAST.Set(_value);
  493. });
  494. }
  495. }
  496. }
  497. }
  498. void resetPCData() {
  499. for (int i = 0; i < sliderStrArrayPC.Length; i++)
  500. {
  501. string typeStr = sliderStrArrayPC[i];
  502. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  503. Slider slider = trans.GetComponent<Slider>();
  504. Text textObj = trans.Find("text").GetComponent<Text>();
  505. //重置的值
  506. int _defValue = 0;
  507. if (typeStr == "PU_BRIGHTNESS")
  508. {
  509. _defValue = 0;
  510. infraredCameraHelper.SetBrightness(_defValue);
  511. }
  512. else if (typeStr == "PU_CONTRAST")
  513. {
  514. _defValue = 0;
  515. infraredCameraHelper.SetContrast(_defValue);
  516. }
  517. textObj.text = _defValue + "";
  518. slider.value = _defValue;
  519. }
  520. }
  521. #endregion
  522. void InitInfraredCamera()
  523. {
  524. if (_inited) return;
  525. _inited = true;
  526. //SDK创建
  527. if (infraredCameraHelper == null)
  528. {
  529. infraredCameraHelper = InfraredCameraHelper.GetInstance();
  530. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  531. infraredCameraHelper.CreateToPc();
  532. #endif
  533. #if UNITY_ANDROID
  534. //开始时候设置一个默认分辨率
  535. Debug.Log("开始时候的分辨率下标:" + resoution.Get());
  536. Vector2 resoutionVec2 = SetDefByIndex((int)resoution.Get());
  537. infraredCameraHelper.Create((int)resoutionVec2.x, (int)resoutionVec2.y);
  538. #endif
  539. infraredCameraHelper.OnPositionUpdate += (Vector2 point) =>
  540. {
  541. //跑九轴时候,不处理这里位置
  542. if (AimHandler.ins && AimHandler.ins.bRuning9Axis()) return;
  543. if (Camera.main == null) return;
  544. Ray ray = Camera.main.ScreenPointToRay(point);
  545. Vector3 rayEndPoint = ray.GetPoint(200);
  546. Quaternion quat = Quaternion.LookRotation(rayEndPoint - Camera.main.transform.position);
  547. // 挑战场景中其相机的父级有旋转,需要换算
  548. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "GameChallenge")
  549. {
  550. quat = Quaternion.AngleAxis(-180, Vector3.up) * quat;
  551. }
  552. if (CameraToLook.ins != null) CameraToLook.ins.localRotation = quat;
  553. if (SB_EventSystem.ins) SB_EventSystem.ins.MoveSimulateMouseByInfrared(point);
  554. //移动目标游戏
  555. if (GeneratingTarget.gm != null) GeneratingTarget.gm.shootingEvent.OnPositionUpdate(point);
  556. };
  557. infraredCameraHelper.OnUVCIsReady += (UVCManager.CameraInfo camera) =>
  558. {
  559. //生成控制摄像机的参数滑条
  560. Debug.Log("初始化摄像机!");
  561. //可以操作按钮
  562. mBtnSee.interactable = true;
  563. //mBtnSee.transform.Find("Text").GetComponent<Text>().text = "调试红外";
  564. updateBtnSee();
  565. //延迟重新设置一次分辨率
  566. StartCoroutine(delayInitOhterInfo(camera));
  567. };
  568. //屏幕变化时候
  569. infraredCameraHelper.OnUVCPosUpdate += (list) =>
  570. {
  571. Debug.Log("OnUVCPosUpdate list.Count" + list.Count);
  572. SetLocatePointsToCameraRender(list, 1, 1);
  573. //InfraredScreenPositioningView 页面
  574. if (list.Count == 4)
  575. {
  576. FindObjectOfType<InfraredScreenPositioningView>()?.SyncScreenPosition();
  577. }
  578. else {
  579. FindObjectOfType<InfraredScreenPositioningView>()?.onReset();
  580. }
  581. };
  582. }
  583. }
  584. private void updateDropdownResolution2()
  585. {
  586. string[] resolutions = {
  587. TextAutoLanguage2.GetTextByKey("HighResolution"),
  588. TextAutoLanguage2.GetTextByKey("LowResolution")
  589. };
  590. // 清除默认选项
  591. _dropdownResolution2.ClearOptions();
  592. // 将分辨率字符串数组转换为 Dropdown 选项
  593. _dropdownResolution2.AddOptions(new List<string>(resolutions));
  594. }
  595. /// <summary>
  596. /// 初始化一些游戏配置
  597. /// </summary>
  598. /// <returns></returns>
  599. IEnumerator delayInitOhterInfo(UVCManager.CameraInfo camera) {
  600. //yield return new WaitForSeconds(1f);
  601. yield return new WaitForEndOfFrame();
  602. #if UNITY_ANDROID
  603. initSlider(camera);
  604. camera.GetResolutionsStrs();
  605. // 默认设置
  606. updateDropdownResolution2();
  607. int index = (int)resoution.Get();
  608. _dropdownResolution2.value = index;
  609. _dropdownResolution2.RefreshShownValue();
  610. // 处理 Dropdown 的选择变化
  611. _dropdownResolution2.onValueChanged.AddListener(index => { OnResolutionChanged(index); });
  612. #endif
  613. #if UNITY_STANDALONE_WIN || UNITY_EDITOR
  614. initSliderPC();
  615. //隐藏不需要的设置
  616. _btnAdjusting.transform.Find("Text").GetComponent<Text>().text = "调整PC参数";
  617. _dropdownResolution.gameObject.SetActive(false);
  618. _dropdownResolution2.gameObject.SetActive(false);
  619. #endif
  620. //震动阈值
  621. SetShakeFilterValue(shakeFilterValue.Get());
  622. _sliderShakeFilter.onValueChanged.AddListener(SetShakeFilterValue);
  623. //功能按钮
  624. //重置触发测试
  625. _btnReset.onClick.AddListener(OnClick_Reset);
  626. _btnScreenLocateManual.onClick.AddListener(OnClick_ScreenLocateManual);
  627. _btnScreenLocateManualAuto.onClick.AddListener(OnClick_ScreenLocateManualAuto);
  628. SetCaptureValue(captureValue.Get());
  629. _sliderCapture.onValueChanged.AddListener(SetCaptureValue);
  630. SetDelayValue(delayValue.Get());
  631. _sliderDelay.onValueChanged.AddListener(SetDelayValue);
  632. //绘制线段
  633. SetLineWidth(lineWidth.Get());
  634. _sliderLineWidth.onValueChanged.AddListener(SetLineWidth);
  635. SetFanWidth(fanWidth.Get());
  636. _fanWidth.onValueChanged.AddListener(SetFanWidth);
  637. //检测红外亮度阈值
  638. SetInfraredFilterValue(infraredFileterValue.Get());
  639. _infraredFilter.onValueChanged.AddListener(SetInfraredFilterValue);
  640. //野鸭设置初始关卡
  641. int defLevel = (int)duckHunterLevel.Get() - 1 < 0 ? 0 : (int)duckHunterLevel.Get() - 1;
  642. _duckLevelDropdown.SetValueWithoutNotify(defLevel);
  643. _duckLevelDropdown.onValueChanged.AddListener(SetDuckLevelTest);
  644. }
  645. /// <summary>
  646. /// 初始化时候获取设置和存储来控制显示准心
  647. /// </summary>
  648. /// <returns></returns>
  649. public bool bInitCrosshairShow() {
  650. return UserSettings.ins.openCrossHair && getCrosshairValue() == 1 ? true : false;
  651. }
  652. /// <summary>
  653. /// 获取弓箭准心存储值
  654. /// </summary>
  655. /// <returns></returns>
  656. public int getCrosshairValue() {
  657. return (int)crosshairValue.Get();
  658. }
  659. public void setCrosshairValue(bool bshow) {
  660. crosshairValue.Set(bshow ? 1 : 0);
  661. }
  662. public void onStartPreview()
  663. {
  664. infraredCameraHelper.onStartPreview();
  665. }
  666. public void onStopPreview()
  667. {
  668. infraredCameraHelper.onStopPreview();
  669. }
  670. /// <summary>
  671. /// 获取并且初始化一次记录点的数据
  672. /// </summary>
  673. /// <returns></returns>
  674. IEnumerator initScreenLocateManual()
  675. {
  676. yield return new WaitForSeconds(1f);
  677. infraredCameraHelper.InitScreenLocateManual();
  678. }
  679. IEnumerator RestartOrKillApp()
  680. {
  681. yield return new WaitForSeconds(0.3f);
  682. if (Application.isEditor) yield break;
  683. if (Application.platform == RuntimePlatform.Android)
  684. {
  685. using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  686. {
  687. const int kIntent_FLAG_ACTIVITY_CLEAR_TASK = 0x00008000;
  688. const int kIntent_FLAG_ACTIVITY_NEW_TASK = 0x10000000;
  689. var currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  690. var pm = currentActivity.Call<AndroidJavaObject>("getPackageManager");
  691. var intent = pm.Call<AndroidJavaObject>("getLaunchIntentForPackage", Application.identifier);
  692. intent.Call<AndroidJavaObject>("setFlags", kIntent_FLAG_ACTIVITY_NEW_TASK | kIntent_FLAG_ACTIVITY_CLEAR_TASK);
  693. currentActivity.Call("startActivity", intent);
  694. currentActivity.Call("finish");
  695. var process = new AndroidJavaClass("android.os.Process");
  696. int pid = process.CallStatic<int>("myPid");
  697. process.CallStatic("killProcess", pid);
  698. }
  699. }
  700. else {
  701. // 获取当前应用程序的可执行文件路径
  702. string executablePath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
  703. // 重启应用程序
  704. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
  705. {
  706. FileName = executablePath,
  707. UseShellExecute = false
  708. };
  709. System.Diagnostics.Process.Start(startInfo);
  710. // 退出当前应用程序
  711. Application.Quit();
  712. };
  713. }
  714. void UpdateInfraredCamera()
  715. {
  716. if (!_visiable) return;
  717. if (!_inited) return;
  718. if (infraredCameraHelper == null) return;
  719. //渲染相机画面
  720. //_cameraRender.texture = infraredCameraHelper.GetCameraTexture();
  721. //_cameraRender.material = infraredCameraHelper.GetCameraMaterial();
  722. if (ScreenLocate.Main.getUVCTexture)
  723. {
  724. if (isFullscreen || ScreenLocate.Main.OutputTextures[4] == null)
  725. {
  726. if (_cameraRender.texture == null || ScreenLocate.Main.getUVCTexture.GetNativeTexturePtr() != _cameraRender.texture.GetNativeTexturePtr())
  727. _cameraRender.texture = ScreenLocate.Main.getUVCTexture;
  728. //ScreenLocate.Main.OutputTextures[4] == null 并且 摄像机缩小的情况下。显示适配线段
  729. if (!isFullscreen) SetAllToggle(true);
  730. }
  731. else if (ScreenLocate.Main.OutputTextures[4] != null)
  732. {
  733. //缩小的情况下。显示黑白色差的合成图
  734. if (_cameraRender.texture == null || ScreenLocate.Main.OutputTextures[4] != null && ScreenLocate.Main.OutputTextures[4].GetNativeTexturePtr() != _cameraRender.texture.GetNativeTexturePtr())
  735. _cameraRender.texture = ScreenLocate.Main.OutputTextures[4];
  736. //底图缩小的情况下。不显示适配线段,并且隐藏points
  737. if (!isFullscreen) {
  738. if (showPoints) {
  739. Transform pointsTF2 = _cameraRender.transform.Find("Points");
  740. for (int i = 0; i < pointsTF2.childCount; i++)
  741. {
  742. Transform pointTF = pointsTF2.GetChild(i);
  743. pointTF.gameObject.SetActive(false);
  744. showPoints = false;
  745. }
  746. }
  747. SetAllToggle(false);
  748. }
  749. }
  750. //_cameraRender.SetNativeSize();
  751. // _MaintainAspectRatio.AdjustSize();
  752. }
  753. //在相机画面显示准心
  754. if (ScreenLocate.Main)
  755. {
  756. var _sl = ScreenLocate.Main;
  757. var buffer = _sl.infraredSpotBuffer;
  758. if (buffer != null)
  759. {
  760. for (int i = 0; i < buffer.Length; i++)
  761. {
  762. if (buffer[i].CameraLocation != null)
  763. {
  764. //添加一个偏移量,使得最后输出的准心是指向正中心
  765. Vector2 newPoint2 = _sl.GetOffsetCameraLocation(buffer[i].CameraLocation.Value);
  766. // 检测到光点
  767. var pos = newPoint2.pixelToLocalPosition_AnchorCenter(_sl.mUVCCameraInfo.Size, _cameraRender.rectTransform.rect);
  768. _crosshairsInCamera[i].gameObject.SetActive(true);
  769. _crosshairsInCamera[i].anchoredPosition = pos;
  770. }
  771. else
  772. _crosshairsInCamera[i].gameObject.SetActive(false);
  773. }
  774. }
  775. //渲染固定摄像机分辨率
  776. SetCalibrationFixedText(_sl.mUVCCameraInfo.CurrentCalibrationResolution);
  777. //渲染摄像机大小
  778. if (_sl.mUVCCameraInfo != null) SetCameraSizeText(_sl.mUVCCameraInfo.Size);
  779. //渲染摄像机识别点位置
  780. SetQuadUnityVectorListText(_sl.PrintVector2List(ScreenLocate.quadUnityVectorList));
  781. }
  782. }
  783. bool showPoints = false;
  784. public void SetLocatePointsToCameraRender(List<Vector2> points, float w, float h)
  785. {
  786. Transform pointsTF2 = _cameraRender.transform.Find("Points");
  787. if (pointsTF2.childCount == points.Count)
  788. {
  789. Vector2 texSize = new Vector2(w, h);
  790. for (int i = 0; i < pointsTF2.childCount; i++)
  791. {
  792. Transform pointTF = pointsTF2.GetChild(i);
  793. Vector2 pos = points[i];
  794. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(texSize, _cameraRender.rectTransform.rect);
  795. pointTF.gameObject.SetActive(true);
  796. showPoints = true;
  797. }
  798. }
  799. else {
  800. for (int i = 0; i < pointsTF2.childCount; i++)
  801. {
  802. Transform pointTF = pointsTF2.GetChild(i);
  803. pointTF.gameObject.SetActive(false);
  804. showPoints = false;
  805. }
  806. }
  807. //绘制数据
  808. DrawTestLine();
  809. }
  810. #region 测试数据绘制
  811. [SerializeField] private Toggle curToggle;
  812. [SerializeField] private Toggle autoToggle; // 控制 AutoUILineGenerator 的 Toggle
  813. [SerializeField] private Toggle semiAutoToggle; // 控制 SemiAutoUILineGenerator 的 Toggle
  814. [SerializeField] private Toggle manualToggle; // 控制 ManualUILineGenerator 的 Toggle
  815. [SerializeField] LineGenerator CurUILineGenerator;//当前结果屏幕线条
  816. [SerializeField] LineGenerator AutoUILineGenerator;//第一个结果屏幕线条
  817. [SerializeField] LineGenerator SemiAutoUILineGenerator;//第二个结果屏幕线条
  818. [SerializeField] LineGenerator ManualUILineGenerator;//第三个结果屏幕线条
  819. [SerializeField] private Dropdown dropdown; // 引用场景中的 Dropdown 组件
  820. [SerializeField] RawImage _cameraRenderTest5;
  821. [SerializeField] RawImage _cameraRenderTest6;
  822. void DrawTestLine()
  823. {
  824. Vector2 texSize = ScreenLocate.Main.getUVCCameraInfoSize;
  825. // 判断 texSize 是否为有效值
  826. if (texSize == Vector2.zero)
  827. {
  828. Debug.LogWarning("Texture size is zero, cannot proceed with line drawing.");
  829. return;
  830. }
  831. // 设置三个屏幕四边形和线条生成器
  832. QuadrilateralInCamera[] screenQuads = new QuadrilateralInCamera[] {
  833. ScreenLocate.Main.ScreenIdentification?.QuadAuto,
  834. ScreenLocate.Main.ScreenIdentification?.QuadSemiAuto,
  835. ScreenLocate.Main.ScreenIdentification?.QuadManual
  836. };
  837. LineGenerator[] lineGenerators = new LineGenerator[] {
  838. AutoUILineGenerator,
  839. SemiAutoUILineGenerator,
  840. ManualUILineGenerator
  841. };
  842. for (int i = 0; i < screenQuads.Length; i++)
  843. {
  844. if (screenQuads[i] == null)
  845. {
  846. Debug.LogWarning($"Screen quadrilateral {i} is null, skipping this line.");
  847. continue;
  848. }
  849. if (lineGenerators[i] == null)
  850. {
  851. Debug.LogWarning($"Line generator {i} is null, skipping this line.");
  852. continue;
  853. }
  854. lineGenerators[i].Points = ConvertQuadToPoints(screenQuads[i], texSize);
  855. }
  856. // 清除旧选项并填充新的选项
  857. dropdown.ClearOptions();
  858. List<string> options = new List<string>();
  859. //for (int i = 0; i < screenQuads.Length; i++)
  860. //{
  861. // options.Add($"Quad {i + 1}");
  862. //}
  863. //options.Add("自动识别");
  864. //options.Add("半自动识别");
  865. //options.Add("手动识别");
  866. options.Add(TextAutoLanguage2.GetTextByKey("AutoRecognition")); // "自动识别"
  867. options.Add(TextAutoLanguage2.GetTextByKey("SemiAutoRecognition")); // "半自动识别"
  868. options.Add(TextAutoLanguage2.GetTextByKey("ManualRecognition")); // "手动识别"
  869. dropdown.AddOptions(options);
  870. // 添加监听器来处理选择事件
  871. dropdown.onValueChanged.AddListener((index) => {
  872. OnDropdownValueChanged(index, screenQuads);
  873. });
  874. if (screenQuads[0] != null || screenQuads[1] != null)
  875. {
  876. Texture texImage6 = ScreenLocate.Main.OutputTextures[5];
  877. if (texImage6 != null) _cameraRenderTest6.texture = texImage6;
  878. }
  879. if (ScreenLocate.quadUnityVectorList.Count == 4) {
  880. //绘制白色线段
  881. var lo = new Vector2(-0.5f, -0.5f);
  882. CurUILineGenerator.Points = new Vector2[4] {
  883. 2 * (ScreenLocate.quadUnityVectorList[0] + lo),
  884. 2 * (ScreenLocate.quadUnityVectorList[1] + lo),
  885. 2 * (ScreenLocate.quadUnityVectorList[3] + lo),
  886. 2 * (ScreenLocate.quadUnityVectorList[2] + lo) };
  887. }
  888. }
  889. // 当下拉框选项更改时调用的方法
  890. private void OnDropdownValueChanged(int index, QuadrilateralInCamera[] screenQuads)
  891. {
  892. if (index >= 0 && index < screenQuads.Length)
  893. {
  894. if (screenQuads[index] != null)
  895. {
  896. QuadrilateralInCamera selectedQuad = screenQuads[index];
  897. Debug.Log($"Selected Quad {index + 1}: {selectedQuad}");
  898. CurUILineGenerator.Points = ConvertQuadToPoints(selectedQuad, ScreenLocate.Main.getUVCCameraInfoSize);
  899. }
  900. else
  901. {
  902. Debug.LogWarning($"Selected Quad {index + 1} is null.");
  903. }
  904. }
  905. else
  906. {
  907. Debug.LogWarning("所选索引超出范围!");
  908. }
  909. }
  910. /// <summary>
  911. /// 提取转换四边形的方法
  912. /// </summary>
  913. /// <param name="quad"></param>
  914. /// <param name="texSize"></param>
  915. /// <returns></returns>
  916. public Vector2[] ConvertQuadToPoints(QuadrilateralInCamera quad, Vector2 texSize)
  917. {
  918. if (quad == null || quad.Quad == null || quad.Quad.Count < 4)
  919. {
  920. Debug.LogWarning("Invalid quadrilateral data provided.");
  921. return new Vector2[0]; // 返回空数组以避免异常
  922. }
  923. Vector2 offset = new Vector2(-0.5f, -0.5f); // 假设 'lo' 是某个已定义的偏移量
  924. return new Vector2[4] {
  925. 2 * (new Vector2(quad.Quad[0].x / texSize.x, quad.Quad[0].y / texSize.y) + offset),
  926. 2 * (new Vector2(quad.Quad[1].x / texSize.x, quad.Quad[1].y / texSize.y) + offset),
  927. 2 * (new Vector2(quad.Quad[3].x / texSize.x, quad.Quad[3].y / texSize.y) + offset),
  928. 2 * (new Vector2(quad.Quad[2].x / texSize.x, quad.Quad[2].y / texSize.y) + offset)
  929. };
  930. }
  931. public Vector2[] ConvertPointsToCoordinates(Vector2[] points, Vector2 texSize, Vector2 pivot)
  932. {
  933. if (points == null || points.Length < 1)
  934. {
  935. Debug.LogWarning("Invalid point data provided.");
  936. return new Vector2[0]; // 返回空数组以避免异常
  937. }
  938. Vector2 offset = new Vector2(-0.5f, -0.5f); // 将坐标从中心对齐到左下角对齐
  939. List<Vector2> transformedPoints = new List<Vector2>();
  940. // 遍历所有传入的点进行转换
  941. foreach (var point in points)
  942. {
  943. // 转换每个点,考虑 texSize 和 pivot 偏移
  944. Vector2 transformedPoint = 2 * ((point / texSize) + offset + pivot);
  945. transformedPoints.Add(transformedPoint);
  946. }
  947. return transformedPoints.ToArray();
  948. }
  949. void initToggle()
  950. {
  951. // 监听 Toggle 值的变化
  952. if (curToggle != null)
  953. curToggle.onValueChanged.AddListener(isOn => ToggleLineGenerator(CurUILineGenerator, isOn));
  954. // 监听 Toggle 值的变化
  955. if (autoToggle != null)
  956. autoToggle.onValueChanged.AddListener(isOn => ToggleLineGenerator(AutoUILineGenerator, isOn));
  957. if (semiAutoToggle != null)
  958. semiAutoToggle.onValueChanged.AddListener(isOn => ToggleLineGenerator(SemiAutoUILineGenerator, isOn));
  959. if (manualToggle != null)
  960. manualToggle.onValueChanged.AddListener(isOn => ToggleLineGenerator(ManualUILineGenerator, isOn));
  961. }
  962. // 控制 LineGenerator 的启用/禁用
  963. private void ToggleLineGenerator(LineGenerator lineGenerator, bool isOn)
  964. {
  965. if (lineGenerator != null && lineGenerator.enabled != isOn)
  966. {
  967. lineGenerator.enabled = isOn;
  968. Debug.Log($"{lineGenerator.name} is now {(isOn ? "enabled" : "disabled")}");
  969. }
  970. }
  971. /// <summary>
  972. /// 设置全部显示
  973. /// </summary>
  974. /// <param name="value"></param>
  975. private void SetAllToggle(bool value) {
  976. ToggleLineGenerator(CurUILineGenerator, value);
  977. ToggleLineGenerator(AutoUILineGenerator, value);
  978. ToggleLineGenerator(SemiAutoUILineGenerator, value);
  979. ToggleLineGenerator(ManualUILineGenerator, value);
  980. }
  981. /// <summary>
  982. /// 放大时候同步一次toggle
  983. /// </summary>
  984. private void SyncAllToggle()
  985. {
  986. ToggleLineGenerator(CurUILineGenerator, curToggle.isOn);
  987. ToggleLineGenerator(AutoUILineGenerator, autoToggle.isOn);
  988. ToggleLineGenerator(SemiAutoUILineGenerator, semiAutoToggle.isOn);
  989. ToggleLineGenerator(ManualUILineGenerator, manualToggle.isOn);
  990. }
  991. // 下拉菜单用于显示纹理名称
  992. public Dropdown textureDropdown;
  993. // 按钮用于导出选中的纹理
  994. public Button exportSelectedButton;
  995. // 按钮用于导出所有纹理
  996. public Button exportAllButton;
  997. // 定义纹理名称
  998. string[] textureNames;
  999. /// <summary>
  1000. /// 更新一下菜单
  1001. /// </summary>
  1002. public void UpdateLanguage() {
  1003. string[] _textureNames = {
  1004. TextAutoLanguage2.GetTextByKey("ScreenAOriginal"), // "A屏幕原图"
  1005. TextAutoLanguage2.GetTextByKey("SemiAutoSegmentB"), // "B半自动识别线段"
  1006. TextAutoLanguage2.GetTextByKey("BlackWhiteContrastC"), // "C黑白色差"
  1007. TextAutoLanguage2.GetTextByKey("RecognitionResultD"), // "D识别结果"
  1008. TextAutoLanguage2.GetTextByKey("ColorContrastOverlayE"), // "E色差叠加识别结果"
  1009. TextAutoLanguage2.GetTextByKey("SemiAutoAlternativeF") // "F半自动备选线段"
  1010. };
  1011. textureNames = _textureNames;
  1012. // 设置下拉菜单的选项
  1013. textureDropdown.ClearOptions();
  1014. textureDropdown.AddOptions(new System.Collections.Generic.List<string>(textureNames));
  1015. }
  1016. void initExport() {
  1017. UpdateLanguage();
  1018. // 添加导出选中纹理的按钮点击事件
  1019. exportSelectedButton.onClick.AddListener(() => SaveTextureToLocal(textureDropdown.value));
  1020. // 添加导出所有纹理的按钮点击事件
  1021. exportAllButton.onClick.AddListener(SaveAllTexturesToLocal);
  1022. }
  1023. /// <summary>
  1024. /// 导出所有纹理
  1025. /// </summary>
  1026. public void SaveAllTexturesToLocal()
  1027. {
  1028. for (int i = 0; i < textureNames.Length; i++)
  1029. {
  1030. SaveTextureToLocal(i); // 调用之前定义的保存方法
  1031. }
  1032. }
  1033. /// <summary>
  1034. /// 导出图片
  1035. /// </summary>
  1036. /// <param name="index"></param>
  1037. public void SaveTextureToLocal(int index)
  1038. {
  1039. Texture2D[] outputTexture2D = ScreenLocate.Main.OutputTextures;
  1040. if (outputTexture2D == null || index >= outputTexture2D.Length || outputTexture2D[index] == null)
  1041. {
  1042. Debug.LogError("Invalid texture index or texture is null.index:"+ index);
  1043. return;
  1044. }
  1045. // 定义保存路径
  1046. string time = DateTime.Now.ToString("yyyyMMdd_HHmmss");
  1047. string baseName = $"{time}_Texture_{index}";
  1048. // 检查索引是否在命名数组内
  1049. string fileName = index < textureNames.Length ? textureNames[index] : $"Texture_{index}";
  1050. #if UNITY_ANDROID && !UNITY_EDITOR_WIN
  1051. string path = System.IO.Path.Combine("/storage/emulated/0/Download", $"{baseName}_{fileName}.png"); //public
  1052. #else
  1053. string path = System.IO.Path.Combine(Application.persistentDataPath, $"{baseName}_{fileName}.png");
  1054. #endif
  1055. // 编码纹理为 PNG 格式
  1056. byte[] bytes = outputTexture2D[index].EncodeToPNG();
  1057. // 将字节写入文件
  1058. System.IO.File.WriteAllBytes(path, bytes);
  1059. Debug.Log($"Texture saved as: {path}");
  1060. }
  1061. //下面是处理对象放大缩小
  1062. public RectTransform cameraObj1; // 拖拽你的 CameraSize1 对象到此字段
  1063. public Button toggleButton; // 缩小时使用的按钮
  1064. private Vector2 originalSize;
  1065. private Vector2 originalAnchoredPosition; // 使用 anchoredPosition 代替 localPosition
  1066. private Vector2 originalAnchorMin;
  1067. private Vector2 originalAnchorMax;
  1068. private int originalSiblingIndex; // 保存初始的层级索引
  1069. private bool isFullscreen = false;
  1070. /// <summary>
  1071. /// 测试效果屏幕数据初始化
  1072. /// </summary>
  1073. void InitFullScreen() {
  1074. if (cameraObj1 != null)
  1075. {
  1076. // 保存初始大小、位置和层级索引
  1077. originalSize = cameraObj1.sizeDelta;
  1078. originalAnchoredPosition = cameraObj1.anchoredPosition;
  1079. originalAnchorMin = cameraObj1.anchorMin; // 记录初始 anchorMin
  1080. originalAnchorMax = cameraObj1.anchorMax; // 记录初始 anchorMax
  1081. originalSiblingIndex = cameraObj1.GetSiblingIndex();
  1082. // 为 cameraObj1 添加点击事件
  1083. cameraObj1.GetComponent<Button>().onClick.AddListener(ToggleFullscreen);
  1084. toggleButton.transform.parent.gameObject.SetActive(false);
  1085. }
  1086. if (toggleButton != null)
  1087. {
  1088. // 为按钮添加点击事件以恢复大小
  1089. toggleButton.onClick.AddListener(RestoreSize);
  1090. }
  1091. }
  1092. void ToggleFullscreen()
  1093. {
  1094. if (cameraObj1 == null) return;
  1095. if (!isFullscreen)
  1096. {
  1097. // 设置锚点为全屏,调整位置和偏移量
  1098. cameraObj1.anchorMin = Vector2.zero;
  1099. cameraObj1.anchorMax = Vector2.one;
  1100. cameraObj1.offsetMin = Vector2.zero;
  1101. cameraObj1.offsetMax = Vector2.zero;
  1102. cameraObj1.anchoredPosition = Vector3.zero;
  1103. cameraObj1.SetSiblingIndex(cameraObj1.parent.childCount - 1); // 设置为最上层
  1104. //刷新一次点
  1105. SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList, 1, 1);
  1106. toggleButton.transform.parent.gameObject.SetActive(true);
  1107. isFullscreen = true;
  1108. //放大情况根据toggle来显示
  1109. SyncAllToggle();
  1110. }
  1111. }
  1112. void RestoreSize()
  1113. {
  1114. if (cameraObj1 == null || !isFullscreen) return;
  1115. // 恢复锚点和偏移量
  1116. cameraObj1.anchorMin = originalAnchorMin; // 使用在 Start() 中记录的初始锚点
  1117. cameraObj1.anchorMax = originalAnchorMax; // 使用在 Start() 中记录的初始锚点
  1118. cameraObj1.sizeDelta = originalSize;
  1119. cameraObj1.anchoredPosition = originalAnchoredPosition; // 恢复为初始 anchoredPosition
  1120. cameraObj1.SetSiblingIndex(originalSiblingIndex); // 恢复到原本的层级索引
  1121. //刷新一次点
  1122. SetLocatePointsToCameraRender(ScreenLocate.quadUnityVectorList, 1, 1);
  1123. toggleButton.transform.parent.gameObject.SetActive(false);
  1124. isFullscreen = false;
  1125. }
  1126. #endregion
  1127. public void SetLocatePointsToCameraRenderTest(List<Vector2> points, float w, float h)
  1128. {
  1129. Transform pointsTF2 = _cameraRender.transform.Find("PointsTest");
  1130. if (pointsTF2.childCount == points.Count)
  1131. {
  1132. Vector2 texSize = new Vector2(w, h);
  1133. for (int i = 0; i < pointsTF2.childCount; i++)
  1134. {
  1135. Transform pointTF = pointsTF2.GetChild(i);
  1136. Vector2 pos = points[i];
  1137. pointTF.localPosition = pos.pixelToLocalPosition_AnchorCenter(texSize, _cameraRender.rectTransform.rect);
  1138. pointTF.gameObject.SetActive(true);
  1139. }
  1140. }
  1141. else
  1142. {
  1143. for (int i = 0; i < pointsTF2.childCount; i++)
  1144. {
  1145. Transform pointTF = pointsTF2.GetChild(i);
  1146. pointTF.gameObject.SetActive(false);
  1147. }
  1148. }
  1149. }
  1150. void SetShakeFilterValue(float v)
  1151. {
  1152. shakeFilterValue.Set(v);
  1153. _sliderShakeFilter.SetValueWithoutNotify(shakeFilterValue.Get());
  1154. _sliderShakeFilter.transform.Find("Value").GetComponent<Text>().text = shakeFilterValue.Get().ToString("f1");
  1155. infraredCameraHelper.SetShakeFilterValue(shakeFilterValue.Get());
  1156. }
  1157. /// <summary>
  1158. /// 重新设置分辨率
  1159. /// </summary>
  1160. /// <param name="index"></param>
  1161. public void resolutionRestartApp(int index)
  1162. {
  1163. #if UNITY_ANDROID
  1164. //StartCoroutine(RestartOrKillApp());
  1165. //获取第一个分辨率设置后设置size
  1166. switch (index)
  1167. {
  1168. case 0:
  1169. infraredCameraHelper.SetHighCameraResolution();
  1170. resoution.Set(index);
  1171. _dropdownResolution2.SetValueWithoutNotify(index);
  1172. break;
  1173. case 1:
  1174. infraredCameraHelper.SetLowCameraResolution();
  1175. resoution.Set(index);
  1176. _dropdownResolution2.SetValueWithoutNotify(index);
  1177. break;
  1178. }
  1179. #endif
  1180. }
  1181. /// <summary>
  1182. /// 初始化时候,设置的值
  1183. /// </summary>
  1184. /// <param name="index"></param>
  1185. public Vector2 SetDefByIndex(int index = 0)
  1186. {
  1187. //SetResolutionNew 里面对应的下标
  1188. Vector2 vec2 = new Vector2(320, 240);
  1189. switch (index)
  1190. {
  1191. case 0:
  1192. vec2 = new Vector2(320, 240);
  1193. break;
  1194. case 1:
  1195. vec2 = new Vector2(160, 120);
  1196. break;
  1197. }
  1198. return vec2;
  1199. }
  1200. /// <summary>
  1201. /// 当 Dropdown 选择变化时的处理方法
  1202. /// </summary>
  1203. /// <param name="OnResolutionChanged">320x240</param>
  1204. private void OnResolutionChanged(int index)
  1205. {
  1206. #if UNITY_ANDROID
  1207. Debug.Log("Selected Resolution: " + index);
  1208. // 将选定的分辨率转换为实际的宽度和高度
  1209. // 并且需要判断是否存在最低分辨率
  1210. if (index == 1 && currentCameraInfo.ContainsResulutionKey("160x120"))
  1211. {
  1212. // 在这里你可以根据实际需要设置屏幕分辨率或进行其他操作
  1213. infraredCameraHelper.SetLowCameraResolution();
  1214. // 保存选择的分辨率到 PlayerPrefs
  1215. resoution.Set(index);
  1216. }
  1217. else if(index == 0){
  1218. //如果是0就 使用高分辨率
  1219. infraredCameraHelper.SetHighCameraResolution();
  1220. // 保存选择的分辨率到 PlayerPrefs
  1221. resoution.Set(index);
  1222. }
  1223. #endif
  1224. }
  1225. /// <summary>
  1226. /// 野鸭关卡测试初始值
  1227. /// </summary>
  1228. /// <param name="optionIndex"></param>
  1229. public void SetDuckLevelTest(int optionIndex)
  1230. {
  1231. //选择下标+1
  1232. int v = optionIndex + 1;
  1233. duckHunterLevel.Set(v);
  1234. int defLevel = (int)duckHunterLevel.Get() - 1 < 0 ? 0 : (int)duckHunterLevel.Get() - 1;
  1235. _duckLevelDropdown.SetValueWithoutNotify(defLevel);
  1236. //_duckLevelDropdown.transform.Find("Value").GetComponent<Text>().text = defLevel+"";
  1237. }
  1238. void OnClick_Reset()
  1239. {
  1240. //SetBrightness(1);
  1241. //SetSaturation(1);
  1242. //SetContrast(1);
  1243. SetShakeFilterValue(6);
  1244. SetLineWidth(4);
  1245. SetFanWidth(90);
  1246. SetInfraredFilterValue(0.8f);
  1247. SetCaptureValue(30.0f);
  1248. SetDelayValue(30.0f);
  1249. //重置为第一关
  1250. SetDuckLevelTest(0);
  1251. }
  1252. public void OnClickClearAll() {
  1253. PlayerPrefs.DeleteAll();
  1254. // 保存更改
  1255. PlayerPrefs.Save();
  1256. StartCoroutine(RestartOrKillApp());
  1257. }
  1258. void OnClick_ScreenLocateManual()
  1259. {
  1260. bool bEnter = true;
  1261. if (BluetoothAim.ins && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  1262. {
  1263. if (BluetoothAim.ins.isMainConnectToInfraredDevice() || BluetoothAim.ins.isMainConnectToGun())
  1264. {
  1265. bEnter = true;
  1266. }
  1267. else
  1268. {
  1269. bEnter = false;
  1270. //只有红外设备才能进行屏幕识别
  1271. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("Connect_Tip_Infrared"));
  1272. }
  1273. }
  1274. if (bEnter)
  1275. {
  1276. ViewManager2.ShowView(ViewManager2.Path_InfraredScreenPositioningView);
  1277. FindObjectOfType<InfraredScreenPositioningView>().enterFromInfraredDemo = true;
  1278. }
  1279. }
  1280. void OnClick_ScreenLocateManualAuto()
  1281. {
  1282. bool bEnter = true;
  1283. if (BluetoothAim.ins && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  1284. {
  1285. if (BluetoothAim.ins.isMainConnectToInfraredDevice() || BluetoothAim.ins.isMainConnectToGun())
  1286. {
  1287. bEnter = true;
  1288. }
  1289. else
  1290. {
  1291. bEnter = false;
  1292. //只有红外设备才能进行屏幕识别
  1293. PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("Connect_Tip_Infrared"));
  1294. }
  1295. }
  1296. if (bEnter) {
  1297. ResetCenterOffset();
  1298. ResetPositioningData();
  1299. infraredCameraHelper.EnterScreenLocateManualAuto();
  1300. }
  1301. }
  1302. public void OnClick_SetAdjustPointsOffset()
  1303. {
  1304. var _sl = ScreenLocate.Main;
  1305. var buffer = _sl.infraredSpotBuffer;
  1306. if (buffer != null)
  1307. {
  1308. for (int i = 0; i < buffer.Length; i++)
  1309. {
  1310. if (buffer[i].CameraLocation != null)
  1311. {
  1312. Debug.Log("CameraLocation:"+buffer[i].CameraLocation.Value);
  1313. var centerOffset = infraredCameraHelper.GetCenterOffset(buffer[i].CameraLocation.Value, "CameraLocation");
  1314. Debug.Log("CenterOffset: " + centerOffset);
  1315. var uvCenterOffset = infraredCameraHelper.GetCenterOffset(buffer[i].ScreenUV.Value, "ScreenUV");
  1316. Debug.Log("UvCenterOffset: " + uvCenterOffset);
  1317. //如果是新手教程场景里面的校准
  1318. InfraredGuider infraredGuiderObj = FindObjectOfType<InfraredGuider>();
  1319. if (infraredGuiderObj != null)
  1320. {
  1321. GameObject infraredGuiderGameObject = infraredGuiderObj.gameObject;
  1322. infraredGuiderGameObject.GetComponent<InfraredGuider>().SetTitleAfterCalibration();
  1323. }
  1324. }
  1325. }
  1326. }
  1327. }
  1328. /// <summary>
  1329. /// 重置中心点
  1330. /// </summary>
  1331. public void ResetCenterOffset() {
  1332. infraredCameraHelper.ResetCenterOffset();
  1333. }
  1334. /// <summary>
  1335. /// 撤销中心点
  1336. /// </summary>
  1337. public void RevokeCenterOffset()
  1338. {
  1339. infraredCameraHelper.RevokeCenterOffset();
  1340. }
  1341. /// <summary>
  1342. /// 重置定位数据
  1343. /// </summary>
  1344. public void ResetPositioningData(bool bSyncLocal = false) {
  1345. infraredCameraHelper.ClearInfraredPositioningData(bSyncLocal);
  1346. }
  1347. void SetCaptureValue(float v)
  1348. {
  1349. captureValue.Set(v);
  1350. _sliderCapture.SetValueWithoutNotify(captureValue.Get());
  1351. _sliderCapture.transform.Find("Value").GetComponent<Text>().text = captureValue.Get().ToString("f1");
  1352. infraredCameraHelper.SetCapture((int)captureValue.Get());
  1353. }
  1354. void SetDelayValue(float v)
  1355. {
  1356. delayValue.Set(v);
  1357. _sliderDelay.SetValueWithoutNotify(delayValue.Get());
  1358. _sliderDelay.transform.Find("Value").GetComponent<Text>().text = delayValue.Get().ToString("f1");
  1359. infraredCameraHelper.SetDelay((int)delayValue.Get());
  1360. }
  1361. #endregion
  1362. #region 相机感光度(默认修改对比度)
  1363. public void onSetSliderCustomValue(Slider _slider, float min = 0.0f, float max = 10.0f)
  1364. {
  1365. onSetSliderValue(_slider, "PU_CONTRAST", min, max);
  1366. }
  1367. public void onSliderCustomEvent(float value, float min = 0.0f, float max = 10.0f)
  1368. {
  1369. onSliderEvent(value, "PU_CONTRAST", min, max);
  1370. }
  1371. public void onSetSliderCustomBrightnessValue(Slider _slider, float min = 0.0f, float max = 10.0f)
  1372. {
  1373. onSetSliderValue(_slider, "PU_BRIGHTNESS", min, max);
  1374. }
  1375. public void onSliderCustomBrightnessEvent(float value, float min = 0.0f, float max = 10.0f)
  1376. {
  1377. onSliderEvent(value, "PU_BRIGHTNESS", min, max);
  1378. }
  1379. public void onSetSliderValue(Slider _slider,string typeStr = "PU_CONTRAST", float min = 0.0f, float max = 10.0f)
  1380. {
  1381. if (BluetoothWindows.IsWindows()) {
  1382. float v = typeStr == "PU_CONTRAST"?infraredCameraHelper.GetContrast(): infraredCameraHelper.GetBrightness();
  1383. // 目标区间 [0, 10] 的边界值
  1384. double targetMin = min;
  1385. double targetMax = max;
  1386. double originalMin = -1;
  1387. double originalMax = 1;
  1388. // 计算转换后的值
  1389. double v2 = MapValue(v, originalMin, originalMax, targetMin, targetMax);
  1390. Debug.Log("PC获取相机的感光度 "+ typeStr + " :" + v + " = " + v2);
  1391. _slider.SetValueWithoutNotify((float)v2);
  1392. return;
  1393. }
  1394. //功能也改为UVC的对比度
  1395. if (running)
  1396. {
  1397. //string typeStr = "PU_CONTRAST";//"PU_BRIGHTNESS";
  1398. //UInt64 _VALUE = currentCameraInfo.GetTypeByName(typeStr);
  1399. UVCCtrlInfo _UVCCtrlInfo = currentCameraInfo.GetInfo(typeStr);
  1400. float v = currentCameraInfo.GetValue(typeStr); // infraredCameraHelper.GetBrightness();
  1401. //float v2 = (v / _UVCCtrlInfo.max) * 10;
  1402. // 目标区间 [0, 10] 的边界值
  1403. double targetMin = min;
  1404. double targetMax = max;
  1405. double originalMin = _UVCCtrlInfo.min;
  1406. double originalMax = _UVCCtrlInfo.max;
  1407. // 计算转换后的值
  1408. double v2 = MapValue(v, originalMin, originalMax, targetMin, targetMax);
  1409. Debug.Log("获取相机的感光度" + typeStr + ": " + v + " = " + v2);
  1410. _slider.SetValueWithoutNotify((float)v2);
  1411. }
  1412. else _slider.SetValueWithoutNotify(5);
  1413. }
  1414. public void onSliderEvent(float value, string typeStr = "PU_CONTRAST", float min = 0.0f,float max = 10.0f)
  1415. {
  1416. // string typeStr = "PU_CONTRAST";// "PU_BRIGHTNESS";
  1417. //pc
  1418. if (BluetoothWindows.IsWindows())
  1419. {
  1420. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  1421. Slider slider = trans.GetComponent<Slider>();
  1422. Text textObj = trans.Find("text").GetComponent<Text>();
  1423. // 原始区间和目标区间的边界值
  1424. double originalMin = min;
  1425. double originalMax = max;
  1426. double targetMin = -1;
  1427. double targetMax = 1;
  1428. // 计算转换后的值
  1429. double result = MapValue(value, originalMin, originalMax, targetMin, targetMax);
  1430. float _current = (float)result;
  1431. //Debug.Log("_current:" + _current + " , result:" + result);
  1432. textObj.text = _current + "";
  1433. slider.value = _current;
  1434. if (typeStr == "PU_CONTRAST")
  1435. {
  1436. infraredCameraHelper.SetContrast(_current);
  1437. pcCONTRAST.Set(_current);
  1438. }
  1439. else {
  1440. infraredCameraHelper.SetBrightness(_current);
  1441. pcBRIGHTNESS.Set(_current);
  1442. }
  1443. return;
  1444. }
  1445. //修改亮度时,调试界面的亮度也应该一起修改
  1446. //功能也改为UVC的对比度
  1447. if (running)
  1448. {
  1449. //Slider slider = transform.Find("InfraredCamera/Layout/SliderBrightness")
  1450. // .GetComponent<Slider>();
  1451. // UInt64 _VALUE = currentCameraInfo.GetTypeByName(typeStr);
  1452. Transform trans = _cameraParameterPanel.transform.Find(typeStr);
  1453. Slider slider = trans.GetComponent<Slider>();
  1454. Text textObj = trans.Find("text").GetComponent<Text>();
  1455. UVCCtrlInfo _UVCCtrlInfo = currentCameraInfo.GetInfo(typeStr);
  1456. int _currentUVCValue = currentCameraInfo.GetValue(typeStr);
  1457. //value 0 ~ 10
  1458. // 原始区间和目标区间的边界值
  1459. double originalMin = min;
  1460. double originalMax = max;
  1461. double targetMin = _UVCCtrlInfo.min;
  1462. double targetMax = _UVCCtrlInfo.max;
  1463. // 计算转换后的值
  1464. double result = MapValue(value, originalMin, originalMax, targetMin, targetMax);
  1465. int _current = (int)(result);
  1466. Debug.Log("_current:" + value + " , result:" + result);
  1467. textObj.text = _current + "";
  1468. slider.value = _current;
  1469. //dUVCParameters.GetValueOrDefault(typeStr).Set(_current);
  1470. //存储初始值,设置一次到UVC参数
  1471. if (_currentUVCValue != _current) currentCameraInfo.SetValue(typeStr, _current);
  1472. }
  1473. }
  1474. public static double MapValue(double value, double originalMin, double originalMax, double targetMin, double targetMax)
  1475. {
  1476. // 线性插值公式
  1477. return targetMin + (value - originalMin) * (targetMax - targetMin) / (originalMax - originalMin);
  1478. }
  1479. #endregion
  1480. #region 绘制线段部分
  1481. public void SetLineWidth(float v)
  1482. {
  1483. lineWidth.Set(v);
  1484. _sliderLineWidth.SetValueWithoutNotify(lineWidth.Get());
  1485. _sliderLineWidth.transform.Find("Value").GetComponent<Text>().text = lineWidth.Get().ToString("f1");
  1486. }
  1487. public void SetFanWidth(float v)
  1488. {
  1489. fanWidth.Set(v);
  1490. _fanWidth.SetValueWithoutNotify(fanWidth.Get());
  1491. _fanWidth.transform.Find("Value").GetComponent<Text>().text = fanWidth.Get().ToString("f1");
  1492. }
  1493. #endregion
  1494. #region 亮度检测部分
  1495. public void SetInfraredFilterValue(float v)
  1496. {
  1497. infraredFileterValue.Set(v);
  1498. _infraredFilter.SetValueWithoutNotify(infraredFileterValue.Get());
  1499. _infraredFilter.transform.Find("Value").GetComponent<Text>().text = infraredFileterValue.Get().ToString("f1");
  1500. infraredCameraHelper.SetInfraredLocateBrightnessThreshold(infraredFileterValue.Get());
  1501. }
  1502. #endregion
  1503. public void resetInfraredPlayerPrefs()
  1504. {
  1505. //测试用
  1506. PlayerPrefs.DeleteKey("entry-guider-infrared-" + LoginMgr.myUserInfo.id);
  1507. PlayerPrefs.DeleteKey("hideInfraredBowAndArrow");
  1508. }
  1509. #region 显示Log部分信息
  1510. [SerializeField] Text _calibrationFixedText;
  1511. [SerializeField] Text _cameraSizeText;
  1512. [SerializeField] Text _quadUnityVectorListText;
  1513. /// <summary>
  1514. /// 校准时候的固定值分辨率
  1515. /// </summary>
  1516. /// <param name="v"></param>
  1517. public void SetCalibrationFixedText(Vector2 v)
  1518. {
  1519. _calibrationFixedText.text = v.x + "*" + v.y;
  1520. }
  1521. /// <summary>
  1522. /// 渲染摄像机当前分辨率
  1523. /// </summary>
  1524. /// <param name="v"></param>
  1525. public void SetCameraSizeText(Vector2 v)
  1526. {
  1527. _cameraSizeText.text = v.x + "*" + v.y;
  1528. }
  1529. /// <summary>
  1530. /// 渲染摄像机识别点
  1531. /// </summary>
  1532. /// <param name="v"></param>
  1533. public void SetQuadUnityVectorListText(string v)
  1534. {
  1535. _quadUnityVectorListText.text = v;
  1536. }
  1537. #endregion
  1538. }
  1539. public class ParamFloatValue
  1540. {
  1541. private string _saveKey;
  1542. private float _valueDefault;
  1543. private bool _valueLoaded;
  1544. private float _value;
  1545. public ParamFloatValue(string saveKey, float valueDefault)
  1546. {
  1547. _saveKey = saveKey;
  1548. _valueDefault = valueDefault;
  1549. }
  1550. public float Get()
  1551. {
  1552. if (!_valueLoaded) _value = PlayerPrefs.GetFloat(_saveKey, _valueDefault);
  1553. return _value;
  1554. }
  1555. public float GetDefault() {
  1556. return _valueDefault;
  1557. }
  1558. public string GetKey()
  1559. {
  1560. return _saveKey;
  1561. }
  1562. public void Set(float value)
  1563. {
  1564. _value = value;
  1565. PlayerPrefs.SetFloat(_saveKey, _value);
  1566. PlayerPrefs.Save();
  1567. }
  1568. /// <summary>
  1569. /// 重置成默认值
  1570. /// </summary>
  1571. public void Reset() {
  1572. PlayerPrefs.SetFloat(_saveKey, _valueDefault);
  1573. PlayerPrefs.Save();
  1574. }
  1575. }