using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; using UnityEngine.EventSystems; namespace DisComputer { /* * @func 格子控制 * @author lz * @date 2020/05/28 */ public class Grid : MonoBehaviour { //零件详细信息 public SpareInfo spareInfo; //零件图片挂载 public RawImage spare_img; //零件名称 public Text spareName_txt; //零件类型 public SpareType type; //零件贴图 public List spareTexs; //初始位置信息 [HideInInspector] public Vector3 initPos; /// /// 当前检测的零件标签 /// public string chectSpareTag; //鼠标响应层 public Image mouse_Btn; //零件列表 public GameObject scrollGo; //content public Transform scrollRootTran; //item public SpareScrollItem item; private void Awake() { EventTrigger trigger = mouse_Btn.GetComponent(); //点击下事件 EventTrigger.Entry entryDown = new EventTrigger.Entry(); entryDown.eventID = EventTriggerType.PointerDown; entryDown.callback.AddListener((data)=> { OnPressDownSpare((PointerEventData)data); }); trigger.triggers.Add(entryDown); EventTrigger.Entry entryOnPoint = new EventTrigger.Entry(); entryOnPoint.eventID = EventTriggerType.PointerClick; entryOnPoint.callback.AddListener((data) => { OnClickPoint((PointerEventData)data); }); trigger.triggers.Add(entryOnPoint); mouse_Btn.gameObject.SetActive(false); } /// /// 设置格子信息 /// /// public void SetGridInfo(SpareType spare) { type = spare; spareName_txt.text = CommonTool.GetSpareName(spare); spare_img.texture = GetSpareTex(spare); if(spare_img.texture == null) { spare_img.gameObject.SetActive(false); mouse_Btn.gameObject.SetActive(false); } else { spare_img.gameObject.SetActive(true); mouse_Btn.gameObject.SetActive(true); } //创建滚动列表 CreateScrollItem(spare); } /// /// 设置格子信息 /// /// public void SetGridInfo(SpareInfo info) { this.spareInfo = info; type = info.type; spareName_txt.text = CommonTool.GetSpareName(info.type); spare_img.texture = info.texture; if (spare_img.texture == null) { spare_img.gameObject.SetActive(false); mouse_Btn.gameObject.SetActive(false); } else { spare_img.gameObject.SetActive(true); mouse_Btn.gameObject.SetActive(true); } //创建滚动列表 CreateScrollItem(info.type); } /// /// 设置详细信息 /// /// /// public void SetGridInfo(SpareType spare,SpareInfo info) { type = spare; this.spareInfo = info; spareName_txt.text = CommonTool.GetSpareName(spare); spare_img.texture = info.texture; if (spare_img.texture == null) { spare_img.gameObject.SetActive(false); mouse_Btn.gameObject.SetActive(false); } else { spare_img.gameObject.SetActive(true); mouse_Btn.gameObject.SetActive(true); } if(info.type!= SpareType.ZhuBan || info.type != SpareType.JiXiang) { //创建滚动列表 CreateScrollItem(spare); } } /// /// 清理格子信息 /// public void CleraGird() { spareName_txt.text = ""; type = SpareType.Null; spare_img.texture = null; spare_img.gameObject.SetActive(false); mouse_Btn.gameObject.SetActive(false); } /// /// 设置当前选择零件 /// /// public void SetCurSpareInfo(SpareInfo info) { spareInfo = info; spare_img.texture = info.texture; Debug.LogFormat(">>>>>>>>>>>> name {0}", info.name); //对应零件切换皮肤 PcControl.Instance.ChangeCurSpareSkin(info); } /// /// 加入零件列表 /// /// void CreateScrollItem(SpareType spare) { if (GameConfig.state == GameState.Unload) return; if (scrollRootTran.childCount>1) return; SpareInfoConfig infoConfig = null; for (int i = 0; i < GameDataConfig.Instance.infoConfig.Count; i++) { if(GameDataConfig.Instance.infoConfig[i].type == spare) { //Debug.LogFormat(">>>> >>>> {0}", spare); infoConfig = GameDataConfig.Instance.infoConfig[i]; break; } } if (infoConfig == null) return; int[] randomArr = LzFramework.FuncTools.CreateRanArrVal(infoConfig.infos.Count, infoConfig.infos.Count).ToArray(); for (int k = 0;k < infoConfig.infos.Count; k++) { GameObject tempItem = Instantiate(item.gameObject); tempItem.transform.SetParent(scrollRootTran); SpareScrollItem itemTemp = tempItem.GetComponent(); itemTemp.RegistGrid(this); tempItem.SetActive(true); tempItem.transform.localScale = new Vector3(1, 1, 1); //itemTemp.spareInfo = infoConfig.infos[k]; itemTemp.spareInfo = infoConfig.infos[randomArr[k]]; itemTemp.SetScrollItem(); } } /// /// 点击零件 /// void OnPressDownSpare(PointerEventData data) { if (Input.GetMouseButton(0)) { scrollGo.SetActive(false); } MouseControl.Instance.SetDragSpare(this,()=> { this.spare_img.gameObject.SetActive(false); mouse_Btn.gameObject.SetActive(false); }); } /// /// 鼠标点击选择零件 /// /// void OnClickPoint(PointerEventData data) { if(GameConfig.state == GameState.Load) { if(GameDataConfig.Instance.pcId == 1) { if (spareInfo.type != SpareType.JiXiang || spareInfo.type != SpareType.ZhuBan) scrollGo.SetActive(!scrollGo.activeSelf); } } } /// /// 显示零件图片 /// public void ShowSpareImg() { this.spare_img.gameObject.SetActive(true); mouse_Btn.gameObject.SetActive(true); } /// /// 设置格子零件图片 /// /// /// public Texture2D GetSpareTex(SpareType spare) { for(int i=0;i< spareTexs.Count; i++) { if(spareTexs[i].type == spare) { return spareTexs[i].texture; } } return null; } } [Serializable] public class SpareTex { public Texture2D texture; public SpareType type; } }