Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to change TargetDPI Value in Project Settings > Player > Resolution Scaling via script?

Discussion in 'Scripting' started by foxyyhappyw, Feb 20, 2023.

  1. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    59
    Hey,
    How to change TargetDPI Value in Project Settings > Player > Resolution Scaling via script?



    in other forums i only found:
    Code (CSharp):
    1. void Start()
    2.     {
    3.         // Set the target Fixed DPI for this quality setting to be half of the default.
    4.         QualitySettings.resolutionScalingFixedDPIFactor = 0.5f;
    5.     }
    but IT'S NOT what I'm looking for because it changes the VALUE in URP-Balanced > Quality > Render Scale value, not in Project Settings

    so, how can i change TargetDPI Value via script?
     
  2. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    You can change the Target DPI value in the Project Settings > Player > Resolution Scaling settings via script by using the PlayerSettings class.

    The PlayerSettings class provides a way to access and modify various player settings in Unity, including the Target DPI value. You can use the SetTargetDevice method to set the Target DPI value to a specific DPI value. Here's an example of how to use it:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class ChangeTargetDPI : MonoBehaviour
    5. {
    6.     [SerializeField] private int targetDPI;
    7.  
    8.     private void Awake()
    9.     {
    10.         PlayerSettings.SetTargetDevice(TargetDevice.Screen, targetDPI);
    11.     }
    12. }
    In this example, the ChangeTargetDPI class is a script that you can attach to a GameObject in your scene. It has a serialized field targetDPI, which is the value you want to set as the Target DPI value.

    In the Awake method, the PlayerSettings.SetTargetDevice method is called to set the Target DPI
     
  3. foxyyhappyw

    foxyyhappyw

    Joined:
    Aug 8, 2022
    Posts:
    59
    I dont know why, but thats not working for me

    I assigned 'using UnityEngine' and 'using UnityEditor';
     
  4. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    Apologies. This has been deprecated. If you're trying to set the targetDPI, you can use the following snippet.

    Screen.dpi = targetDPI;
     
  5. restush96

    restush96

    Joined:
    May 28, 2019
    Posts:
    120
    @Leuki it's read-only in 2022.3