Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KazYamof · Apr 01, 2019 at 10:20 AM · 2dbuildaspect-ratioscreen size

Android resolution/aspect ration issue

I'm doing the classic Aranoid 2D. I have 2 walls (Sprite Renderer) set at -7x and 7x. I have a method that calculates the distance between them, scales the bricks to adjust exactly in this range, and them instantiate them side by side, beginning from left wall and ending at right wall, resulting in 10 brick columns. Works perfectly at Editor.

However, when I did my first build to android, the last column is not being generated, leaving me with only 9 brick columns. I created a script to camera, to force the aspect ratio 16/9, but didn't work. I have no clue what could be wrong.

Help!

alt text

Sample:

 public List GenerateBricks()
 {
      var bricks = new List<Brick>();
 
      var sideWallWidth = this.RightWall.GetComponent<BoxCollider2D>().size.x;
      var topWallHeight = this.TopWall.GetComponent<BoxCollider2D>().size.y;
     
       var stageWidth = (this.LeftWall.transform.position - this.RightWall.transform.position).magnitude - sideWallWidth;
     
        var brickWidth = this.Brick.GetComponent<BoxCollider2D>().size.x;
        var brickHeight = this.Brick.GetComponent<BoxCollider2D>().size.y;
     
         var columns = (int)(stageWidth / brickWidth);
         var rows = this.Rows; 
     
         var origin = new Vector3(this.LeftWall.transform.position.x + sideWallWidth / 2 + brickWidth / 2,
                                      this.TopWall.transform.position.y - topWallHeight / 2 - brickHeight / 2);
     
         for (var c = 0; c < columns; c++)
         {
              for (var r = 0; r < rows; r++)
              {
                  var b = Instantiate(this.Brick);
                  b.transform.position = new Vector3(origin.x + c * brickWidth, origin.y - r * brickHeight);
                   bricks.Add(b.GetComponent<Brick>());
               }
          }
     
         return bricks;
 }
 
 private void Awake()
 {
         this.camera = GetComponent<Camera>();
         this.camera.aspect = this.width / this.height;
 }

Edit 1 : the problem also occurs on Windows Build

Edit 2 : I did a build with "Development Build, Script Debug" enabled and it worked. Makes me more confuse than before!

sem-titulo.png (227.6 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Apr 01, 2019 at 12:13 PM 0
Share

force aspect ratio? for having black lines? i will share a script when i reach my house, thats really easy to use with some debug information forthis kind of tasks. and is better than scaling the objects in performance and will have same speed across resolutions.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KazYamof · Apr 01, 2019 at 09:17 PM

Solved!

I remove everything from

  private void Awake()
  {
          this.camera = GetComponent<Camera>();
          this.camera.aspect = this.width / this.height;
  }

Reset all my values at Player tab in Project Settings. In the same tab: Set Aspect Ration Mode to> Native Aspect Ration Default Orientation: Portrait

Now it works!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by xxmariofer · Apr 01, 2019 at 05:29 PM

test this class

 using System.Collections;
 using UnityEngine;
 
 namespace CustomCamera
 {
     [ExecuteInEditMode]
     public class ResizeCamera : MonoBehaviour
     {
         [Tooltip("Camera Limits Size")]
         [SerializeField] private float size;
         [SerializeField] private bool enableDebugging;
 
         private Camera cameraReference;
 
         private enum ResizeMode
         {
             AdjustWidth,
             AdjustHeight
         }
         [SerializeField]private ResizeMode resizeMode = ResizeMode.AdjustWidth;
 
         public float deltaTime {
             get
             {
                 return Time.deltaTime * 2;
             }
         }
 
         #region events
         private void Awake()
         {
             cameraReference = Camera.main;
 
             if(!cameraReference.orthographic)
             {
                 Debug.LogWarning("Camera must be in Ortographic Mode");
                 cameraReference.orthographic = true;
             }
         }
 
         private void Update()
         {
             if(!enableDebugging)
             {
                 return;
             }
 
             Awake();
 
             float width, height;
             GetCameraSize(out width, out height);
 
             Debug.DrawRay(new Vector2(-width,  height), Vector2.down  * height * 2, Color.red);
             Debug.DrawRay(new Vector2(-width, -height), Vector2.right * width  * 2, Color.red);
             Debug.DrawRay(new Vector2( width, -height), Vector2.up    * height * 2, Color.red);
             Debug.DrawRay(new Vector2( width,  height), Vector2.left  * width  * 2, Color.red);
         }
         #endregion
 
         public void GetCameraSize(out float width, out float height)
         {
             if(resizeMode == ResizeMode.AdjustHeight)
             {
                 height = size;
                 width = height * cameraReference.aspect;            
             }
             else
             {
                 width = size;
                 height = width / cameraReference.aspect;
             }
 
             cameraReference.orthographicSize = height;
         }
     }
 }
 

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KazYamof · Apr 01, 2019 at 05:40 PM 0
Share

It didn't worked for my case. Last column still not being generated in builds, only in editor.

avatar image xxmariofer KazYamof · Apr 01, 2019 at 05:49 PM 0
Share

with that script you can force the Build to be the exact size you need, may i ask whats happening? the last column is not being instantiate and is leaving the empty space? is it posible you share the project? if not, whats the columns value being logged in build and in editor?

avatar image KazYamof · Apr 01, 2019 at 07:02 PM 0
Share

Exactly, i got an empty space there

avatar image xxmariofer KazYamof · Apr 01, 2019 at 07:13 PM 0
Share

can you debug.log all these values please

stageWidth , brickWidth and columns

avatar image KazYamof xxmariofer · Apr 01, 2019 at 08:38 PM 0
Share

Stage width: 13,6; BrickWidth: 1,36; Columns: 10 The math is right...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

301 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom aspect ratio in build 0 Answers

Stop screen from stretching in build? 0 Answers

Unity 2D targeting multiple screen sizes 1 Answer

Distribute terrain in zones 3 Answers

How to set a second camera's bounding box? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges