Question: I want to change `Reference Resolutuion` with C# script in real time game, how can I make it?

[![st](https://i.stack.imgur.com/Vpszx.png)](https://i.stack.imgur.com/Vpszx.png)

Because I want to scale my controls with easiest way

[![enter image description here](https://i.stack.imgur.com/ZXxoA.png)](https://i.stack.imgur.com/ZXxoA.png)
[![enter image description here](https://i.stack.imgur.com/cXRG3.png)](https://i.stack.imgur.com/cXRG3.png)

Answer: 
Like all the other properties of [`CanvasScaler`](https://docs.unity3d.com/2018.3/Documentation/ScriptReference/UI.CanvasScaler.html) you can simply use the [`CanvasScaler.referenceResolution`](https://docs.unity3d.com/2018.3/Documentation/ScriptReference/UI.CanvasScaler-referenceResolution.html) property in a script like

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

```
You cannot set reference resolution, but can caculate and set the **Scale Factor** with UISclleMode: **Constant Pixel Size**.

[![enter image description here](https://i.stack.imgur.com/KVIvw.png)](https://i.stack.imgur.com/KVIvw.png)

```
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);
}

```