- Home /
[2D]Size of an object + align on grid
Hello.
First, I want to know if it's possible to see the object's size directly in unity, and if yes, if we can change it directly. I know we can get it with renderer.bounds.size, but it would help if I could do that directly, like for the position/rotation/scale.
Secondly, I want to know if it's possible to align objects to the grid, in function of an object's size. It would REALLY help me for the scenes'creation.
Thanks. (and sorry for the bad english).
Answer by cryingwolf85 · May 24, 2014 at 11:31 PM
What do you mean by getting the size directly? I would say renderer.bounds.size
and transform.localScale
are about as direct as it gets. Ex:
Vector3 scale = transform.localScale;
As for the grid: Here are the steps I would take:
1: Get the selected objects x and y position.
2: Take off the remainder from the grid size on both the width and the height:
xPos = objPos.x - objPos.x % gridSpacing (Same with the yPos)
3: Move the object to it's new locked xPos and yPos
You are going to have to execute these things in edit mode.
By "getting the size directly", I mean, in the Unity interface, directly in the editor. I can easily change position, rotation, or scale in the "transform" component, can I change the size like that?
Thanks for the grid, it's approximatly what I wanted!
No problem. For getting the sizes through the editor, use a combination of transform.localScale and displaying and editing the results via Editor Windows
Good luck my friend
Answer by Stending · May 25, 2014 at 09:46 AM
I tried some stuff, and it doesn't work.
I can easily change position with this.transform.position = new Vector2 (x,y);
but it doesn't work with scale or size. When I try this : renderer.bounds.size = new Vector2 (x, y);
I have the error :
Assets/Scripts/ScaleChange.cs(20,26): error CS1612: Cannot modify a value type return value of `UnityEngine.Renderer.bounds'. Consider storing the value in a temporary variable
I can assign it, but just read it.
Someone know how I can change the size of an object ?
Okay, it works with transform.LocalScale, but it gives me the relative size of the object. I mean, 1 is his default size, 2 is the double, etc..
I want to get and change the "global size", with the default unit of unity. The size I get with renderer.bounds.size.