Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 VergilUa · Nov 09, 2016 at 08:29 AM · scalerectscreentoworldpoint

Rescaling mesh or GameObject to fit in GUI Rect

Hi guys. I've got some rectangle drawed with mouse

 Vector3 topLeft = Vector3.Min(startPosition, endPosition);
 Vector3 bottomRight = Vector3.Max(startPosition, endPosition);

 //Screen space?
 Rect rect = Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);

and I'm trying to convert it into some gameobject by instantiating a prefab (basically a quad with attached mesh to it):

 Vector3 worldPoint = Camera.main.ScreenToWorldPoint(new Vector3(rect.center.x, rect.center.y, 5));
 GameObject go = (GameObject) Instantiate(prefab, worldPoint, Quaternion.identity);

It centers perfectly, but how do I resize it to fit exactly to the size of drawed rectangle?

I can't wrap my head around it. Any ideas?

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

3 Replies

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

Answer by VergilUa · Nov 09, 2016 at 07:55 PM

Ok, I've figured it out. Here's a solution if anybody else will stumble upon such problem.

 Mesh mesh = go.GetComponent<MeshFilter>().mesh;
 
 Bounds bounds = mesh.bounds;
 
 //Getting xMin and yMin
 Vector3 min = new Vector3(rect.xMin, rect.yMin, 0);
 Vector3 max = new Vector3(rect.xMax, rect.yMax, 0);
             
 //Converting them to worldspace coordinates
 min = Camera.main.ScreenToWorldPoint(min);            
 max = Camera.main.ScreenToWorldPoint(max);
 
 //Calculating width/height in world space coorinate-size
 float width = max.x - min.x;
 float height = max.y - min.y;

 //Calculating scale factors
 float widthScaleFactor =  (width / bounds.size.x);
 float heightScaleFactor = (height / bounds.size.y);
 
 Vector3 localScale = go.transform.localScale;

 //Scaling object
 localScale.x *= widthScaleFactor;
 localScale.y *= heightScaleFactor;

 go.transform.localScale = localScale;

Probably not the prettiest one, but 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 ElijahShadbolt · Nov 09, 2016 at 07:32 PM

Assuming the quad mesh's dimensions are 1x1 units, you could set the instantiated object's transform's scale to be the same as the Rect's width and height.

 go.transform.localScale = new Vector3(rect.width, rect.height, 1);
Comment
Add comment · Show 2 · 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 VergilUa · Nov 09, 2016 at 07:36 PM 1
Share

It doesn't work that way, since it's a gui rect. I've already submited a working answer, but it's under moderation. T_T

Thank you for your effort

avatar image ElijahShadbolt · Nov 10, 2016 at 08:42 PM 0
Share

Oh yeah. I completely missed that :/

avatar image
0

Answer by topitsky · Jan 03 at 04:21 PM

This works pretty well, but its off when the models origin is not a center.

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

6 People are following this question.

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

Related Questions

Is it possible to change Rect Transform Scale throw script 1 Answer

Problem dragging, scaling, moving objects in #scene view with mouse 0 Answers

Questions about UGUI Rect Transform Anchors. How to make UI objects scale, correctly, with screen? 1 Answer

Objects overlapping in perspective rather than real space? 1 Answer

Correct use to scale Text and Rect? 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