UI_HeroScrollFlowComponment.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. /// <summary>
  6. /// 英雄列表滑动功能组件
  7. /// </summary>
  8. public class UI_HeroScrollFlowComponment : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// 操作控制层
  12. /// </summary>
  13. public UI_ScrollFlow mScrollFlow;
  14. /// <summary>
  15. /// 模板
  16. /// </summary>
  17. public GameObject mPrefab;
  18. /// <summary>
  19. /// 子对象节点
  20. /// </summary>
  21. public GameObject mNodes;
  22. /// <summary>
  23. /// 是否显示 可以上阵或者详细信息的操作功能
  24. /// </summary>
  25. private bool IsShowBattleOrQueryButton = false;
  26. /// <summary>
  27. /// 是否要接收 数据发生变化的事件
  28. /// </summary>
  29. private bool IsNeedReceiveDataChangedEvent = false;
  30. /// <summary>
  31. /// 初始化UI
  32. /// </summary>
  33. /// <param name="isShowOperateButton"> 是否显示 可以上阵或者详细信息的操作功能</param>
  34. /// <param name="isNeedReceiveDataChangedEvent">是否要接收 数据发生变化的事件</param>
  35. public void InitComponments(bool isShowOperateButton = false,bool isNeedReceiveDataChangedEvent= false)
  36. {
  37. this.IsShowBattleOrQueryButton = isShowOperateButton;
  38. this.IsNeedReceiveDataChangedEvent = isNeedReceiveDataChangedEvent;
  39. mPrefab = this.transform.Find("prefab").gameObject;
  40. mPrefab.SetActive(false);
  41. mNodes = this.transform.Find("Nodes").gameObject;
  42. mScrollFlow = mNodes.GetComponent<UI_ScrollFlow>();
  43. if (mScrollFlow == null)
  44. {
  45. AssemblyHelper.Instance.BindScript("UI_ScrollFlow", mNodes);
  46. mScrollFlow = mNodes.GetComponent<UI_ScrollFlow>();
  47. }
  48. mNodes.GetComponent<RectTransform>().anchoredPosition = new Vector2(-250, 0);
  49. mScrollFlow.InitHeroComponmentsSetting();
  50. mScrollFlow.goItemPrefab = mPrefab;
  51. mScrollFlow.nodeContent = mNodes.GetComponent<RectTransform>();
  52. CreateElement();
  53. mScrollFlow.EventRefreshCardState += RefreshCardState;
  54. }
  55. /// <summary>
  56. /// 刷新卡牌的状态
  57. /// </summary>
  58. /// <param name="isSelected">是否选中</param>
  59. /// <param name="t">卡牌</param>
  60. private void RefreshCardState(bool isSelected, UI_ScrollFlow_Item t)
  61. {
  62. if (t != null && t.gameObject != null)
  63. {
  64. UI_HeroMustCardElement go = t.gameObject.GetComponent<UI_HeroMustCardElement>();
  65. go.RefreshSelectedState(isSelected);
  66. if (isSelected)
  67. {
  68. _curHeroUID = go.mData;
  69. if (Action_ChangeCurrentCard != null)
  70. {
  71. Action_ChangeCurrentCard(go.mData, t.gameObject);
  72. }
  73. }
  74. }
  75. }
  76. /// <summary>
  77. /// 销毁
  78. /// </summary>
  79. private void OnDestory()
  80. {
  81. mScrollFlow.EventRefreshCardState -= RefreshCardState;
  82. }
  83. /// <summary>
  84. /// 创建元素
  85. /// </summary>
  86. private void CreateElement()
  87. {
  88. mScrollFlow.onInitializeItem = onInitializeItem;
  89. mScrollFlow.Init(0);
  90. }
  91. /// <summary>
  92. /// 元素初始化
  93. /// </summary>
  94. /// <param name="go"> 元素对象 </param>
  95. /// <param name="dataIndex"> 数据索引 </param>
  96. private void onInitializeItem(GameObject go, int dataIndex)
  97. {
  98. UI_HeroMustCardElement element = go.GetComponent<UI_HeroMustCardElement>();
  99. if (null == element)
  100. {
  101. AssemblyHelper.Instance.BindScript("UI_HeroMustCardElement", go);
  102. element = go.GetComponent<UI_HeroMustCardElement>();
  103. }
  104. element.Init(this.IsNeedReceiveDataChangedEvent, this.IsShowBattleOrQueryButton);
  105. element.Action_QueryButton = OnClicked_QueryButton;
  106. element.Action_SelectedSomeCard = OnClicked_Select;
  107. element.Action_UseButton = OnClicked_UseButton;
  108. ////element.Action_UNLockButton = OnClicked_LockButton;
  109. element.Action_BuyButton = OnClicked_BuyHeroButton;
  110. element.Action_GetMethodButton = OnClicked_GetMethodButton;
  111. if (dataIndex >= 0 && dataIndex < mDataList.Count)
  112. {
  113. element.SetData(mDataList[dataIndex]);
  114. ////LogHelper.LogError("onInitializeItem:::" + dataIndex + " uid::" + mDataList[dataIndex]);
  115. if (elements.ContainsKey(dataIndex))
  116. {
  117. elements[dataIndex] = go;
  118. }
  119. else
  120. {
  121. elements.Add(dataIndex, go);
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// 数据列表
  127. /// </summary>
  128. private List<string> mDataList = new List<string>();
  129. /// <summary>
  130. /// 刷新元素
  131. /// </summary>
  132. /// <param name="heroUIDList">英雄列表</param>
  133. /// <param name="bResetPos"> 是否重设位置 </param>
  134. public void SetHeroData(List<string> heroUIDList, bool bResetPos = false)
  135. {
  136. mDataList = new List<string>();
  137. elements = new Dictionary<int, GameObject>();
  138. mDataList.AddRange(heroUIDList);
  139. mScrollFlow.Hero_DataChanged(heroUIDList.Count, bResetPos);
  140. if (heroUIDList.Count == 0)
  141. {
  142. mScrollFlow.enabled = false;
  143. _curHeroUID = string.Empty;
  144. }
  145. else
  146. {
  147. mScrollFlow.enabled = true;
  148. }
  149. }
  150. /// <summary>
  151. /// 子对象集合
  152. /// </summary>
  153. private Dictionary<int, GameObject> elements = new Dictionary<int, GameObject>();
  154. /// <summary>
  155. /// 当前英雄UID
  156. /// </summary>
  157. public string _curHeroUID = string.Empty;
  158. /// <summary>
  159. /// 获得当前英雄UID
  160. /// </summary>
  161. /// <returns>string</returns>
  162. internal string GetCurHeroUID()
  163. {
  164. return _curHeroUID;
  165. }
  166. /// <summary>
  167. /// 刷新卡牌锁定以及上阵状态
  168. /// </summary>
  169. internal void RefeshHeroCardLockAndFightState()
  170. {
  171. foreach (GameObject xx in elements.Values)
  172. {
  173. UI_HeroMustCardElement ele = xx.GetComponent<UI_HeroMustCardElement>();
  174. bool isInTeam = UserProxy.Instance.player.heroTeamConfig.CheckSomeHeroBeUsed(ele.mHeroUIDData);
  175. ele.RefreshCardLockAndFightState(isInTeam);
  176. }
  177. }
  178. /// <summary>
  179. /// 更改当前选择的卡牌
  180. /// </summary>
  181. internal Action<string, GameObject> Action_ChangeCurrentCard;
  182. /// <summary>
  183. /// 点击卡牌事件
  184. /// </summary>
  185. internal Action<string, GameObject> Action_SelectedSomeCard;
  186. /// <summary>
  187. /// 点击卡牌上阵事件
  188. /// </summary>
  189. internal Action<string, GameObject> Action_UseButton;
  190. /// <summary>
  191. /// 点击卡牌查询事件
  192. /// </summary>
  193. internal Action<string, GameObject> Action_QueryButton;
  194. /// <summary>
  195. /// 点击卡牌 购买按钮
  196. /// </summary>
  197. public Action<string, GameObject> Action_BuyButton;
  198. /// <summary>
  199. /// 点击卡牌 未解锁按钮
  200. /// </summary>
  201. public Action<string, GameObject> Action_UNLockButton;
  202. /// <summary>
  203. /// 点击卡牌 获取按钮
  204. /// </summary>
  205. public Action<string, GameObject> Action_GetMethodButton;
  206. /// <summary>
  207. /// 查询
  208. /// </summary>
  209. /// <param name="go"> 按钮对象 </param>
  210. private void OnClicked_QueryButton(GameObject go)
  211. {
  212. UI_HeroMustCardElement ele = go.GetComponent<UI_HeroMustCardElement>();
  213. if (ele == null)
  214. {
  215. return;
  216. }
  217. if (Action_QueryButton != null)
  218. {
  219. Action_QueryButton(ele.mData, go);
  220. }
  221. }
  222. /// <summary>
  223. /// 上阵
  224. /// </summary>
  225. /// <param name="go">按钮对象</param>
  226. private void OnClicked_UseButton(GameObject go)
  227. {
  228. UI_HeroMustCardElement ele = go.GetComponent<UI_HeroMustCardElement>();
  229. if (ele == null)
  230. {
  231. return;
  232. }
  233. if (Action_UseButton != null)
  234. {
  235. Action_UseButton(ele.mData, go);
  236. }
  237. }
  238. /// <summary>
  239. /// 未解锁按钮
  240. /// </summary>
  241. /// <param name="go">按钮对象</param>
  242. private void OnClicked_LockButton(GameObject go)
  243. {
  244. UI_HeroMustCardElement ele = go.GetComponent<UI_HeroMustCardElement>();
  245. if (ele == null)
  246. {
  247. return;
  248. }
  249. if (Action_UNLockButton != null)
  250. {
  251. Action_UNLockButton(ele.mData, go);
  252. }
  253. }
  254. /// <summary>
  255. /// 点击 获取按钮
  256. /// </summary>
  257. /// <param name="go"></param>
  258. private void OnClicked_GetMethodButton(GameObject go)
  259. {
  260. UI_HeroMustCardElement ele = go.GetComponent<UI_HeroMustCardElement>();
  261. if (ele == null)
  262. {
  263. return;
  264. }
  265. if (Action_GetMethodButton != null)
  266. {
  267. Action_GetMethodButton(ele.mData, go);
  268. }
  269. }
  270. /// <summary>
  271. /// 购买 按钮
  272. /// </summary>
  273. /// <param name="go">按钮对象</param>
  274. private void OnClicked_BuyHeroButton(GameObject go)
  275. {
  276. UI_HeroMustCardElement ele = go.GetComponent<UI_HeroMustCardElement>();
  277. if (ele == null)
  278. {
  279. return;
  280. }
  281. if (Action_BuyButton != null)
  282. {
  283. Action_BuyButton(ele.mData, go);
  284. }
  285. }
  286. /// <summary>
  287. /// 点击选择
  288. /// </summary>
  289. /// <param name="go">按钮对象</param>
  290. private void OnClicked_Select(GameObject go)
  291. {
  292. UI_ScrollFlow_Item script = go.GetComponent<UI_ScrollFlow_Item>();
  293. if (script == null)
  294. {
  295. return;
  296. }
  297. if (mScrollFlow.Current != script)
  298. {
  299. mScrollFlow.ToAppointedItem(script);
  300. }
  301. else
  302. {
  303. UI_HeroMustCardElement ele = go.GetComponent<UI_HeroMustCardElement>();
  304. if (ele == null)
  305. {
  306. return;
  307. }
  308. if (Action_SelectedSomeCard != null)
  309. {
  310. Action_SelectedSomeCard(ele.mData, go);
  311. }
  312. }
  313. }
  314. /// <summary>
  315. /// 对外的外界: 设定选中的卡牌
  316. /// 来促使卡牌进行移动
  317. /// </summary>
  318. /// <param name="data"></param>
  319. public void OnChangeSelectData(string data)
  320. {
  321. if (string.IsNullOrEmpty(data))
  322. return;
  323. foreach (GameObject xx in elements.Values)
  324. {
  325. UI_HeroMustCardElement ele = xx.GetComponent<UI_HeroMustCardElement>();
  326. if (ele.mData == data)
  327. {
  328. OnClicked_Select(ele.gameObject);
  329. }
  330. }
  331. }
  332. }