/// <summary> /// 给单位的ItemPanel视图设置鼠标停留、离开事件(用于显示提示视图)、还有购买物品 /// </summary> private void SetItemPanelMouseEvent() { EventTrigger.Entry onMouseEnter = new EventTrigger.Entry { eventID = EventTriggerType.PointerEnter }; onMouseEnter.callback.AddListener(eventData => { if (itemGrid.item == null) { return; } if (itemTipsView == null) { itemTipsView = GameObject.Instantiate <ItemTipsView>(ItemTipsViewPrefab, canvas.transform); itemTipsView.BindingContext = new ItemViewModel(); } // 设置提示窗口出现位置 itemTipsView.transform.SetParent(transform); (itemTipsView.transform as RectTransform).anchoredPosition = new Vector2((transform as RectTransform).sizeDelta.x / 2, (transform as RectTransform).sizeDelta.y / 2); itemTipsView.transform.SetParent(canvas.transform); itemTipsView.BindingContext.Modify(itemGrid); itemTipsView.Reveal(); }); EventTrigger.Entry onMouseExit = new EventTrigger.Entry { eventID = EventTriggerType.PointerExit }; onMouseExit.callback.AddListener(eventData => { if (itemGrid.item == null) { return; } itemTipsView.Hide(immediate: true); }); // 右键单击购买物品的事件 EventTrigger.Entry onMouseClick = new EventTrigger.Entry { eventID = EventTriggerType.PointerClick }; onMouseClick.callback.AddListener(eventData => { // 当物品不处于冷却状态时,才能购买此物品 if (Input.GetMouseButtonUp(1) && !itemGrid.IsCoolDowning) { itemTipsView.Hide(); storeView.Sell(itemGrid); } }); EventTrigger eventTrigger = GetComponent <EventTrigger>(); eventTrigger.triggers.Clear(); eventTrigger.triggers.Add(onMouseEnter); eventTrigger.triggers.Add(onMouseExit); eventTrigger.triggers.Add(onMouseClick); }
private void BindTipsViewEvent(ItemGrid itemGrid, ItemPanelView itemPanelView) { // 鼠标进入事件 var enterViewEntry = new EventTrigger.Entry { eventID = EventTriggerType.PointerEnter, }; enterViewEntry.callback.AddListener(eventData => { if (itemGrid.item == null) { return; } if (itemTipsView == null) { itemTipsView = GameObject.Instantiate <ItemTipsView>(ItemTipsViewPrefab, canvas.transform); itemTipsView.BindingContext = new ItemViewModel(); } // 设置提示窗口出现位置 itemTipsView.transform.SetParent(itemPanelView.transform); (itemTipsView.transform as RectTransform).anchoredPosition = new Vector2((itemPanelView.transform as RectTransform).sizeDelta.x / 2, (itemPanelView.transform as RectTransform).sizeDelta.y / 2); itemTipsView.transform.SetParent(canvas.transform); itemTipsView.BindingContext.Modify(itemGrid); itemTipsView.Reveal(); }); // 鼠标离开事件 var exitViewEntry = new EventTrigger.Entry { eventID = EventTriggerType.PointerExit }; exitViewEntry.callback.AddListener(eventData => { if (itemGrid.item == null) { return; } itemTipsView.Hide(immediate: true); }); // eventTrigger添加监听事件 EventTrigger eventTrigger = itemPanelView.GetComponent <EventTrigger>(); eventTrigger.triggers.Add(enterViewEntry); eventTrigger.triggers.Add(exitViewEntry); }
private void Init() { UICamera = GameObject.Find("UICamera").GetComponent <Camera>(); canvas = GameObject.Find("Canvas").GetComponent <Canvas>(); maskImage = new Image[itemViews.Count()]; for (int i = 0; i < itemViews.Count(); i++) { var itemView = itemViews[i]; maskImage[i] = itemView.transform.Find("ItemImagePanel").GetComponentInChildren <Mask>().GetComponent <Image>(); } itemGrids = characterMono.characterModel.itemGrids; for (int i = 0; i < itemViews.Count(); i++) { ItemPanelView itemPanelView = itemViews[i]; ItemGrid itemGrid = itemGrids[i]; itemPanelView.itemGrid = itemGrid; itemPanelView.BindingContext = new ItemViewModel(); itemPanelView.BindingContext.Modify(itemGrid); // 为每一个ItemPanelView添加鼠标进入和离开事件 // 鼠标进入事件 var enterViewEntry = new EventTrigger.Entry { eventID = EventTriggerType.PointerEnter, }; enterViewEntry.callback.AddListener(eventData => { if (itemGrid.item == null) { return; } if (itemTipsView == null) { itemTipsView = GameObject.Instantiate <ItemTipsView>(ItemTipsViewPrefab, canvas.transform); itemTipsView.BindingContext = new ItemViewModel(); } // 设置提示窗口出现位置 itemTipsView.transform.SetParent(itemPanelView.transform); (itemTipsView.transform as RectTransform).anchoredPosition = new Vector2((itemPanelView.transform as RectTransform).sizeDelta.x / 2, (itemPanelView.transform as RectTransform).sizeDelta.y / 2); itemTipsView.transform.SetParent(canvas.transform); itemTipsView.BindingContext.Modify(itemGrid); itemTipsView.Reveal(); }); // 鼠标离开事件 var exitViewEntry = new EventTrigger.Entry { eventID = EventTriggerType.PointerExit }; exitViewEntry.callback.AddListener(eventData => { if (itemGrid.item == null) { return; } itemTipsView.Hide(immediate: true); }); // eventTrigger添加监听事件 EventTrigger eventTrigger = itemPanelView.GetComponent <EventTrigger>(); eventTrigger.triggers.Add(enterViewEntry); eventTrigger.triggers.Add(exitViewEntry); } }