human_area.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /// <summary>
  2. /// ZWN-XHJ
  3. /// 20211019
  4. /// </summary>
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using UnityEngine.Analytics;
  10. public enum human_area_state
  11. {
  12. halfbody,//半身
  13. totalbody//全身
  14. }
  15. public class human_area : MonoBehaviour
  16. {
  17. public human_area_state human_area_state_cur;
  18. Vector3 middle_vec, head_vec, foot_vec;
  19. int area = 0, area_sum = 0;//当前是否在区域内计数
  20. [HideInInspector]
  21. public List<int> area_list;//中心的位置
  22. private float area_mid_maxX = 100,
  23. area_mid_minX = -100,
  24. area_mid_maxY = 505,
  25. area_mid_minY = -505,
  26. area_height = 844;//中心的范围
  27. private float this_height, this_height_left, this_height_right;
  28. int area_count = 15;
  29. private bool area_true = false;//当前是否在区域内
  30. float area_per = 0.7f;
  31. Vector3 head, right_ankle, left_ankle;
  32. Vector3 default_vec = new Vector3(-Screen.width, -Screen.height, 0);
  33. float area_value = 0.5f;
  34. float area_value_total = 0.5f;
  35. float area_value_half = 0.5f;
  36. void Awake()
  37. {
  38. if (human_area_state_cur == human_area_state.totalbody)
  39. {
  40. //area_height = 844;//中心的范围
  41. area_height = 1009;//中心的范围
  42. }
  43. else if (human_area_state_cur == human_area_state.halfbody)
  44. {
  45. //area_height = 714;//中心的范围
  46. area_height = 1009;//中心的范围
  47. }
  48. }
  49. void Start()
  50. {
  51. /* area_mid_maxX = 100;
  52. area_mid_minX = -100;
  53. area_mid_maxY = 505;
  54. area_mid_minY = -505;*/
  55. area_mid_maxX = 274;
  56. area_mid_minX = -274;
  57. area_mid_maxY = 505;
  58. area_mid_minY = -505;
  59. if (Application.platform == RuntimePlatform.WindowsEditor
  60. || Application.platform == RuntimePlatform.WindowsPlayer)
  61. {
  62. area_count = 15;
  63. }
  64. if (Application.platform == RuntimePlatform.Android)// 使用Unity切换Platform无法模拟
  65. {
  66. area_count = 6;
  67. }
  68. area_value_total = PlayerPrefs.GetFloat("total");
  69. if (area_value_total == 0)
  70. {
  71. area_value_total = 0.5f;
  72. }
  73. area_value_half = PlayerPrefs.GetFloat("half");
  74. if (area_value_half == 0)
  75. {
  76. area_value_half = 0.5f;
  77. }
  78. if (human_area_state_cur == human_area_state.totalbody)
  79. {
  80. area_value = area_value_total;
  81. text_total.text = area_value.ToString();
  82. }
  83. else if (human_area_state_cur == human_area_state.halfbody)
  84. {
  85. area_value = area_value_half;
  86. text_half.text = area_value.ToString();
  87. }
  88. }
  89. /* //使用例子
  90. void Update()
  91. {
  92. print(human_area_true());
  93. }*/
  94. int area_num = -1;
  95. public Text text_total;
  96. public Text text_half;
  97. public void total_sum()
  98. {
  99. area_value_total += 0.1f;
  100. area_value = area_value_total;
  101. PlayerPrefs.SetFloat("total", area_value_total);
  102. text_total.text = area_value.ToString();
  103. }
  104. public void total_dec()
  105. {
  106. area_value_total -= 0.1f;
  107. area_value = area_value_total;
  108. PlayerPrefs.SetFloat("total", area_value_total);
  109. text_total.text = area_value.ToString();
  110. }
  111. public void half_sum()
  112. {
  113. area_value_half += 0.1f;
  114. area_value = area_value_half;
  115. PlayerPrefs.SetFloat("half", area_value_half);
  116. text_half.text = area_value.ToString();
  117. }
  118. public void half_dec()
  119. {
  120. area_value_half -= 0.1f;
  121. area_value = area_value_half;
  122. PlayerPrefs.SetFloat("half", area_value_half);
  123. text_half.text = area_value.ToString();
  124. }
  125. /*float area_value_total = 0.5f;
  126. float area_value_half = 0.5f;*/
  127. //public Text show;
  128. /// <summary>
  129. /// -1:不存在
  130. /// 0:在区域内
  131. /// 1:偏左
  132. /// 2:偏右
  133. /// 3:偏前
  134. /// 4:偏后
  135. /// 5:人数太多
  136. /// </summary>
  137. /// <returns></returns>
  138. public int human_area_true()
  139. {
  140. //print("common_data.single.detail_lists[3] == 1: " + (common_data.single.detail_lists[3] == 1));
  141. /* 0:鼻子1:左眼2:右眼3:左耳4:右耳5:左肩6:右肩7:左肘
  142. 8:右肘9:左腕10:右腕11:左臀12:右臀
  143. 13:左膝14:右膝15:左脚踝16:右脚踝*/
  144. //if (zwn_interface.single.PoseHelper.Zwn_Pose_Data_Roi)
  145. //{
  146. // if (zwn_interface.single.PoseHelper.zwn_pose_array_cur.count < 1)
  147. // {
  148. // area_num = -1;
  149. // }
  150. // else if (zwn_interface.single.PoseHelper.zwn_pose_array_cur.count > 1)
  151. // {
  152. // area_num = 5;
  153. // }
  154. // else
  155. // {
  156. // /*if (zwn_common_data.single.zwn_pose_android_cur.point.rect_xy.x < area_mid_minX)
  157. // {
  158. // //print("偏左");
  159. // area_num = 1;
  160. // }
  161. // else if (zwn_common_data.single.zwn_pose_android_cur.point.rect_xy.x > area_mid_maxX)
  162. // {
  163. // //print("偏右");
  164. // area_num = 2;
  165. // }
  166. // else
  167. // {
  168. // area_num = area_points();
  169. // }*/
  170. // if (zwn_common_data.single.zwn_pose_cur.point.rect_xy.x < area_mid_minX)
  171. // {
  172. // //print("偏左");
  173. // area_num = 1;
  174. // }
  175. // else if (zwn_common_data.single.zwn_pose_cur.point.rect_xy.x > area_mid_maxX)
  176. // {
  177. // //print("偏右");
  178. // area_num = 2;
  179. // }
  180. // else
  181. // {
  182. // area_num = area_points();
  183. // }
  184. // }
  185. // return area_num;
  186. //}
  187. return -1;
  188. }
  189. int area_points()
  190. {
  191. if ((ZwnAmlogicHelperData.isSomeone == true
  192. && human_area_state_cur == human_area_state.totalbody
  193. && zwn_common_data.single.zwn_transform_pose[15] != default_vec
  194. && zwn_common_data.single.zwn_transform_pose[16] != default_vec)
  195. || (ZwnAmlogicHelperData.isSomeone == true && human_area_state_cur == human_area_state.halfbody))//是否存在整个人
  196. {
  197. if (human_area_state_cur == human_area_state.totalbody)
  198. {
  199. middle_vec = (zwn_common_data.single.zwn_transform_pose[15]
  200. + zwn_common_data.single.zwn_transform_pose[16]
  201. + zwn_common_data.single.zwn_transform_pose[11]
  202. + zwn_common_data.single.zwn_transform_pose[12]
  203. + zwn_common_data.single.zwn_transform_pose[5]
  204. + zwn_common_data.single.zwn_transform_pose[6]
  205. ) / 6;
  206. foot_vec = (zwn_common_data.single.zwn_transform_pose[15]
  207. + zwn_common_data.single.zwn_transform_pose[16]
  208. ) / 2;
  209. head_vec = zwn_common_data.single.zwn_transform_pose[0];
  210. head = zwn_common_data.single.zwn_transform_pose[1];
  211. right_ankle = zwn_common_data.single.zwn_transform_pose[15];
  212. left_ankle = zwn_common_data.single.zwn_transform_pose[16];
  213. if (right_ankle.y != -1 && left_ankle.y != -1)
  214. {
  215. if (head.y > 0 && right_ankle.y > 0)
  216. {
  217. this_height_right = Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(right_ankle.y));
  218. }
  219. else
  220. {
  221. this_height_right = Mathf.Abs(Mathf.Abs(head.y) + Mathf.Abs(right_ankle.y));
  222. }
  223. if (head.y > 0 && left_ankle.y > 0)
  224. {
  225. this_height_left = Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(left_ankle.y));
  226. }
  227. else
  228. {
  229. this_height_left = Mathf.Abs(Mathf.Abs(head.y) + Mathf.Abs(left_ankle.y));
  230. }
  231. this_height = Mathf.Max(this_height_right, this_height_left);
  232. }
  233. }
  234. else if (human_area_state_cur == human_area_state.halfbody)
  235. {
  236. middle_vec = (zwn_common_data.single.zwn_transform_pose[11]
  237. + zwn_common_data.single.zwn_transform_pose[12]
  238. + zwn_common_data.single.zwn_transform_pose[5]
  239. + zwn_common_data.single.zwn_transform_pose[6]
  240. ) / 4;
  241. foot_vec = (zwn_common_data.single.zwn_transform_pose[11]
  242. + zwn_common_data.single.zwn_transform_pose[12]
  243. ) / 2;
  244. //head_vec = zwn_common_data.single.zwn_transform_pose[0];
  245. //head = zwn_common_data.single.zwn_transform_pose[1];
  246. head_vec = (zwn_common_data.single.zwn_transform_pose[5]
  247. + zwn_common_data.single.zwn_transform_pose[6]
  248. ) / 2;
  249. head = (zwn_common_data.single.zwn_transform_pose[5]
  250. + zwn_common_data.single.zwn_transform_pose[6]
  251. ) / 2;
  252. right_ankle = zwn_common_data.single.zwn_transform_pose[11];
  253. left_ankle = zwn_common_data.single.zwn_transform_pose[12];
  254. }
  255. if (right_ankle.y != -1 && left_ankle.y != -1)
  256. {
  257. if (head.y > 0 && right_ankle.y > 0)
  258. {
  259. this_height_right = Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(right_ankle.y));
  260. }
  261. else
  262. {
  263. this_height_right = Mathf.Abs(Mathf.Abs(head.y) + Mathf.Abs(right_ankle.y));
  264. }
  265. if (head.y > 0 && left_ankle.y > 0)
  266. {
  267. this_height_left = Mathf.Abs(Mathf.Abs(head.y) - Mathf.Abs(left_ankle.y));
  268. }
  269. else
  270. {
  271. this_height_left = Mathf.Abs(Mathf.Abs(head.y) + Mathf.Abs(left_ankle.y));
  272. }
  273. this_height = Mathf.Max(this_height_right, this_height_left);
  274. }
  275. // print("this_height: " + this_height + " area_height * 0.75f: " + area_height * 0.75f);
  276. //show.text = "this_height: " + this_height + " area_height * 0.75f: " + area_height * 0.75f;
  277. if (
  278. middle_vec.x >= area_mid_minX
  279. && middle_vec.x <= area_mid_maxX
  280. && foot_vec.y >= area_mid_minY
  281. && foot_vec.y <= area_mid_maxY
  282. && head_vec.y >= area_mid_minY
  283. && head_vec.y <= area_mid_maxY
  284. && this_height >= area_height * area_value
  285. )
  286. {
  287. if (area != 1)
  288. {
  289. area = 1;
  290. }
  291. }
  292. else
  293. {
  294. if (area != 0)
  295. {
  296. area = 0;
  297. }
  298. }
  299. area_list.Add(area);
  300. if (area_sum != 0)
  301. {
  302. area_sum = 0;
  303. }
  304. if (area_list.Count >= area_count)
  305. {
  306. area_list.Remove(area_list[0]);
  307. for (int i = 0; i < area_list.Count; i++)
  308. {
  309. area_sum += area_list[i];
  310. }
  311. if (area_sum >= area_count * area_per)
  312. {
  313. if (!area_true)
  314. {
  315. area_true = true;
  316. }
  317. }
  318. else
  319. {
  320. area_true = false;
  321. if (middle_vec.x < area_mid_minX)
  322. {
  323. //print("偏左");
  324. area_num = 1;
  325. }
  326. else if (middle_vec.x > area_mid_maxX)
  327. {
  328. //print("偏右");
  329. area_num = 2;
  330. }
  331. else if (this_height < area_height * area_value)
  332. {
  333. //print("偏后");
  334. area_num = 4;
  335. }
  336. else
  337. {
  338. //print("偏前");
  339. area_num = 3;
  340. }
  341. }
  342. }
  343. if (!area_true)
  344. {
  345. //人不在区域内
  346. return area_num;
  347. }
  348. else
  349. {
  350. //print("人存在区域");
  351. return 0;
  352. }
  353. }
  354. else
  355. {
  356. area_list.Clear();
  357. //print("人不存在");
  358. return -1;
  359. }
  360. }
  361. }