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 CodyCantEatThis · Feb 02, 2018 at 05:03 AM · resolutioncamera viewport

Trying to adjust camera for different resolutions cuts out part of the image

I'm trying to write a script to adjust the camera for any screen size. I intend for the game to be played on mobile. When I add the script to my camera and adjust the resolution in Unity, everything works fine, however when I try to play it on my phone it cuts off part of the picture. Is it something wrong with my code or the way I set up my scene?

 public class PortraitCam : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         float width = Screen.width;
         float height = Screen.height;
         float ar = width / height;
 
         gameObject.GetComponent<Camera> ().aspect = ar;
     
     }
 }


In Unity:

On my phone:

Comment
Add comment
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

2 Replies

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

Answer by UnitedCoders · Feb 02, 2018 at 05:28 AM

You can achieve your desired result using this way. If you want adjust your All GameObjects sprites. Just simply create an Empty GameObject( at Vector3.Zero, name it ParentOfAll) add sprite renderer on it, add the below script . And make child all your GameObjects of ParentOfAll and adjust position and anchor of all your child GO. According to current Screen Resolution.

  using UnityEngine;
   using System.Collections;
     
     [ExecuteInEditMode]
     public class BgScaleToScreenSize : MonoBehaviour {
         SpriteRenderer sr;
     
         // Use this for initialization
         void Start () {
             Resize ();
             
         
         }
         
         // Update is called once per frame
         void Update () {
             if (Input.GetKeyDown (KeyCode.R)) {
                 Resize();
             }
             
         
                 
             
         }
         void Resize()
         {
             sr=GetComponent<SpriteRenderer>();
             if(sr==null) return;
             
             transform.localScale=new Vector3(1,1,1);
             
             float width=sr.sprite.bounds.size.x;
             float height=sr.sprite.bounds.size.y;
             
             
             float worldScreenHeight=Camera.main.orthographicSize*2f;
             float worldScreenWidth=worldScreenHeight/Screen.height*Screen.width;
             
             Vector3 xWidth = transform.localScale;
             xWidth.x=worldScreenWidth / width;
             transform.localScale=xWidth;
             //transform.localScale.x = worldScreenWidth / width;
             Vector3 yHeight = transform.localScale;
             yHeight.y=worldScreenHeight / height;
             transform.localScale=yHeight;
             //transform.localScale.y = worldScreenHeight / height;
     
             
         }
     }
     

This script adjust your Parent GameObject Scale to your device resolution, and fit on the screen. But it will lose the aspect ratio of the sprite. if you want to maintain the aspect then add this script

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 public class ScaleWidthToScreenWidth : MonoBehaviour {
     SpriteRenderer sr;
 
     
 
 
     void OnEnable()
     {
         Resize ();
     }
     // Use this for initialization
     void Start () {
         Resize ();
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetKeyDown (KeyCode.R)) {
             Resize();
         }
 
     
     }
     void Resize()
     {
         sr=GetComponent<SpriteRenderer>();
         if(sr==null) return;
         
         transform.localScale=new Vector3(1,1,1);
         
         float width=sr.sprite.bounds.size.x;
     //    float height=sr.sprite.bounds.size.y;
         
         
         float worldScreenHeight=Camera.main.orthographicSize*2f;
         float worldScreenWidth=worldScreenHeight/Screen.height*Screen.width;
         
         Vector3 xWidth = transform.localScale;
         xWidth.x=worldScreenWidth / width;
 //        transform.localScale=xWidth;
 
         transform.localScale = new Vector3 (xWidth.x, xWidth.x, 1);
         //transform.localScale.x = worldScreenWidth / width;
 //        Vector3 yHeight = transform.localScale;
 //        yHeight.y=worldScreenHeight / height;
 //        transform.localScale=yHeight;
         //transform.localScale.y = worldScreenHeight / height;
         
     }
 }
 


I hope this will works for you :) @CodyCantEatThis

Comment
Add comment · Show 1 · 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 CodyCantEatThis · Feb 02, 2018 at 07:44 AM 0
Share

I got it to work, but I didn't use the BgScaleToScreenSize since it would mess up everything. Thanks!

avatar image
0

Answer by UnitedCoders · Feb 02, 2018 at 05:34 AM

If you want to maintain the anchor like bottomLeft,Right or whatever then add this script to your GameObject in which you want to maintain the anchor.

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 public class MySetAnchor : MonoBehaviour {
 
     public enum MyAnchorPoint {Center, LeftBottom,RightBottom,TopLeft,TopRight,CenterBottom,CenterTop,CenterLeft,CenterRight}
     public MyAnchorPoint _MyAnchorPoint;
     
 
     void OnEnable()
     {
         SetAnchorMethod ();
     }
     // Use this for initialization
     void Start () {
 
 
         SetAnchorMethod ();
     
 
 
     
 
     
     }
     
     // Update is called once per frame
     void Update () {
 
 
         if (Input.GetKeyDown (KeyCode.R)) {
             SetAnchorMethod();
         }
 
 
     
     }
 
     void SetAnchorMethod()
     {
         if ( _MyAnchorPoint==MyAnchorPoint.LeftBottom) {
             this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, 10));
         }
         else if (_MyAnchorPoint==MyAnchorPoint.RightBottom) {
             this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, 10));
         }
         else if (_MyAnchorPoint==MyAnchorPoint.TopLeft) {
             this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (0, 1, 10));
         }
         else if (_MyAnchorPoint==MyAnchorPoint.TopRight) {
             this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (1, 1, 10));
         }
         else if (_MyAnchorPoint==MyAnchorPoint.CenterBottom) {
             this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (.5f, 0, 10));
         }
         else if (_MyAnchorPoint==MyAnchorPoint.CenterTop) {
             this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (.5f, 1, 10));
         }
         else if (_MyAnchorPoint==MyAnchorPoint.CenterLeft) {
             this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (0f, .5f, 10));
         }
         else if (_MyAnchorPoint==MyAnchorPoint.CenterRight) {
             this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (1f, .5f, 10));
         }
         else if (_MyAnchorPoint==MyAnchorPoint.Center) {
             this.transform.position = Camera.main.ViewportToWorldPoint (new Vector3 (.5f, .5f, 10));
         }
     }
 }


I hope this will help you :)

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

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

75 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

Related Questions

Camera viewport should be square and pixel-perfect, regardless of player aspect ratio. 0 Answers

Cinemachine confiner on different aspect ratios 1 Answer

Outside game clear screen 0 Answers

Should texture assets be created for 300dpi? 0 Answers

Why is lightmap resolution different across certain objects? 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