title: How to resize a squares to fit inside a screen using Unity3d
body: <p>I'm new to Unity3d. Is it easy to write a program who scale and distribute squares after the size of the screen. It would have been cool to have it in 3D, but 2D is also working for my case. The animated Gif, show what I want. Hope someone can help me with an example of how this can be done. Tanks!</p>
  
  <p>To calculte the square size I use the following function:        </p>
  
  <pre class="lang-c# prettyprint-override"><code>        public static double getPieceSquareCalculated(int height, int width, int numberOfPieces)
      {
          double px = Math.Ceiling(Math.Sqrt(numberOfPieces * width / height)) +1;
          px = px == 0 ? 1 : px; 
          double sx, sy;
          if (Math.Floor(px * height / width) * px &lt; numberOfPieces)  
              sx = height / Math.Ceiling(px * height / width);
          else
              sx = width / px;
          double py = Math.Ceiling(Math.Sqrt(numberOfPieces * height / width))+1;
          py = py == 0 ? 1 : py; 
          if (Math.Floor(py * width / height) * py &lt; numberOfPieces)  
              sy = width / Math.Ceiling(width * py / height);
          else
              sy = height / py;
          return Math.Max(sx, sy);
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/LZWXN.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LZWXN.gif" alt="Scale squares after resize screen"></a></p>