void OnButCreateDefenderUp(BaseEventData data) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitinfo; if (IsHitGround(ray, out hitinfo)) { if (IsTileUseAble(hitinfo)) { Vector3 hitpos = new Vector3(hitinfo.point.x, 0, hitinfo.point.z); Vector3 gridpos = TileObject.Instance.transform.position; float tilesize = TileObject.Instance.tileSize; hitpos.x = gridpos.x + (int)((hitpos.x - gridpos.x) / tilesize) * tilesize + tilesize * 0.5f; hitpos.z = gridpos.z + (int)((hitpos.z - gridpos.z) / tilesize) * tilesize + tilesize * 0.5f; GameObject go = data.selectedObject; if (go.name.Contains("1")) { if (SetPoint(-15)) { Defender.Create <Defender>(hitpos, new Vector3(0, 180, 0)); } } else if (go.name.Contains("2")) { if (SetPoint(-20)) { Defender.Create <Archer>(hitpos, new Vector3(0, 180, 0)); } } } } m_isSelectedSoldierButton = false; }
// 抬起 "创建防守单位按钮" 创建防守单位 void OnButCreateDefenderUp(BaseEventData data) { // 创建射线 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitinfo; // 检测是否与地面相碰撞 if (Physics.Raycast(ray, out hitinfo, 1000, m_groundlayer)) { // 如果选中的是一个可用的格子 if (TileObject.Instance.getDataFromPosition(hitinfo.point.x, hitinfo.point.z) == (int)Defender.TileStatus.GUARD) { // 获得碰撞点位置 Vector3 hitpos = new Vector3(hitinfo.point.x, 0, hitinfo.point.z); // 获得Grid Object坐位位置 Vector3 gridPos = TileObject.Instance.transform.position; // 获得格子大小 float tilesize = TileObject.Instance.tileSize; // 计算出所点击格子的中心位置 hitpos.x = gridPos.x + (int)((hitpos.x - gridPos.x) / tilesize) * tilesize + tilesize * 0.5f; hitpos.z = gridPos.z + (int)((hitpos.z - gridPos.z) / tilesize) * tilesize + tilesize * 0.5f; // 获得选择的按钮GameObject,将简单通过按钮名字判断选择了哪个按钮 GameObject go = data.selectedObject; if (go.name.Contains("1")) //如果按钮名字包括“1” { if (SetPoint(-15)) // 减15个铜钱,然后创建近战防守单位 { Defender.Create <Defender>(hitpos, new Vector3(0, 180, 0)); } } else if (go.name.Contains("2")) // 如果按钮名字包括“2” { if (SetPoint(-20)) // 减20个铜钱,然后创建远程防守单位 { Defender.Create <Archer>(hitpos, new Vector3(0, 180, 0)); } } } } m_isSelectedButton = false; }
void OnButCreateDefenderUp(BaseEventData data) //抬起“创建防守单位按钮” { print("test 144"); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //创建射线 RaycastHit hitinfo; //保存射线信息 print("test 147"); if (Physics.Raycast(ray, out hitinfo, 1000, m_groundlayer)) //检测是否与地面碰撞 { print("test 150"); //如果选中一个可用的格子 if (TileObject.Instance.getDataFromPosition(hitinfo.point.x, hitinfo.point.z) == (int)Defender.TileStatus.GUARD) { print("test 154"); Vector3 hitpos = new Vector3(hitinfo.point.x, 0, hitinfo.point.z); //获得碰撞点位置 Vector3 gridPos = TileObject.Instance.transform.position; //获得Grid的坐标位置 print(gridPos); float tilesize = TileObject.Instance.tileSize; //获得格子大小 hitpos.x = gridPos.x + (int)((hitpos.x - gridPos.x) / tilesize) * tilesize + tilesize * 0.5f; hitpos.z = gridPos.z + (int)((hitpos.z - gridPos.z) / tilesize) * tilesize + tilesize * 0.5f; //计算格子的中心位置 GameObject go = data.selectedObject; //获得选择按钮 if (go.name.Contains("1")) //如果按钮名字包括"1" { if (SetMoney(-15)) //减少15块钱,创建近战单位 { Defender.Create <Defender>(hitpos, new Vector3(0, 180, 0)); } } else if (go.name.Contains("2")) { if (SetMoney(-20)) //减少20块,创建远程单位 { Defender.Create <Archer>(hitpos, new Vector3(0, 180, 0)); } } } } print("test 174"); m_isSelectedButton = false; }
//抬起创建防守单位按钮 void OnbutCreateDefendrUp(BaseEventData data) { Debug.Log(1); GameObject go = data.selectedObject; //创建射线 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitinfo; //计算是否与地面相撞 if (Physics.Raycast(ray, out hitinfo, 1000, m_groundlayer)) { Debug.Log(go.name); //如果选中的是个空格子 if (TileObject.get.getData(hitinfo.point.x, hitinfo.point.z) == (int)Defender.TileStatus.FREE) { //获得选中位置 Vector3 pos = new Vector3(hitinfo.point.x, 0, hitinfo.point.z); //使位置位于格子中心 pos.x = (int)pos.x + TileObject.get.tileSize * 0.5f; pos.z = (int)pos.z + TileObject.get.tileSize * 0.5f; //根据按钮名称创建不同防守单位 if (go.name.Contains("1")) //创建近战单位 { if (SetPoint(-15)) //消耗15铜钱 { Defender.Create <Defender>(pos, new Vector3(0, 180, 0)); } } else //创建远程单位 { if (SetPoint(-20)) { Defender.Create <Archer>(pos, new Vector3(0, 180, 0)); } } } } m_isSelectedButton = false; }