- Home /
Setting values of a Transform childed to a RectTransform giving unexpected results.
Straight forward code, straight forward problem:
itemGraphics[itemSlot.IndexInList] = (GameObject)Instantiate(itemSlot.Item.ModelAsset);
itemGraphics[itemSlot.IndexInList].transform.parent = itemSlot.transform;
itemGraphics[itemSlot.IndexInList].transform.lossyScale.Set(1, 1, 1);
itemGraphics[itemSlot.IndexInList].transform.rotation = Quaternion.identity;
It's giving unexpected values in the inspector for scale and rotation. The parent (itemSlot) of the child (itemGraphic) is a worldspace ui element, so it has a RectTransform, where as the child itemGraphic is a regular gameobject with a regular Transform.
I tried editing local scale and local rotation instead; same issue. I tried parenting after setting scale and rotation (tried both local and world) and still same issue.
All I want to do is child a 3D graphic to a 2D UI element.
EDIT: Sceenshots of the output:
After changing scale to 1 and rotation to 0 through inspector at runtime:
EDIT: I just figured something out. It seems to be scaling down the child's x and y by the parent rectTransform's width and height. The rectTransforms width and height is 50, so if I scale the child's x and y to 50, it returns to normal.
I guess if there's no better way, I can just code it to scale up the child by the parents width and height...
EDIT: ^Actually that still doesn't work. For some reason the final scale is some crazy high numbers in the thousands. Something's retransforming it after I do.
Also if I change the values at runtime in the inspector, if the rotation isn't 0,0,0, the model skews even though the scale doesn't change.
This is all very strange behaviour, I wish I could fine documentation on how rectTransforms and transforms are supposed to work together.
I haven't tried this, but can you try to set the position of child object with worldtoviewportpoint?
The position actually seems fine. Just the rotation and scale get all messed up. It's all stretched and skewed.
Answer by Bunny83 · Nov 21, 2014 at 11:09 PM
You can't set the lossy scale. It's readonly and all you do is reading the lossyscale and then you change the returned value. Vector3 is a value type. lossyScale is a property which doesn't have a setter, only a getter. The lossy scale is approximated from the transform hierarchy but depending on the rotation / translation / scale of the parents.
Setting the localScale before parenting should work as Unity adapts the scale to try to maintain the current scale. Once parented the scale might be changed depending on the scale / rotation / translation of the parents.
I tried that:
itemGraphics[itemSlot.IndexInList] = (GameObject)Instantiate(itemSlot.Item.$$anonymous$$odelAsset);
itemGraphics[itemSlot.IndexInList].transform.localScale = Vector3.one;
itemGraphics[itemSlot.IndexInList].transform.rotation = Quaternion.identity;
itemGraphics[itemSlot.IndexInList].transform.parent = itemSlot.transform;
Same issue still.
Well, that probably means that your hierarchy has some strange scaling on the z axis. I don't really use 4.6 so i hadn't much time to look through the new features. It might be possible that a rect transform (which is ment for 2d gui) might set some strange z scale (0 for example). Is it that important to have the 3D object as child of the 2D button?
In the inspector it shows the z scale as 1 for everything. And if I`m not supposed to child 3D objects to 2D UI elements, how else am I supposed to attach 3D meshes to my GUI? Only way I can think of would be to not child them to anything and hardcode transform values in Update();
I thought one of the features of the 4.6 UI was that rectTransforms were compatible with regular Transforms, but it doesn't seem to be as simple as that and I can't find any documentation, examples, or tutorials on how it's supposed to be done.