纳金网

标题: 屏幕滑动旋转的代码 [打印本页]

作者: 王者再临    时间: 2015-2-27 00:48
标题: 屏幕滑动旋转的代码
屏幕滑动旋转代码
  1. 用相机来做,using UnityEngine;

  2. using System.Collections;

  3. public class CRLuo_Camera_FingerTipRotation : MonoBehaviour

  4. {

  5. public string _ = “-=<CRLuo鼠标或触摸摄像机旋转控制程序>=-”;

  6. public string __ = “一级左右转对象”;

  7. public GameObject R_Y_Obj;

  8. public GameObject R_X_Obj;

  9. public string ____ = “三级摄像机对象”;

  10. //public Camera MainCamera;

  11. public GameObject Cube;

  12. public string _____ = “旋转鼠标速度”;

  13. public float MouseMoveSpeed = 10f;

  14. public string ______ = “推拉鼠标速度”;

  15. public float MouseScaleSpeed = 10f;

  16. public string _______ = “默认旋转角度(Z无效)”;

  17. public Vector3 Default_R;

  18. public string ________ = “默认摄像机距离”;

  19. public float Default_Distance = 5;

  20. //public string _______ = “默认焦距”;

  21. //public float Default_FOV = 60;

  22. Vector2 MousePotOld;

  23. float Distance_Old;

  24. float Distance_New;

  25. public string _________ = “界面说明显示开关”;

  26. public bool ShowRadme;

  27. void Start()

  28. {

  29. ResetPos();

  30. }

  31. void ResetPos()

  32. {

  33. R_Y_Obj.transform.localRotation = Quaternion.Euler(new Vector3(0, Default_R.y, 0));

  34. R_X_Obj.transform.localRotation = Quaternion.Euler(new Vector3(Default_R.x, 0, 0));

  35. // MainCamera.transform.localPosition = new Vector3(0, 0, -Default_Distance);

  36. Cube.transform.localPosition = new Vector3(0, 0, -Default_Distance+5);

  37. //MainCamera.fieldOfView = Default_FOV;

  38. }

  39. void Update()

  40. {

  41. if (Input.GetMouseButtonDown(0))

  42. {

  43. MousePotOld = Input.mousePosition;

  44. }

  45. else if (Input.GetMouseButton(0))

  46. {

  47. if (MousePotOld != null)

  48. {

  49. R_Y_Obj.transform.Rotate(new Vector3(0, (Input.mousePosition.x - MousePotOld.x) * Time.deltaTime * MouseMoveSpeed, 0));

  50. R_X_Obj.transform.Rotate(new Vector3(-(Input.mousePosition.y - MousePotOld.y) * Time.deltaTime * MouseMoveSpeed, 0, 0));

  51. MousePotOld = Input.mousePosition;

  52. }

  53. }

  54. if (Input.GetAxis(“Mouse ScrollWheel”) != 0)

  55. {

  56. Debug.Log(Input.GetAxis(“Mouse ScrollWheel”));

  57. //MainCamera.transform.localPosition = new Vector3(0, 0, MainCamera.transform.localPosition.z + Input.GetAxis(“Mouse ScrollWheel”)* MouseScaleSpeed);

  58. Cube.transform.localPosition = new Vector3(0, 0, Cube.transform.localPosition.z + Input.GetAxis(“Mouse ScrollWheel”)* MouseScaleSpeed);

  59. }

  60. if (Input.touchCount > 1)

  61. {

  62. Touch touch1 = Input.touches[0];

  63. Touch touch2 = Input.touches[1];

  64. if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved)

  65. {

  66. Distance_New = Vector2.Distance(touch1.position, touch2.position);

  67. if (Distance_Old != null)

  68. {

  69. //MainCamera.transform.localPosition = new Vector3(0, 0, MainCamera.transform.localPosition.z - (Distance_New - Distance_Old) * MouseScaleSpeed);

  70. Cube.transform.localPosition = new Vector3(0, 0, Cube.transform.localPosition.z + Input.GetAxis(“Mouse ScrollWheel”)* MouseScaleSpeed);

  71. }

  72. Distance_Old = Distance_New;

  73. }

  74. }

  75. }

  76. void OnGUI()

  77. {

  78. //界面按钮提示

  79. if (ShowRadme)

  80. {

  81. GUI.Label(new Rect(Screen.width * 0.5f - 100, Screen.height * 0.1f - 40f, 200, 100), “-=<<摄像机旋转程序>>=-”);

  82. GUI.Label(new Rect(Screen.width * 0.5f - 100, Screen.height * 0.1f - 20f, 200, 100), “点击/滑动旋转”);

  83. GUI.Label(new Rect(Screen.width * 0.5f - 100, Screen.height * 0.1f, 200, 100), “滚轮/双指缩放”);

  84. if (GUI.Button(new Rect(Screen.width * 0.5f - 100, Screen.height * 0.9f - 50, 200, 100), “Reset”))

  85. {

  86. ResetPos();

  87. }

  88. }

  89. }

  90. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5