Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mirkojpn · Oct 13, 2020 at 01:37 PM · rendertexturespriterendereraspect-ratio

fit spriterenderer image to screen after change aspect ratio

hello! i'll try to explain what i have... my "field" camera with this script (used to keep the aspetct)

 using UnityEngine;
  
 [RequireComponent(typeof(Camera))]
 public class CameraCrop : MonoBehaviour {
  
     // Set this to your target aspect ratio, eg. (16, 9) or (4, 3).
     public Vector2 targetAspect = new Vector2(16, 9);
     Camera _camera;
  
     void Start () {
         _camera = GetComponent<Camera>();
         UpdateCrop();
     }
  
     // Call this method if your window size or target aspect change.
     public void UpdateCrop() {
         // Determine ratios of screen/window & target, respectively.
         float screenRatio = Screen.width / (float)Screen.height;
         float targetRatio = targetAspect.x / targetAspect.y;
  
         if(Mathf.Approximately(screenRatio, targetRatio)) {
             // Screen or window is the target aspect ratio: use the whole area.
             _camera.rect = new Rect(0, 0, 1, 1);
         }
         else if(screenRatio > targetRatio) {
             // Screen or window is wider than the target: pillarbox.
             float normalizedWidth = targetRatio / screenRatio;
             float barThickness = (1f - normalizedWidth)/2f;
             _camera.rect = new Rect(barThickness, 0, normalizedWidth, 1);
         }
         else {
             // Screen or window is narrower than the target: letterbox.
             float normalizedHeight = screenRatio / targetRatio;
             float barThickness = (1f - normalizedHeight) / 2f;
             _camera.rect = new Rect(0, barThickness, 1, normalizedHeight);
         }
     }
 }

and one more camera inside my canvas used to who 3d character on UI and a background image ( used to show a screenshot got from the field camera) so what i have on my ui is 3d model with a background spriterenderer with a screenshot texture got from "field" camera ( using rendertexture )

on cap1 is what i have on my "field" camera then i take a screenshot and on cap2 is what i have after i get a screenshot with the rendertexture then convert it to a texture2d and create a new sprite from that texture ( to a sprite render inside the canvas camera)

so as you can see the screenshot doesn't fit the entire screen, without the above script it does fit perfectly.

this is what i do to resize it

     void ResizeSpriteToScreen()
     {
         SpriteRenderer sr = sprite.GetComponent<SpriteRenderer>();
  
         sprite.transform.position = _myCamera.transform.position + Vector3.forward * sr.transform.position.z;
  
         sprite.transform.localScale = new Vector3(1, 1, 1);
         Vector3 lossyScale = sprite.transform.lossyScale;
  
         float width = sr.sprite.bounds.size.x;
         float height = sr.sprite.bounds.size.y;
  
         float worldScreenHeight = _myCamera.orthographicSize * 2f;
         float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
  
         Vector3 xWidth = sprite.transform.localScale;
         xWidth.x = worldScreenWidth / width;
         sprite.transform.localScale = xWidth;
         //transform.localScale.x = worldScreenWidth / width;
         Vector3 yHeight = sprite.transform.localScale;
         yHeight.y = worldScreenHeight / height;
         sprite.transform.localScale = yHeight;
  
         Vector3 newLocalScale = new Vector3(transform.localScale.x / lossyScale.x,
             sprite.transform.localScale.y / lossyScale.y,
             sprite.transform.localScale.z / lossyScale.z
         );
  
         sprite.transform.localScale = newLocalScale;
     }

alt text

cap1.png (46.5 kB)
cap2.png (46.7 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

140 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

Related Questions

How to get a camera to render to render texture at a custom aspect ratio. 1 Answer

How to keep Render Texture aspect ratio from perspective camera when window is resized? 1 Answer

Graphics.Blit scale 1 Answer

Canvas aspect ratio 1 Answer

Screenshot from a rendertexture (non-main camera) to fit sprite (in 3D space) exactly 1 Answer


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