Under the orthographic camera, the function of dragging the wheel button and scrolling the wheel to zoom is realized

A function has been implemented. Press the mouse wheel button to drag all the objects in the field of view (in fact, the camera itself is moving), and scroll the wheel to zoom the content (in fact, change the size of the camera field of view)

 

The effect is as follows

 

 

code here

 

1  using UnityEngine;
 2  using UnityEngine.UI;
 3  
4  ///  <summary> 
5  /// mounted on the main camera
 6  ///  </summary> 
7  public  class Cont : MonoBehaviour
 8 {  9
 private      new  Camera camera;
 10      private  bool isDrag = false ; Vector3 startMousePosition ; // The position of the mouse on the screen when dragging starts 12 
private      Vector3 startCameraPosition ; //
     The position of the camera in the world space when dragging starts 
13  
14      private Text text; // Display screen resolution, can be ignored 
15      private  void Start()
 16      {
 17          text = GameObject.FindWithTag( " Text " ).GetComponent<Text> ();
 18          camera = GetComponent<Camera> ();
 19          temp = camera.orthographicSize;
 20  
21          text.text = camera.scaledPixelWidth + "   " + camera.scaledPixelHeight;
 22          dragScaleX = 1.0f/ camera.scaledPixelHeight; // horizontal scaling value 
23          dragScaleY = 1.0f / camera.scaledPixelHeight; // vertical scaling value 
24      }
 25  
26      void Update()
 27      {
 28          Drag(); // drag 
29          Scale(); // wheel scaling 
30      }
 31  
32      [SerializeField]
 33      private  flo at ScrollScale = 0.1f ;
 34      private  float temp;
 35      private  floattempAxis;
 36      private  void Scale() // Roller scaling 
37      {
 38          tempAxis = Input.GetAxis( " Mouse ScrollWheel " ); // Get the wheel input, -1/0/1 
39          if (tempAxis == 0 ) return ;
 40  
41          temp -= tempAxis * ScrollScale * temp;
 42          if (temp < 0 ) //Control to prevent the field of view from being negative, causing the content to be centered symmetric
 43          {
 44              temp += tempAxis * ScrollScale * temp;
 45              return ;
46          }
 ​​47          camera.orthographicSize = temp;
 48      }
 49  
50      private  void Drag() // Drag 
51      {
 52          if (Input.GetMouseButtonDown( 2 )) // Roll button 
53          {
 54              isDrag = true ;
 55              startMousePosition = Input.mousePosition; // Record mouse position before dragging 
56              startCameraPosition = transform.localPosition; // Record camera position before dragging 
57          }
 58         if (Input.GetMouseButtonUp(2))
59         {
60             isDrag = false;
61         }
62 
63         MoveScene();
64     }
65     [SerializeField]
66     private float dragScaleX = 0.001f;
67     [SerializeField]
68     private float dragScaleY = 0.001f;
69     private Vector3 worldDir;
70     private void MoveScene()
71     {
72         if (!isDrag) return;
73 
74         worldDir = (startMousePosition - Input.mousePosition) * 2 * camera.orthographicSize;
75         worldDir.x *= dragScaleX;
76         worldDir.y *= dragScaleY;
77         transform.localPosition = startCameraPosition + worldDir;
78     }
79 
80     private void OnGUI()
81     {
82         text.text = camera.pixelWidth + "  " + camera.pixelHeight;
83     }
84 }

 

When using it, just mount the component on the main camera

The function is completed, but there is one thing that is very puzzling, why the 22 and 23 lines of the code are all 1/camera.scaledPixelHeight. If it is not camera.scaledPixelHeight but camera.scaledPixelWidth, the mouse will lag or lead, please advise.

Reprinted in: https://www.cnblogs.com/Yukisora/p/8747167.html

Guess you like

Origin blog.csdn.net/a1808508751/article/details/101353100