2016-09-21 7 views
6

मैं देख रहा स्क्रीन से बड़ा नक्शा का उपयोग कर रहा हूं। इसलिए मुझे उस मानचित्र के चारों ओर पैन करने में सक्षम होना चाहिए। मुझे कैमरा और मानचित्र को क्लैंप करने में कोई समस्या है। मैं क्लैंप की चौड़ाई और ऊंचाई के रूप में छवि के आयाम का उपयोग करने में सक्षम होना चाहता हूं। समस्या इकाइयां है।मैं मानचित्र की सीमा कैसे बना सकता हूं और उस मानचित्र की पैनिंग को वही कैसे बना सकता हूं?

छवि 2144 x 1708 कैमरा ट्रांसपोजिशन एकल अंकों (14 x 7) या ऐसा कुछ है।

मेरे द्वारा उपयोग किए जा रहे सभी कोड नीचे दिए गए हैं।

private Vector3 mouseOrigin; // Position of cursor when mouse dragging starts 
private bool isPanning;  // Is the camera being panned? 

public bool useBoundary = true; 
public Vector2 boundaryMin; 
public Vector2 boundaryMax; 

public Image map; 


void Start() 
{ 

    Camera cam = Camera.main; 

    float mapRatio = map.rectTransform.rect.width/map.rectTransform.rect.height; 

    float mapScreenHeight = (1.5f * cam.orthographicSize); 
    float mapScreenWidth = (3f * mapScreenHeight) * cam.aspect; 

    boundaryMin = new Vector2(0, 1); 
    boundaryMax = new Vector2(map.rectTransform.rect.width, map.rectTransform.rect.height); 



} 


void Update() 
{ 
    // Get the left mouse button 
    if (Input.GetMouseButtonDown(0)) 
    { 
     // Get mouse origin 
     mouseOrigin = Input.mousePosition; 
     isPanning = true; 
    } 


    // Disable movements on button release 
    if (!Input.GetMouseButton(0)) isPanning = false; 

    // Rotate camera along X and Y axis 

    // Move the camera on it's XY plane 
    if (isPanning) 
    { 
     Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 
     Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0); 
     transform.Translate(move, Space.Self); 
     BoundaryCheck(); 
    } 

} 

void BoundaryCheck() 
{ 
    if (!useBoundary) 
     return; 

    Vector3 newPos = transform.position; 

    newPos.x = Mathf.Clamp(newPos.x, boundaryMin.x, boundaryMax.x); 
    newPos.y = Mathf.Clamp(newPos.y, boundaryMin.y, boundaryMax.y); 
    transform.position = newPos; 
} 

}

किसी भी मदद की बहुत सराहना की जाएगी।

+0

क्या आप आकार 14x7 की दूसरी छवि का उपयोग नहीं कर सकते? और आप संपादक में 'सीमामाइन' और 'सीमा मैक्स 'का पर्दाफाश कर रहे हैं लेकिन फिर' स्टार्ट() 'में आप उनके मूल्य को ओवरराइट करते हैं तो उन्हें सार्वजनिक बनाने का क्या मतलब है? –

+0

मैं चीजों को निजी में बदल सकता हूं, यह कोई मुद्दा नहीं है। एक पैनिंग मानचित्र में आप आमतौर पर स्क्रीन के आधार पर मानचित्र का आकार निर्धारित करते हैं? जब मोबाइल डिवाइस पर स्क्रीन बदलती है तो यह कैसे काम करता है? –

उत्तर

2

आप यूनिटी यूआई - स्क्रॉल रेक्ट का उपयोग करके ऐसा कर सकते हैं।

बाहर Unity UI - Scroll Rect - Introduction

+0

ऐसा लगता है कि यह काम कर सकता है! मुझे इसे जाने दो। –

+0

ज़रूर। अगर आपको उत्तर उपयोगी लगता है तो ऊपर उठो। –

1

मेरी राय में चेक आप कैमरे पैनिंग के लिए स्क्रिप्ट नीचे का उपयोग करने, जूमिंग और घूर्णन की आवश्यकता चाहिए। आप किसी अवांछनीय कार्य को बंद कर सकते हैं। अब आपके प्रश्न के मुताबिक आपको आंदोलन को प्रतिबंधित करना होगा (आपकी छवि सीमा के अनुसार)। बस आप अपनी सीमाओं के अनुसार भिन्न अक्ष पर कैमरे की गतिविधियों को प्रतिबंधित कर सकते हैं। आपको नक्शा की सीमाओं में क्यूब्स रखना चाहिए और कैमरे के आंदोलन और पैनिंग आदि की सीमा के रूप में अपनी स्थिति का उपयोग करना चाहिए

