Search Unity

Make SpriteRenderer Fit to the Window. Orthographic camera.

Discussion in '2D' started by AlexeyGapon, Jul 19, 2014.

  1. AlexeyGapon

    AlexeyGapon

    Joined:
    Jul 18, 2014
    Posts:
    4
    Hello Every one can anybody help me with that. I spend a week but can`t find how to do that.
    All answers i found here dose not work. PLease Help.
    How to Fit Game Window to Sprite aspect?
     
    Last edited: Jul 19, 2014
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    Don't really understand what you are trying to do. Can you explain better what you are trying to achieve?
     
  3. AlexeyGapon

    AlexeyGapon

    Joined:
    Jul 18, 2014
    Posts:
    4
    I am using Orthocamera. How can i set Spritrenderer to fit my gamewindow? With script not with designer.
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    You just repeated the title of the thread, you didn't clarify what you are trying to do. Do you have a sprite that you want to fill the full camera view? Do you mean scale? Pixel ratio? Be visible in the camera bounds? Match aspect ratio?

    Please describe what your goal is. You can't "fit a spriterender to a game window". Explain what the desired result that you want is, and I am sure that myself or someone else can help you get the result you are looking for.
     
  5. AlexeyGapon

    AlexeyGapon

    Joined:
    Jul 18, 2014
    Posts:
    4
    Ok so i have a sprite. I draged it into a orthoview and the Spriterenderer were created. What the size is of this sprite plane? Haw should i get the size of camera viewing gizmo? In openGL for example you can draw en image in any pleace using it`s screen coordinates for example i can draw an image with actual size of "1024x768" just setting the quad coordinates on screen space. How can i translate screen coordinates of plane into it`s world coordinates. Why different objects has different size but acale always = 1? Where is is the size parameter? How can i get the size of plane in pixels od the screen? Haw can i move my plane to fit for example screen rect "new Rect (100,50, 250, 350)" ?

    I need to display a sprite i ampaninig my camera changing transform.position and zooming it using camera.orthographicSize but the plane always hase fixed size. How it is possible me camera never move out of this plane? Always pan infront of the plane no metter what orthographicSize is.
     
  6. AlexeyGapon

    AlexeyGapon

    Joined:
    Jul 18, 2014
    Posts:
    4
    I found Out how to calculate size in Unity. Thats is tricky but it works.
    I am using my own classes for a reason to modify them in time. So here is the solution.



    Advice: In the designer set the camera Bounds smaller then bacground size.Otherwisw you should write some function to check FitZoom just like in this example "FirBorders"

    Code (CSharp):
    1. public class SizeF
    2.     {
    3.         public float Width  = 0f;
    4.         public float Height = 0f;
    5.  
    6.         /// <summary>
    7.         /// Constructor for reference types <see cref="BasicClasses+SizeF"/> class.
    8.         /// </summary>
    9.         public SizeF()
    10.         {}
    11.  
    12.         public SizeF(float w, float h)
    13.         {
    14.             Width = w;
    15.             Height = h;
    16.         }
    17.     }
    18.  
    19.  
    20.     ///Position of floats
    21.     public class PointF
    22.     {
    23.         public float X  = 0f;
    24.         public float Y = 0f;
    25.        
    26.         /// <summary>
    27.         /// Constructor for reference types <see cref="BasicClasses+SizeF"/> class.
    28.         /// </summary>
    29.         public PointF()
    30.         {}
    31.        
    32.         public PointF(float x, float y)
    33.         {
    34.             X = x;
    35.             Y = y;
    36.         }
    37.     }
    38.  
    39.  
    40. //**************************************
    41.    /// <summary>
    42.     ///Returns camera viewing Gizmo Size with units
    43.     /// </summary>
    44.     /// <returns>The camera size.</returns>
    45.     public SizeF GetCameraSize()
    46.     {
    47.         SizeF CamSize = new BasicClasses.SizeF ();
    48.  
    49.         CamSize.Height = Camera.main.orthographicSize * 2f;
    50.         CamSize.Width = (float) CamSize.Height * ((float)Screen.width / (float)Screen.height);
    51.         return CamSize;
    52.     }
    53.  
    54.     /// <summary>
    55.     /// Get the position of the main camera.
    56.     /// </summary>
    57.     /// <returns>The camera position.</returns>
    58.     public PointF GetCameraPosition ()
    59.     {
    60.         return new PointF (Camera.main.transform.position.x, Camera.main.transform.position.y);
    61.     }
    62.  
    63. //*****************************
    64. //Returns renderer bounds of the game object.
    65.     public SizeF GetGameObjectBounds (string name)
    66.     {
    67.         GameObject g = GameObject.Find (name);
    68.         return new SizeF (g.renderer.bounds.size.x, g.renderer.bounds.size.y);
    69.     }
    70.  
    71.     //Returns Transform of the game object.
    72.     public PointF GetGameObjectPosition (string name )
    73.     {
    74.         GameObject g = GameObject.Find (name);
    75.         return new PointF (g.transform.position.x, g.transform.position.y);
    76.     }
    77.  
    78. //*******
    79. /*calculate the bounds for camera paning*/
    80.      /// <summary>
    81.     /// Bounds of camera Paning
    82.     /// </summary>
    83.     void SetupBorders ()
    84.     {      
    85.         BasicClasses.PointF Pos  = GOInformer.GetGameObjectPosition  ("Background");
    86.         BasicClasses.SizeF  Size = GOInformer.GetGameObjectBounds ("Background");
    87.  
    88.  
    89.         this.BorderLeft   =  Pos.X - Size.Width / 2f;
    90.         this.BorderRight  =  Pos.X + Size.Width / 2f;;
    91.         this.BorderTop    =  Pos.Y + Size.Height / 2f;
    92.         this.BorderBottom =  Pos.Y - Size.Height / 2f;
    93.     }
    94. //*******************************
    95. /*Check if the camera Viewving gizmo is inside Border Bounds*/
    96.  
    97.  
    98.     /// <summary>
    99.     /// Fits the borders of the Background
    100.     /// </summary>
    101.     void FitBorders ()
    102.     {
    103.  
    104.         PointF CamPos   = CameraInformer.GetCameraPosition ();
    105.         SizeF CamBounds = CameraInformer.GetCameraSize ();
    106.  
    107.  
    108.        /*Paning bounds*/
    109.         ///higher then top
    110.         if (this.BorderTop  < CamPos.Y + CamBounds.Height / 2f)
    111.         {
    112.             Camera.main.transform.position =  new Vector3 (Camera.main.transform.position.x,
    113.                                                           this.BorderTop - CamBounds.Height / 2f,
    114.                                                           -10);
    115.         }
    116.  
    117.         //lover then bottom
    118.         if (this.BorderBottom > CamPos.Y - CamBounds.Height / 2f)
    119.         {
    120.             Camera.main.transform.position =  new Vector3 (Camera.main.transform.position.x,
    121.                                                           this.BorderBottom + CamBounds.Height / 2f,
    122.                                                           -10);
    123.         }
    124.  
    125.  
    126.         //more left then left
    127.         if (this.BorderLeft   > CamPos.X - CamBounds.Width  / 2f)
    128.         {
    129.             Camera.main.transform.position =  new Vector3 (this.BorderLeft + CamBounds.Width  / 2f,
    130.                                                           Camera.main.transform.position.y,
    131.                                                           -10);
    132.         }
    133.  
    134.  
    135.  
    136.         ///more right then right
    137.         if (this.BorderRight < CamPos.X + CamBounds.Width  / 2f)
    138.         {
    139.             Camera.main.transform.position =  new Vector3 (this.BorderRight - CamBounds.Width  / 2f,
    140.                                                          Camera.main.transform.position.y,
    141.                                                          -10);
    142.         }
    143.     }
    144.  
    145.