private void UnHoverEntity() { if (hoveredEntiy != null) { hoveredEntiy.UnHover(); onUnHover(hoveredEntiy); } hoveredEntiy = null; }
public Action <SelectAbleEntity> onUnHover;//UI 和模型都需要订阅这个事件 hover只是头顶和模型底部 select 是头顶+模型底部+右侧UI,可同时存在 //三国类型的hover其实没啥用,slect才有用 private void HoverEntity(SelectAbleEntity entity) { if (hoveredEntiy != entity) //hover需要判断,点击不需要判断。重复点击,重复出发音效 { UnHoverEntity(); //先取消之前的hover hoveredEntiy = entity; entity.Hover(); onHover(entity); } }
private void OnTriggerExit(Collider other) { if (other.transform.tag == "InterAction") { Transform parentTrans = other.transform.parent; SelectAbleEntity entity = parentTrans.GetComponent <SelectAbleEntity>(); if (entity.selectType == ESelectType.Building) { if (list_InterBuilding.Contains(entity as Building)) { list_InterBuilding.Remove(entity as Building); } } else { if (list_Intertroop.Contains(entity as Troop)) { list_Intertroop.Remove(entity as Troop); } } } }
//右键点击一个 private void TroopActionOnEntity(SelectAbleEntity entity) { //Debug.Log("Troop Action on Entity"); foreach (Troop troop in selectedTroopList) { if (troop != null) { if (entity.selectType == ESelectType.Troop) { Troop targetTroop = entity as Troop; if (targetTroop.CanBeAttack) { troop.CommadAttackTroop(targetTroop); } else { troop.CommandFollowTroop(targetTroop); } } else if (entity.selectType == ESelectType.Building) { Building targetBuilding = entity as Building; if (targetBuilding.CanBeAttack) { troop.CommandAttackBuilding(targetBuilding); } else if (targetBuilding.CanTroopMoveInto) { troop.CommandMoveInToBuilding(targetBuilding); } else { troop.CommandMoveToPoint(targetBuilding.transform.position); } } } } }
private void OnTriggerEnter(Collider other) { // Debug.Log(other.gameObject + "ontigerStay"); if (other.transform.tag == "InterAction") { Transform parentTrans = other.transform.parent; SelectAbleEntity entity = parentTrans.GetComponent <SelectAbleEntity>(); if (entity.selectType == ESelectType.Building) { if (!list_InterBuilding.Contains(entity as Building)) { list_InterBuilding.Add(entity as Building); } } else { if (!list_Intertroop.Contains(entity as Troop)) { list_Intertroop.Add(entity as Troop); } } } }
private void OnUnHover(SelectAbleEntity entity) { }
void Update() { //If the game is not running if (GameMgr.Instacne.state != EGameState.Running || !canSelection) { return; } if (!EventSystem.current.IsPointerOverGameObject()) //没有在UI物体上 { // 点击事件--而且只针对ButtonDown RayCheck = Camera.main.ScreenPointToRay(Input.mousePosition); // Debug.DrawRay(RayCheck.origin, RayCheck.direction); if (Physics.Raycast(RayCheck, out Hit, Mathf.Infinity, RaycastLayerMask.value)) { Debug.DrawRay(RayCheck.origin, RayCheck.direction * 2000, Color.red); //Debug.Log(Hit.transform.name+ "sssssssssss"); SelectAbleEntity HitObj = null; if (Hit.transform.parent) { HitObj = Hit.transform.parent.GetComponent <SelectAbleEntity>(); } if (HitObj == null) { HitObj = Hit.transform.GetComponent <SelectAbleEntity>(); } //选择碰撞体 if (Input.GetMouseButtonDown(1))//右键Action { moveToObject.position = Hit.point; if (HitObj != null && selectedTroopList.Count > 0) //右键点击中某个对象 执行Action { TroopActionOnEntity(HitObj); } else //啥也没点击中移动 { if (selectedTroopList.Count > 0) //Troop 和Building 不会共存,就算共存也是Toop优先 { TroopActionOnPoint(Hit.point); } else if (selectedBuilding != null) { BuildingActionOnPoint(Hit.point); } } } else if (Input.GetMouseButtonDown(0)) //左键点击 只有点击下的那一瞬间会出发 ButtonUp不会触发 SelectionBox是针对ButtonUp { Debug.Log(Hit.transform.name + "sssssssssss" + " " + HitObj); if (HitObj != null) { if (HitObj.selectType == ESelectType.Building) { SelectBuilding(HitObj as Building); } else { Debug.Log(Hit.transform.name + "sssssssssss"); SelectTroop(HitObj as Troop); } } else //一个都没选中,取消选择 { UnSelecteAllEntity(); } } else if (!showSelectionBox) { // if (HitObj != null) // Hover 没有出现框选才可以hover { HoverEntity(HitObj); } else { UnHoverEntity(); } } } } else { //如果要使用这个判断,那就不能存在全屏 UI矩阵 //Debug.Log("SSSSSSSSSSS"+EventSystem.current.IsPointerOverGameObject() + Input.mousePosition.ToString()); } if (true) { return; } //Selection Box: ************************ if (Input.GetMouseButton(0)) { if (hasCreatedSelectionBox == false) //这个不代表显示,按下一瞬间这个hasCreatedSelectionBox=true,这个是显示FirstMousePos,而且只要ButtonUp 这个就会变成false { FirstMousePos = Input.mousePosition; hasCreatedSelectionBox = true; } //Check if the box size is above the minimal size. if (Vector3.Distance(FirstMousePos, Input.mousePosition) > MinBoxSize) { showSelectionBox = true; UnHoverEntity(); //出现框选要取消hover事件 //Activate the selection box object if it's not activated. if (SelectionBox.gameObject.activeSelf == false) { SelectionBox.gameObject.SetActive(true); } LastMousePos = Input.mousePosition; //Always save the last mouse position. //Calculate the box's size in the canvas: Vector3 CurrentMousePosUI = Input.mousePosition - Canvas.localPosition; Vector3 FirstMousePosUI = FirstMousePos - Canvas.localPosition; //Set the selection box size in the canvas. SelectionBox.GetComponent <RectTransform>().sizeDelta = new Vector2(Mathf.Abs(CurrentMousePosUI.x - FirstMousePosUI.x), Mathf.Abs(CurrentMousePosUI.y - FirstMousePosUI.y)); Vector3 Center = (FirstMousePos + Input.mousePosition) / 2 - Canvas.localPosition; //Calculate the center of the selection box. SelectionBox.GetComponent <RectTransform>().localPosition = Center; //Set the selection position. } } if (Input.GetMouseButtonUp(0) && showSelectionBox == true) //上面只是画图,和选择无关 { hasCreatedSelectionBox = false; showSelectionBox = false; SelectionBox.gameObject.SetActive(false); //这里我们逆向思维,不使用碰撞检测 List <Troop> tempTroopList = new List <Troop>(); foreach (KeyValuePair <int, Troop> pair in EntityMgr.Instacne.dic_Troop) { Vector3 wordPos = pair.Value.rectSelectedPoint.position; Vector3 screenPos = Camera.main.WorldToScreenPoint(wordPos); if ((screenPos.x > FirstMousePos.x && screenPos.x < LastMousePos.x) || (screenPos.x < FirstMousePos.x && screenPos.x > LastMousePos.x)) { if ((screenPos.y > FirstMousePos.y && screenPos.y < LastMousePos.y) || (screenPos.y < FirstMousePos.y && screenPos.y > LastMousePos.y)) { tempTroopList.Add(pair.Value); } } } if (tempTroopList.Count > 0) { UnSelecteAllEntity(); SelectTroopList(tempTroopList); } } }