1. var mouseOverColor = Color.blue;//声明变量为蓝色 2. private var originalColor : Color;//声明变量存储本来颜色 3. function Start () { 4. originalColor = renderer.sharedMaterial.color;//开始时得到物体本来着色 5. } 6. function OnMouseEnter () { 7. renderer.material.color = mouseOverColor;//当鼠标滑过时改变物体颜色为蓝色 8. } 9. 10. function OnMouseExit () { 11. renderer.material.color = originalColor;//当鼠标滑出时恢复物体本来颜色 12. } 13. 14. function OnMouseDown () { 15. var screenSpace = Camera.main.WorldToScreenPoint(transform.position);//三维物体坐标转屏幕坐标 16. //将鼠标屏幕坐标转为三维坐标,再算出物体位置与鼠标之间的距离 17. var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z)); 18. print("down"); 19. while (Input.GetMouseButton(0)) 20. { 21. //print("button0"); 22. var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z); 23. var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset; 24. transform.position = curPosition; 25. yield;//这个很重要,循环执行 26. }
|