Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

how to make sure objects have the same size with a script

Discussion in 'Scripting' started by rainbow_design, Nov 27, 2016.

  1. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    I want to arrange 3D objects in a row but i need to make sure they have the same xsize how can i do this with a script
     
    wael-shammaa likes this.
  2. logang

    logang

    Joined:
    Feb 1, 2013
    Posts:
    63
    Code (CSharp):
    1.     //Array of objects
    2.     public GameObject[] objs;
    3.  
    4.     // Use this for initialization
    5.     void Start()
    6.     {
    7.         //Loop through the array
    8.         for(int x = 0; x < objs.Length -1; x ++)
    9.         {
    10.             //check if the object is the right size
    11.             if (objs[x].transform.localScale.x == 1/*Change this number to whatever you need*/)
    12.             {
    13.                 //Object is that size
    14.                 print("Object \"" + objs[x].name + "\" is the currect size!");
    15.             }
    16.             else
    17.             {
    18.                 //Object is not that size
    19.                 print("Object \"" + objs[x].name + "\" is NOT the currect size! the object size is : " + objs[x].transform.localScale.x);
    20.             }
    21.         }
    22.     }
     
  3. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    i mean objects that have different sizes when the scale is the same, i know how to change the scale but not how to get the real size of an 3D object.
     
  4. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    The size of an object is somewhat arbitrary. A GameObject doesn't have a size, just a position, rotation and scale. If you're talking about the size of the mesh renderer, then you can get the bounds of the renderer which should approximate the size of the mesh closely enough: https://docs.unity3d.com/ScriptReference/Renderer-bounds.html
     
  5. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    I am talking about the mesh. I have downloaded the Rocks pack. The .obj files all have different size now i wonder how "scale" them so they have all one size at x without editing all of them.
     
    Last edited: Aug 26, 2017
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    That's a late answer to the other posts.

    Like @kru suggested, you should use the object's bounds.
    It's just some kind of normalization process, which takes place on the largest axis (only one axis to keep proportions).
    The bounds are represented by an AABB, an axis aligned bounding box and are determined by the actual mesh.

    So, the steps are:

    - get the total bounds, e.g. B(10f, 25f, 16f) whereas scale is S(1f, 1f, 1f)
    - find the largest axis value, which is 25 in this example
    - "normalize" by dividing the scale by that value: S'(0.04f, 0.04f, 0.04f) so that the bounds become approx. B'(0.4f, 1f, 0.64f)

    The mesh is now scaled to fit into an unit cube.
    Add another factor to the result (or during calculation) to scale it for your needs.

    Will be immediately added to my utitlies btw, never had the requirement to do such thing but always a nice to have. :)
     
    Last edited: Aug 26, 2017
    zombiegorilla likes this.
  7. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    I have a vague idea how that script would look like.
    Maybe a function that takes the Gameobject and scales it . However i would not know how to deal with the bounds like finding the biggest or multiplicate that with a Vector3.
    If you make a script to add it to your utitlies please share it then. This is a reoccuring problem for me last time i solved it by fixing all my own blends to one size after futily trying to make the script
     
    Last edited: Aug 26, 2017
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Actually, when I said immediately, I meant I'd take note of it to implement it when I'm done with other stuff that I'm working on, as I usually take a lot of time to think about my personal preference to implement such things.

    In its simplest form:
    - you pass in a Renderer (or several renderers)
    - get the bounds (if you passed multiple bounds, you want to combine them to get total bounds - there's a utitlity method for that, I just cannot remember the name atm - I've written something similar at work though, so I know it does exist)
    - do the math mentioned in the previous post

    A more complex one would either accept a renderer, a component in general (or specificly a transform) or a GameObject and might offer more functionality, some ideas are:

    - pass the instance of the type you chose for the overload
    - have additional parameters that allow you to set a rule/policy to determine how to treat the object, e.g.:
    => get the root object first in order to get all renderers in the object's hierarchy,
    => or simply start to get all renderes starting with the received object (or the component's object respectively)
    => allow to specify the desired size of bounds / with or without keeping proportions
     
    Last edited: Aug 26, 2017
  9. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    I made this now and did try it but it does not work. one i got negative values on ma and 2 the sizes are not even.
    Code (CSharp):
    1.  
    2.  
    3.      
    4.        public static void normalizeassetx(GameObject asset) {
    5.            var rend = asset.GetComponent<Renderer> ();
    6.            Bounds bound= rend.bounds;
    7.            float ma;
    8.            float sizer=10;
    9.            if (bound.max.x >= bound.max.y) ma = bound.max.x;
    10.            else  ma = bound.max.y;
    11.            ma = ma / sizer;
    12.            if (ma < 0) ma *= -1;
    13.            pre ("max" + ma, "scale " + 1 / ma);
    14.            asset.transform.localScale = new Vector3 (1 / ma, 1 / ma, 1 / ma);
    15.        }
    16.  
     
    Last edited: Aug 26, 2017
    sirshelley likes this.
  10. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    Okay i managed it bound.size.x was what i needet:

    Code (CSharp):
    1.         public static void normalizeasset( GameObject asset) {
    2.             var rend = asset.GetComponent<Renderer> ();
    3.             Bounds bound= rend.bounds;
    4.             float ma;
    5.             float sizer=10;
    6.             if (bound.size.x>= bound.size.y&& bound.size.y >= bound.size.z) ma= bound.size.x;
    7.             else if (bound.size.y >= bound.size.z && bound.size.z >= bound.size.x) ma = bound.size.y;
    8.             else  ma = bound.size.z;
    9.             ma = ma / sizer;
    10.             pre ("max"+ma, "scale "+1/ma);
    11.             asset.transform.localScale= new Vector3(1 / ma, 1 / ma, 1 / ma);
    12.         }
    13.         public static void normalizeassetx(GameObject asset) {
    14.             var rend = asset.GetComponent<Renderer> ();
    15.             Bounds bound= rend.bounds;
    16.             float ma;
    17.             float sizer=10;
    18.             if (bound.size.x > bound.size.y) ma = bound.size.x;
    19.             else  ma = bound.size.y;
    20.             ma = ma / sizer;
    21.             if (ma < 0) ma *= -1;
    22.             pre ("max" + ma, "scale " + 1 / ma);
    23.             asset.transform.localScale = new Vector3 (1 / ma, 1 / ma, 1 / ma);
    24.         }
     
    sirshelley likes this.
  11. jpcyrillo

    jpcyrillo

    Joined:
    Sep 2, 2021
    Posts:
    1
    object1.transform.localScale = new Vector3(object2.transform.localScale.x, object2.transform.localScale.y, object2.transform.localScale.z);