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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Could somebody explain to me what this code does and how it works?

Discussion in 'Getting Started' started by Jnanda313, May 16, 2015.

  1. Jnanda313

    Jnanda313

    Joined:
    Jun 1, 2014
    Posts:
    21
    I was following a certain tutorial and don't really understand what this does to the camera.

    public class PixelPerfectCamera : MonoBehaviour
    {

    public static float pixelsToUnits = 1f;
    public static float scale = 1f;
    public Vector2 nativeResolution = new Vector2 (240, 160);

    // Use this for initialization
    void Awake ()
    {
    var camera = GetComponent<Camera>();
    if (camera.orthographic)
    {
    scale = Screen.height/nativeResolution.y;
    pixelsToUnits *= scale;
    camera.orthographic = (Screen.height/2f)/pixelsToUnits;
    }
    }


    }
     
  2. Aaron_T

    Aaron_T

    Joined:
    Sep 30, 2014
    Posts:
    123
    I believe that it checks if your camera is orthographic (in comparison to perspective), and if so it then scales how far the camera can view depending on the vertical size of the screen.

    Tip for the future: Surround all code in code blocks so that it is easier to read. http://forum.unity3d.com/threads/using-code-tags-properly.143875/
     
    Jnanda313 likes this.
  3. Jnanda313

    Jnanda313

    Joined:
    Jun 1, 2014
    Posts:
    21
    Thanks and i will use code blocks from now on.