UIMaterials.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /****************************************************************************
  2. * 2022.1 SK-20211220VCWK
  3. ****************************************************************************/
  4. using System;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using QFramework;
  9. using UniRx;
  10. namespace QFramework.MREditor
  11. {
  12. public partial class UIMaterials : UIElement
  13. {
  14. private void Awake()
  15. {
  16. }
  17. protected override void OnBeforeDestroy()
  18. {
  19. }
  20. private ToggleGroup group;
  21. private Dictionary<String, List< MaterialBtn>> dicBtns = new Dictionary<string, List<MaterialBtn>>();
  22. private Dictionary<string, List<MaterialBtn>> dicCMVBtns = new Dictionary<string, List<MaterialBtn>>();
  23. private string oldSpoitName;
  24. private MaterialBtn selectBtn;
  25. public void Initialized(string spoitName, List<CompositeMaterialValue> listObjs, MaterialBtn materialBtn)
  26. {
  27. oldSpoitName = "";
  28. group = UIMaterialContent.GetComponent<ToggleGroup>();
  29. dicBtns.Add(spoitName, new List<MaterialBtn>());
  30. InitBtn();
  31. // AddMaterialBtn.Hide();
  32. Debug.Log("#### "+listObjs.Count);
  33. for (int i = 0; i < listObjs.Count; i++)
  34. {
  35. Debug.Log((MaterialType)listObjs[i].type);
  36. switch ((MaterialType)listObjs[i].type)
  37. {
  38. case MaterialType.NULL:
  39. break;
  40. case MaterialType.Image:
  41. case MaterialType.Video:
  42. case MaterialType.Model:
  43. case MaterialType.Text:
  44. //CreateMaterialBtn(listObjs[i].materialList[0].name, listObjs[i].name, spoitName, materialBtn);
  45. //break;
  46. case MaterialType.ImageorViedoAndText:
  47. case MaterialType.ModelAndText:
  48. Debug.Log(listObjs[i].name);
  49. dicCMVBtns.Add(spoitName+listObjs[i].name, new List<MaterialBtn>());
  50. Debug.Log(listObjs[i].name);
  51. dicCMVBtns[spoitName + listObjs[i].name].Add(CreateMaterialBtn(listObjs[i].name, listObjs[i].name, spoitName, materialBtn));
  52. Debug.Log(listObjs[i].name+1111);
  53. listObjs[i].materialList.ForEach(item =>
  54. {
  55. dicCMVBtns[spoitName + listObjs[i].name].Add(CreateMaterialBtn(item.name, listObjs[i].name, spoitName, materialBtn));
  56. });
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. #region Old
  63. //listObjs.ForEach(item =>
  64. //{
  65. // materialBtn.Instantiate()
  66. // .Parent(UIMaterialContent)
  67. // .Identity()
  68. // .ApplySelfTo(btn =>
  69. // {
  70. // Toggle toggle = btn.GetComponent<Toggle>();
  71. // toggle.onValueChanged.AddListener(state =>
  72. // {
  73. // if (state)
  74. // {
  75. // SendMsg(new OnSelectObjName(item.name, spoitName));
  76. // selectBtn = btn;
  77. // }
  78. // });
  79. // toggle.group = group;
  80. // btn.Text.text = item.name;
  81. // dicBtns[spoitName].Add(btn);
  82. // Vector3 pos = btn.GetComponent<RectTransform>().anchoredPosition3D;
  83. // btn.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(pos.x, pos.y, 0);
  84. // });
  85. //});
  86. #endregion
  87. }
  88. public MaterialBtn CreateMaterialBtn(string Name ,string CMVName , string spoitName ,MaterialBtn materialBtn)
  89. {
  90. return materialBtn.Instantiate()
  91. .Parent(UIMaterialContent)
  92. .Identity()
  93. .ApplySelfTo(btn =>
  94. {
  95. Toggle toggle = btn.GetComponent<Toggle>();
  96. toggle.onValueChanged.AddListener(state =>
  97. {
  98. if (state)
  99. {
  100. SendMsg(new OnSelectObjName(Name, CMVName, spoitName));
  101. selectBtn = btn;
  102. }
  103. });
  104. toggle.group = group;
  105. btn.Text.text = Name;
  106. dicBtns[spoitName].Add(btn);
  107. Vector3 pos = btn.GetComponent<RectTransform>().anchoredPosition3D;
  108. btn.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(pos.x, pos.y, 0);
  109. btn.transform.localEulerAngles = Vector3.one;
  110. Debug.Log(spoitName + " " + CMVName + " " + Name);
  111. });
  112. }
  113. public MaterialBtn CreateMaterialBtn(string Name, string CMVName, string spoitName, MaterialBtn materialBtn ,bool f)
  114. {
  115. MaterialBtn materialitem = null;
  116. materialBtn.Instantiate()
  117. .Parent(UIMaterialContent)
  118. .Identity()
  119. .ApplySelfTo(btn =>
  120. {
  121. Toggle toggle = btn.GetComponent<Toggle>();
  122. toggle.onValueChanged.AddListener(state =>
  123. {
  124. if (state)
  125. {
  126. SendMsg(new OnSelectObjName(Name, CMVName, spoitName));
  127. selectBtn = btn;
  128. }
  129. });
  130. toggle.group = group;
  131. btn.Text.text = Name;
  132. dicBtns[spoitName].Add(btn);
  133. Vector3 pos = btn.GetComponent<RectTransform>().anchoredPosition3D;
  134. btn.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(pos.x, pos.y, 0);
  135. btn.transform.localEulerAngles = Vector3.one;
  136. materialitem = btn;
  137. })
  138. .Show();
  139. UIMaterialContent.GetComponent<RectTransform>().sizeDelta += new Vector2(0, 100 );
  140. return materialitem;
  141. }
  142. /// <summary>
  143. /// 选择当前景点 ,目前用扫图触发的方式
  144. /// </summary>
  145. /// <param name="spoitName"> </param>
  146. public void SelectSpoit( string spoitName)
  147. {
  148. if (!dicBtns.ContainsKey(spoitName))
  149. return;
  150. if (oldSpoitName.IsNotNullAndEmpty())
  151. dicBtns[oldSpoitName].ForEach(item =>
  152. {
  153. item.Hide();
  154. });
  155. dicBtns[spoitName].ForEach(item =>
  156. {
  157. item.Show();
  158. // Debug.Log(spoitName + " Show");
  159. });
  160. UIMaterialContent.GetComponent<RectTransform>().sizeDelta += new Vector2(0, (dicBtns[spoitName].Count+2) * 70 + 15);
  161. oldSpoitName = spoitName;
  162. Debug.Log(dicBtns[spoitName].Count);
  163. if(dicBtns[spoitName].Count>0)
  164. selectBtn = dicBtns[spoitName][0];
  165. ALLBtn.transform.SetAsFirstSibling();
  166. AddMaterialBtn.transform.SetAsLastSibling();
  167. }
  168. public void RemoveMaterials( string selectName)
  169. {
  170. if(selectBtn!=null)
  171. {
  172. selectBtn.Hide();
  173. selectBtn = null;
  174. if(dicCMVBtns.ContainsKey(selectName))
  175. {
  176. dicCMVBtns[selectName].ForEach(item =>
  177. {
  178. item.Hide();
  179. });
  180. }
  181. }
  182. }
  183. public void AddMaterial(string spoitName, CompositeMaterialValue objValue, MaterialBtn materialBtn)
  184. {
  185. switch ((MaterialType)objValue.type)
  186. {
  187. case MaterialType.NULL:
  188. break;
  189. case MaterialType.Image:
  190. case MaterialType.Video:
  191. case MaterialType.Model:
  192. case MaterialType.Text:
  193. //CreateMaterialBtn(objValue.materialList[0].name, objValue.name, spoitName, materialBtn, false);
  194. //break;
  195. case MaterialType.ImageorViedoAndText:
  196. case MaterialType.ModelAndText:
  197. dicCMVBtns.Add(spoitName + objValue.name, new List<MaterialBtn>());
  198. dicCMVBtns[spoitName + objValue.name].Add(CreateMaterialBtn(objValue.name, objValue.name, spoitName, materialBtn, false));
  199. objValue.materialList.ForEach(item =>
  200. {
  201. Debug.Log(item.name);
  202. dicCMVBtns[spoitName + objValue.name].Add(CreateMaterialBtn(item.name, objValue.name, spoitName, materialBtn, false));
  203. });
  204. break;
  205. default:
  206. break;
  207. }
  208. AddMaterialBtn.transform.SetAsLastSibling();
  209. }
  210. public void InitBtn()
  211. {
  212. ALLBtn.onValueChanged.AddListener(state =>
  213. {
  214. if (state)
  215. {
  216. // 选择整体
  217. if (oldSpoitName.IsNotNullAndEmpty())
  218. SendMsg(new OnSelectObjName(oldSpoitName, oldSpoitName, oldSpoitName));
  219. }
  220. });
  221. AddMaterialBtn.onValueChanged.AddListener(state =>
  222. {
  223. if (state)
  224. {
  225. SendMsg(new OnUIOpenElenemt(false));
  226. }
  227. });
  228. }
  229. #region Old
  230. //public void AddMaterial(string spoitName, MaterialObjValue objValue, MaterialBtn materialBtn)
  231. // {
  232. // materialBtn.Instantiate()
  233. // .Parent(UIMaterialContent)
  234. // .Identity()
  235. // .ApplySelfTo(btn =>
  236. // {
  237. // btn.GetComponent<RectTransform>().localEulerAngles = Vector3.zero;
  238. // Toggle toggle = btn.GetComponent<Toggle>();
  239. // toggle.onValueChanged.AddListener(state =>
  240. // {
  241. // if (state)
  242. // {
  243. // SendMsg(new OnSelectObjName(objValue.name, spoitName));
  244. // selectBtn = btn;
  245. // }
  246. // });
  247. // toggle.group = group;
  248. // btn.Text.text = objValue.name;
  249. // dicBtns[spoitName].Add(btn);
  250. // Vector3 pos = btn.GetComponent<RectTransform>().anchoredPosition3D;
  251. // btn.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(pos.x,pos.y,0);
  252. // })
  253. // .Show();
  254. // AddMaterialBtn.transform.SetAsLastSibling();
  255. // UIMaterialContent.GetComponent<RectTransform>().sizeDelta += new Vector2(0, 70);
  256. //}
  257. #endregion
  258. }
  259. }