InfraredDemo.cs 64 KB

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