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 ahstein · Sep 19, 2018 at 11:46 PM · camerasizerectminimaporder-of-execution

Resizing minimap camera fails on first function call, but not following calls

I am attempting to create a map menu (essentially a minimap that you can open/close) that fits inside of a scroll image frame. In order to work on multiple resolution screens, I'm changing the minimap camera's rect when it is enabled based on the size of the image it should fit inside. Here's the script to do it:

 public class MinimapCamera : MonoBehaviour {
 
     [SerializeField]
     RectTransform mapImageRect;
     Camera cam;
     MapGenerator mapGen;
 
     [SerializeField]
     Vector3 lowerLeftOffset;
     [SerializeField]
     Vector3 upperRightOffset;
 
     private void OnEnable()
     {
         if(!cam)
             cam = GetComponent<Camera>();
 
         if(!mapGen)
             mapGen = GameObject.FindGameObjectWithTag("Map Generator").GetComponent<MapGenerator>();
 
         SetCameraRect();
     }
 
     void SetCameraRect()
     {
         cam.aspect = mapGen.width / mapGen.height;
         cam.orthographicSize = (mapGen.width * mapGen.areaSize + 5) / 2;
 
         Vector3[] mapCorners = new Vector3[4];
         mapImageRect.GetWorldCorners(mapCorners);
         Vector3[] mapCornersNormalized = new Vector3[4];
 
         for (int i = 0; i < 4; i++)
         {
             mapCornersNormalized[i] = Camera.main.WorldToViewportPoint(mapCorners[i]);
         }
 
         Rect newCamRect = new Rect(mapCornersNormalized[0] + lowerLeftOffset, mapCornersNormalized[2] - upperRightOffset - mapCornersNormalized[0]);
         cam.rect = newCamRect;
     }
 }
 

The gameobject this script is attached to is enabled at the same time that the object with the RectTransform it's using for sizing is enabled, when you click on a button. The other object has an AspectRatioFitter on it that resizes it.

The script works properly the second time you click the button to open the map, but it seems to inaccurately resize the camera the first time the OnEnable() function runs.

I tried calling SetCameraRect() twice in a row within OnEnable(), but it did not fix the issue. I suspect the issue is related to the script order of execution (for example, the mapImageRect is not enabled before this script runs), but the order of execution documentation has been unhelpful in solving the problem. Setting the MinimapCamera to run either before or after the Default Time has no effect.

Thanks for your help!

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 ahstein · Sep 20, 2018 at 05:21 PM

Solved, but in a pretty ugly way. If someone has a better solution I'd still love to hear it.

I added a new script to the mapImageRect object that uses UIBehavior.OnRectTransformDimensionsChange() to call SetCameraRect() on my MinimapCamera. It works, but I'm not a fan of needing to pass references between the two objects to both objects.

Edit: And really a big thank you to my wife for suggesting that some kind of callback should exist for when the dimensions of the RectTransform change. She deserves the credit for solving it.

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 EliasMc · Sep 20, 2018 at 11:05 PM 0
Share

Glad to hear you found a solution. Sometimes it isn't always pretty.

avatar image
0

Answer by EliasMc · Sep 20, 2018 at 01:17 AM

Hope this helps. So if you are calling this code from a button call. Could you instead of using OnEnable. Define your own function to do this and call it from the call. such as from another script so when you hit a key like if


psudeocode


if (input.getkey('m')){ MinimapCamera.turncameraon();}


Or if your calling this from the editor or a gui button, then make sure the function is public and drag and drop it into the onclick int the editor and save as prefab. Or assign it to OnClick via script.

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 ahstein · Sep 20, 2018 at 01:35 AM 0
Share

Good idea, but it didn't work.

I set the button to call this function via the editor and it had the same problem. The button is doing 4 things (in this order): (1) pausing the game via a Game$$anonymous$$anager script, (2) set the game object with the map image active, (3) set the $$anonymous$$imap camera active, and (4) calling this function.

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

149 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

Related Questions

Fixing a Camera viewport rect to a certain size? (So it doesn't change with the size of the gamewindow) 1 Answer

Camera placement / size behavior 3 Answers

How I get sizeof pixels of object 1 Answer

Getting Camera View (Mini-map) 0 Answers

Two Levels of Directional Lighting For Different Cameras 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