缩放多个分辨率/不同设备的UI

时间:2016-06-06 08:30:30

标签: user-interface unity3d

我有一个非常简单的统一GUI,它有以下方案:

enter image description here

布雷克特等按钮。

GUI在PC上运行良好,并且在screen space : overlay上,因此应该自动调整以适应每个屏幕。

但是在平板电脑上,整个GUI在屏幕中央较小且缩小,元素周围有很大的边距(现在无法加入截图)

解决这个问题的方法是什么?是播放器设置还是项目设置?

2 个答案:

答案 0 :(得分:2)

自动缩放UI需要使用锚点,RecTransform的轴心点和Canvas Scaler组件的组合。没有图像或视频,理解它是很难。完全了解如何执行此操作非常重要,Unity为此提供了完整的视频教程。您可以观看here

此外,使用滚动条,滚动视图和其他类似的UI控件时,ContentSizeFitter组件也用于确保它们适合该布局。

答案 1 :(得分:0)

MovementRange存在问题。我们也必须扩大这个价值。 我这样做了:

public int MovementRange = 100;
    public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
    public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
    public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
    private int _MovementRange = 100;

    Vector3 m_StartPos;
    bool m_UseX; // Toggle for using the x axis
    bool m_UseY; // Toggle for using the Y axis
    CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
    CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input

    void OnEnable()
    {
        CreateVirtualAxes();
    }

    void Start()
    {
        m_StartPos = transform.position;
        Canvas c = GetComponentInParent<Canvas>();
        _MovementRange = (int)(MovementRange * c.scaleFactor);
        Debug.Log("Range:"+ _MovementRange);
    }

    void UpdateVirtualAxes(Vector3 value)
    {
        var delta = m_StartPos - value;
        delta.y = -delta.y;
        delta /= _MovementRange;
        if (m_UseX)
        {
            m_HorizontalVirtualAxis.Update(-delta.x);
        }

        if (m_UseY)
        {
            m_VerticalVirtualAxis.Update(delta.y);
        }
    }

    void CreateVirtualAxes()
    {
        // set axes to use
        m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
        m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);

        // create new axes based on axes to use
        if (m_UseX)
        {
            m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
            CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
        }
        if (m_UseY)
        {
            m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
            CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
        }
    }


    public void OnDrag(PointerEventData data)
    {
        Vector3 newPos = Vector3.zero;

        if (m_UseX)
        {
            int delta = (int)(data.position.x - m_StartPos.x);
            delta = Mathf.Clamp(delta, -_MovementRange, _MovementRange);
            newPos.x = delta;
        }

        if (m_UseY)
        {
            int delta = (int)(data.position.y - m_StartPos.y);
            delta = Mathf.Clamp(delta, -_MovementRange, _MovementRange);
            newPos.y = delta;
        }
        transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
        UpdateVirtualAxes(transform.position);
    }