void mouseDragOrClick(Vector2 currentFlatMousePos) { //滑鼠左鍵點下後抓取滑鼠位置,並且開始判斷是拖曳或是點擊 if (mouseDragging) { Vector2 diffScreen = currentFlatMousePos - startClipPos; if (diffScreen.x + diffScreen.y != 0f) //滑鼠從點下到放開有移動就視為拖曳 { Vector2 diffRelative = new Vector2(diffScreen.x / Screen.width, diffScreen.y / Screen.height); Vector2 diff = startDragingPos - diffRelative * taiwanMap.terrainSize * taiwanMap.rangeScale; taiwanMap.newPos = ((int)diff.x, (int)diff.y); maybeClick = false; //如果是拖曳就不是點擊 } } if (Input.GetMouseButtonDown(0)) { startDragingPos = new Vector2(taiwanMap.newPos.x, taiwanMap.newPos.y); startClipPos = currentFlatMousePos; mouseDragging = true; maybeClick = true; } if (Input.GetMouseButtonUp(0)) { if (maybeClick) //點擊滑鼠就是放士兵或收士兵 { if (hit.transform.name == "Terrain") //點在圖台上表示放士兵 { float terrainRatio = taiwanMap.terrainSize * taiwanMap.rangeScale; (int x, int y)terrainRayHit = ((int)(hit.point.x * terrainRatio), (int)(hit.point.z * terrainRatio)); (int x, int y)hitTerrainPos = (terrainRayHit.x + taiwanMap.newPos.x, terrainRayHit.y + taiwanMap.newPos.y); var placeLongLat = GIS.TerrainToLonglat(hitTerrainPos); var troopID = troopManager.TroopMissingID("soldier"); // troopManager.TroopMove("soldier", troopID, placeLongLat.lng, placeLongLat.lat); messageBroker.AddTroop("soldier", troopID, placeLongLat.lng, placeLongLat.lat); } else if (hit.transform.name.StartsWith("ToonSoldier_WW2_demo_model")) //點到士兵表示收士兵 { hit.transform.gameObject.tag = "Finish"; //使用rayhit只能看到被hit到的Gameobject,但在原本的Troopmanager中採用dictionary進行Gameobject管理。 //直接刪除gameobject會造成錯誤。所以必須找到對應的dictionary key,透過管理系統進行刪除。這在士兵量太過 //龐大時效能會不好 string troopID = troopManager.GetDeletedSelectedTroopID(); messageBroker.RemoveTroop("soldier", troopID); } troopManager.redrawTroops = true; } maybeClick = false; mouseDragging = false; } }