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 greenshadow · Jul 19, 2013 at 01:22 AM · instantiateprefabobjectscale

How do I scale an object or prefab to a size in Unity units?

I'm working on a game in which most of the objects will all fit into 3d grid spaces in the scene, which are 1x1x1 Unity units in size. I am currently in the process of programming a level editor for the game, where the level designers would be able to simply choose a prefab in the inspector, and then click on a spot in the scene grid to instantiate the object in the selected grid space.

However, we have a few different artists making assets for the game, and the prefabs come in different sizes. How would I programmatically instantiate a prefab with a scale that makes the object exactly 1.0 Unity units high? There must be a way to standardize this process, but I can't seem to find it.

By the way, I am using C#, though I don't think that would change the approach (just the syntax).

Comment
Add comment · Show 3
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 Eric5h5 · Jul 19, 2013 at 01:53 AM 1
Share

It would be better if you got the artists to standardize. You don't want to touch scale if it's not strictly necessary, since it's best for performance if objects remain at a scale of 1.0.

avatar image greenshadow · Jul 19, 2013 at 01:58 AM 0
Share

@Eric5h5 Thanks for the feedback. I am inclined to agree with you; just for the sake of it, is there an approach to achieve this in code?

avatar image Eric5h5 · Jul 19, 2013 at 02:14 AM 0
Share

Possibly; maybe somebody will answer the question. ;) That's why I posted a comment, since it's not a direct answer.

1 Reply

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

Answer by robertbu · Jul 19, 2013 at 05:52 AM

I'm going to go out on a bit of a limb here since I don't have a lot of experience with meshes. First, you can use the mesh.bounds.size and set the Transform.localScale at runtime to get a one-unit sized object. As @Eric5h5 mentions, it is better if you don't do it this way (i.e. resize using the Transform), but it may be workable in your situation.

Second, you could also resize the mesh in the editor. Here is a bit of code to show you the concept:

 void ResizeMeshToUnit() {
     MeshFilter mf = GetComponent<MeshFilter>();
     if (mf == null) 
         return;
     Mesh mesh = mf.sharedMesh;
     Bounds bounds = mesh.bounds;
     
     float size = bounds.size.x;
     if (size < bounds.size.y)
         size = bounds.size.y;
     if (size < bounds.size.z)
         size = bounds.size.z;
     
     if (Mathf.Abs(1.0f - size) < 0.01f) {
         Debug.Log ("Already unit size");
         return;
     }
     
     float scale = 1.0f / size;
             
     Vector3[] verts = mesh.vertices;
     
     for (int i = 0; i < verts.Length; i++) {
         verts[i] = verts[i] * scale;    
     }
     
     mesh.vertices = verts;
     mesh.RecalculateBounds();
     mesh.RecalculateNormals();
 }

You could put something like this in an editor script and resize the meshes. Note if you put this function in a script and execute it at runtime in the editor, it will permanently change the mesh.

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 greenshadow · Aug 07, 2013 at 11:31 PM 0
Share

Thank you sir. This is valiant effort, and does work as described, with a $$anonymous$$or tweak:

         void Resize$$anonymous$$eshToUnit(GameObject t) {
             $$anonymous$$eshFilter mf = t.GetComponent<$$anonymous$$eshFilter>();
             if (mf == null)
                 return;
             $$anonymous$$esh mesh = mf.shared$$anonymous$$esh;
 
             //***Set this to renderer bounds ins$$anonymous$$d of mesh bounds***
             Bounds bounds = t.renderer.bounds;
              
             float size = bounds.size.x;
             if (size < bounds.size.y)
                 size = bounds.size.y;
             if (size < bounds.size.z)
                 size = bounds.size.z;
              
             if ($$anonymous$$athf.Abs(1.0f - size) < 0.01f) {
                 Debug.Log ("Already unit size");
                 return;
             }
              
             float scale = 1.0f / size;
              
             Vector3[] verts = mesh.vertices;
              
             for (int i = 0; i < verts.Length; i++) {
                 verts[i] = verts[i] * scale;
             }
              
             mesh.vertices = verts;
             mesh.RecalculateBounds();
             mesh.RecalculateNormals();
         }

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

16 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

Related Questions

Problem with instantiate rotation 0 Answers

Instantiating Objects on the Sides of Other Objects 1 Answer

How to Reference Player when Instantiating Prefab 1 Answer

How to destroy instantiated objects. 1 Answer

How to delete an instantiated object 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