void Update() { // 点击鼠标左键取得、移动、旋转、翻转七巧板 if (Input.GetMouseButtonDown(0)) { // 将屏幕坐标转换成世界坐标 v3 = Camera.main.ScreenToWorldPoint(Input.mousePosition); // 将三维坐标转换成二维坐标 v2 = new Vector2(v3.x, v3.y); // 发射射线 hit = Physics2D.Raycast(v2, Vector2.zero); if (hit.collider != null && hit.collider.tag.Equals("Tangram")) { TangramUI.StartGame(); isMouseDownL = true; // 获得碰撞物体名字 hitName = hit.collider.gameObject.name; // 如果点击的是旋转精灵,则翻转6号七巧板 if (hitName.Equals(tn [0])) { Vector3 tempScale = tg [6].transform.localScale; tempScale.x *= -1; tg [6].transform.localScale = tempScale; TangramAA.TangramRefreshPoint(hit.collider.gameObject); // tg [6].transform.Rotate (180, 0, 0); rotateCount += 2; TangramUI.RotateCount(rotateCount); } else { // 如果点击一个七巧板后直接点击另一个七巧板,则初始化上个七巧板,并渲染当前七巧板 if (isSpriteDown && hitNameOld != hitName) { InitialTangramBack(hitNameOld); } isSpriteDown = true; isClick = true; mouseDownTime = Time.time; hitNameOld = hitName; RenderTangram(hitName); } } else { // 如果点击空白区域,初始化七巧板 isMouseDownL = false; isClick = false; if (isSpriteDown) { InitialTangramBack(hitName); InitialTangramBack(hitNameOld); isSpriteDown = false; hitNameOld = hitName; } } } // 当按下并移动鼠标时同步移动七巧板 if (isMouseDownL && isSpriteDown) { if (lastMousePosition != Vector3.zero) { Vector3 offset = Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePosition; if (offset != Vector3.zero) { isMove = true; hit.transform.position = hit.transform.position + offset; ScaleTangramChange(hit.collider.gameObject, IsInMakeArea(hit.collider.gameObject)); } } lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); } // 单击鼠标右键初始化其他七巧板,若有翻转则弹出翻转精灵 if (Input.GetMouseButtonDown(1)) { v3 = Camera.main.ScreenToWorldPoint(Input.mousePosition); v2 = new Vector2(v3.x, v3.y); hit = Physics2D.Raycast(v2, Vector2.zero); if (isMouseDownR) { tg [0].transform.position = gamedata.tangramPosition [0]; isMouseDownR = false; } if (isSpriteDown && hitNameOld != null) { InitialTangramBack(hitNameOld); isSpriteDown = false; } if (hit.collider != null && IsInMakeArea(hit.collider.gameObject) && hit.collider.tag.Equals("Tangram")) { isMouseDownR = true; isSpriteDown = true; hitName = hit.collider.gameObject.name; if (hitName.Equals(tn [6])) { RenderTangram(hitName); tg [0].transform.position = new Vector3(hit.transform.position.x, hit.transform.position.y, 0); hitNameOld = hitName; } } } // 松开鼠标左键时判断七巧版状态并执行相应操作 if (Input.GetMouseButtonUp(0)) { isMouseDownL = false; lastMousePosition = Vector3.zero; // 判断七巧板的位置,决定是否将其移回,移回后刷新特征点 if (isSpriteDown && !IsInMakeArea(hit.collider.gameObject)) { MoveTangramBack(hit.collider.gameObject); TangramAA.TangramRefreshPoint(hit.collider.gameObject); } // 判断是否移动过,决定移动次数和自动调整 if (isMove) { moveCount++; TangramUI.MoveCount(moveCount); soundPlay.PlayMoveSound(); isMove = false; // 如果对七巧板进行过操作,则自动调整七巧板位置 if ((PlayerPrefs.GetInt("isAutomaticAdjustment", 1) == 1) && isSpriteDown && IsInMakeArea(hit.collider.gameObject)) { TangramAA.TangramRefreshPoint(hit.collider.gameObject); TangramAA.TangramAutAdj(hit.collider.gameObject); } } else { // 每完成一次翻转或点击其他地方时初始化旋转精灵坐标 tg [0].transform.position = gamedata.tangramPosition [0]; // 如果是有效单击(响应时间内的单击)七巧板则旋转七巧板 if (isSpriteDown && isClick && IsInMakeArea(hit.collider.gameObject) && !hitName.Equals(tn [0])) { clickTime = Time.time - mouseDownTime; if (clickTime < 0.3f) { hit.transform.Rotate(0, 0, -45); rotateCount++; TangramUI.RotateCount(rotateCount); soundPlay.PlayMoveSound(); isClick = false; // 旋转后不调整位置,但刷新特征点 if ((PlayerPrefs.GetInt("isAutomaticAdjustment", 1) == 1) && isSpriteDown && IsInMakeArea(hit.collider.gameObject)) { TangramAA.TangramRefreshPoint(hit.collider.gameObject); } } } } } if (Input.GetMouseButtonUp(1)) { lastMousePosition = Vector3.zero; isClick = false; } }