如何在Unity中通过脚本编辑“参考分辨率”?

时间:2019-05-27 05:41:24

标签: c# unity3d

我想在实时游戏中使用C#脚本更改Reference Resolutuion,我该怎么做?

st

因为我想以最简单的方式扩展控件

enter image description here enter image description here

3 个答案:

答案 0 :(得分:1)

您无法设置参考分辨率,但可以使用UISclleMode:恒定像素大小来计算和设置比例因子

enter image description here

void SetReferenceResolution(Vector2 m_ReferenceResolution,float m_MatchWidthOrHeight = 1f)
{
    private const float kLogBase = 2;
    float logWidth = Mathf.Log(screenSize.x / m_ReferenceResolution.x, kLogBase);
    float logHeight = Mathf.Log(screenSize.y / m_ReferenceResolution.y, kLogBase);
    float logWeightedAverage = Mathf.Lerp(logWidth, logHeight, m_MatchWidthOrHeight);
    GetComponent<Canvas>().scaleFactor = Mathf.Pow(kLogBase, logWeightedAverage);
}

答案 1 :(得分:0)

就像CanvasScaler的所有其他属性一样,您可以在脚本之类的脚本中简单地使用CanvasScaler.referenceResolution属性

GetComponent<CanvasScaler>().referenceResolution = new Vector2(xSize, ySize);

答案 2 :(得分:0)

  

UI缩放模式:Constant Pixel Size

简单模板:

public class Scale : MonoBehaviour {

    public float scale = 1f; // 1 by default

    public void Update()
    {
        GetComponent<Canvas>().scaleFactor = scale;
    }
}