如何使地图的边框和该地图的平移相同?

Tim*_*ley 6 unity-game-engine

我使用的地图比我查看的屏幕大.因此,我需要能够平移该地图.夹紧相机和地图时遇到问题.我希望能够使用图像的尺寸作为夹具的宽度和高度.问题是单位.

图像是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;
}
Run Code Online (Sandbox Code Playgroud)

}

任何帮助将不胜感激.

Anm*_*kar 2

您可以使用 Unity UI - 滚动矩形来完成此操作。

查看Unity UI - 滚动矩形 - 简介