T
T
TheTalion2017-08-19 16:20:27
Mathematics
TheTalion, 2017-08-19 16:20:27

Correctly changing Rect Transform bindings relative to an object in the scene. What formula, or what tools to do this?

In general, there is a task: a field is created, it is necessary that each cell of the field stretches to all screen resolutions, but the problem is that when an object is created, it receives bindings relative to the parent (or receives bindings of the prefab), and not relative to itself. In total, it turns out that you need a formula for transferring bindings to an object, knowing its distance from the sides of the screen.
Here are the screenshots:
There is this:
014de37066bf32760e9e38eeab9a056e-full.jp
And you need something like this from the code:
436cd2b184d95abaa11d285fa84bf79c-full.jp
Obviously there is a dependency of the parties and obviously this problem arose for many people. Here are the statistics: if the object is in the left corner, then its initial values ​​are:
Left 0 (no distance from the left side)
Top 418 (removal of the top side of the object from the top) + 26 (if you bring it to the very edge)
Right 773 (remove the extreme right side of the object from the right side of the screen) + 50 (if you bring it to the very edge)
Bottom 0 (no distance from the bottom)
i.e. the size of the object is described by a figure at a distance in the left extreme corner at 0\26\50\0 Anchors
are in:
Anchors:
minX 0
minY 0
maxX 1
MaxY 1
(i.e. full screen)
And it should become:
Left 0
Top 0
Right 0
Bottom
0 no distance from object
Anchors:
minX 0
minY 0
maxX 0.06
MaxY 0.098
(i.e. only by object size)
How to correctly change bindings from code? Or what formula should be, then I am weak in mathematics. It is clear that there is something like x = 1 = 418 + 23, y = 1 = 773 + 50, so if, for example, x = 1 = 441, then iscX = X = 418, then how to find X?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2017-08-19
@TheTalion

I didn't quite understand the question.
If the parent interferes, then why not remove it?
If we know the distance from the object to the boundaries of the parent, then we just need to calculate the percentage relative to its size.
a.size=8*10
absize=2*3
abpos=2,2
Then
abanchor=2/8,2/10,(2+2)/8,(2+3)/10=1/4,1/ 5.1/2.1/2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question