using UnityEngine; 
using System.Collections; 

public class CamMovementManager : MonoBehaviour 
{ 
    #region Vars 
    public float turnSpeed = 1.0f;  // Speed of camera turning when mouse moves in along an axis 
    public float panSpeed = 4.0f;  // Speed of the camera when being panned 
    public float zoomSpeed = 4.0f;  // Speed of the camera going back and forth 

    private Vector3 mouseOrigin; // Position of cursor when mouse dragging starts 
    private bool isPanning;  // Is the camera being panned? 
    private bool isRotating; // Is the camera being rotated? 
    private bool isZooming;  // Is the camera zooming? 

    private float pannPosLimit = 300f; 
    private int fovMin = 15; 
    private int fovMax = 90; 

    #endregion Vars 

    #region UnityEvents 

    void Update() 
    { 
     // Get the left mouse button 
     if (Input.GetMouseButtonDown(0)) 
     { 
      // Get mouse origin 
      mouseOrigin = Input.mousePosition; 
      isRotating = true; 
     } 

     // Get the right mouse button 
     if (Input.GetMouseButtonDown(1)) 
     { 
      // Get mouse origin 
      mouseOrigin = Input.mousePosition; 
      isPanning = true; 
     } 

     // Get the middle mouse button 
     if (Input.GetMouseButtonDown(2)) 
     { 
      // Get mouse origin 
      //mouseOrigin = Input.mousePosition; 
      //isZooming = true; 
     } 

     //changing fov on mouse scroll to zoomIn/out 
     float fov = Camera.main.fieldOfView; 
     fov += Input.GetAxis("Mouse ScrollWheel") * 10f; 
     fov = Mathf.Clamp(fov, fovMin, fovMax); 
     Camera.main.fieldOfView = fov;//*/ 


     // Disable movements on button release 
     if (!Input.GetMouseButton(0)) isRotating = false; 
     if (!Input.GetMouseButton(1)) isPanning = false; 
     if (!Input.GetMouseButton(2)) isZooming = false; 

     // Rotate camera along X and Y axis 
     if (isRotating) 
     { 
      Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 
      //Debug.Log("rotate pos : " + pos); 
      transform.RotateAround(transform.position, transform.right, -pos.y * turnSpeed); 
      transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed); 

     } 

     // Move the camera on it's XY plane 
     if (isPanning) 
     { 
      if (Input.mousePosition.y > pannPosLimit) 
      { 
       Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 

       Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0); 
       transform.Translate(move, Space.Self); 
      } 
     } 

     // Move the camera linearly along Z axis 
     if (isZooming) 
     { 
      Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 

      Vector3 move = pos.y * zoomSpeed * transform.forward; 
      transform.Translate(move, Space.World); 
     } 
    } 

    #endregion 

    #region CustomMethods 

    public void CamRotating() 
    { 

     Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 

     transform.RotateAround(transform.position, transform.right, -pos.y * turnSpeed); 
     transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed); 
    } 

    public void CamPanning() 
    { 
     Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 

     Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0); 
     transform.Translate(move, Space.Self); 
    } 

    public void CamZooming() 
    { 
     Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin); 

     Vector3 move = pos.y * zoomSpeed * transform.forward; 
     transform.Translate(move, Space.World); 
    } 

    #endregion CustomMethods 
} 
+0

मैंने वास्तव में आपके द्वारा पोस्ट किए गए कोड के समान कोड शुरू किया। मैं बॉक्स विधि का प्रयास करूंगा और देख सकता हूं कि स्क्रीन के आकार के साथ यह स्केल अच्छी तरह से है या नहीं। –

+0

बक्से आपके कैमरे के आंदोलन की सीमाएं हैं जैसा कि आपने छवि सीमा –

+0

बॉक्स स्थिति के बारे में बताया है, इसलिए आप केवल बॉक्स मेष से बाहर निकल सकते हैं –

संबंधित मुद्दे