teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

更に追記

2017/12/10 15:59

投稿

IShix
IShix

スコア1729

answer CHANGED
@@ -11,4 +11,13 @@
11
11
 
12
12
  ###[追記]親のRectTransformのpivotが原因かもしれません
13
13
  親のpivotの位置が「x0.5 , y0.5」以外だとlocalPositionの値もそれをふまえた値が代入されます。一度親のpivotが「x0.5 , y0.5」以外でないか確認してみてください。検証したものをプラグインにしてみました。よければ見てみてください。
14
- [http://firestorage.jp/download/6880185a695f585900277b836b671cf8d26d006b](http://firestorage.jp/download/6880185a695f585900277b836b671cf8d26d006b)
14
+ [http://firestorage.jp/download/6880185a695f585900277b836b671cf8d26d006b](http://firestorage.jp/download/6880185a695f585900277b836b671cf8d26d006b)
15
+
16
+
17
+ ###[追記]pivotなどを考慮して場所を計算するのは大変なのでボタンが収まる親オブジェクトを用意する
18
+ ボタンが収まる親オブジェクトを用意すると比較的計算が楽になります。
19
+ ですが、何かのタイミングで親オブジェクトのpivotを触って計算しているのを忘れる可能性もあるのでスクリプトで制御するか、もしくは子を移動するのではなくオブジェクトの表示/非表示で対応するかした方がいいと思います。
20
+
21
+ 計算方法は説明すると長くなってしまうのでボタンが収まる親オブジェクトのサンプルを用意しました。そのコードを見ながらご自分の環境にマッチするように計算してみてください。
22
+
23
+ [http://firestorage.jp/download/9d74c0ae9529781ba2513cf62c6f44d952a5b086](http://firestorage.jp/download/9d74c0ae9529781ba2513cf62c6f44d952a5b086)

2

追記

2017/12/10 15:59

投稿

IShix
IShix

スコア1729

answer CHANGED
@@ -7,4 +7,8 @@
7
7
 
8
8
  ```C#
9
9
  gameObject.transform.SetParent(newParent, false);
10
- ```
10
+ ```
11
+
12
+ ###[追記]親のRectTransformのpivotが原因かもしれません
13
+ 親のpivotの位置が「x0.5 , y0.5」以外だとlocalPositionの値もそれをふまえた値が代入されます。一度親のpivotが「x0.5 , y0.5」以外でないか確認してみてください。検証したものをプラグインにしてみました。よければ見てみてください。
14
+ [http://firestorage.jp/download/6880185a695f585900277b836b671cf8d26d006b](http://firestorage.jp/download/6880185a695f585900277b836b671cf8d26d006b)

1

追記

2017/12/09 18:12

投稿

IShix
IShix

スコア1729

answer CHANGED
@@ -3,6 +3,8 @@
3
3
  worldPositionStays
4
4
  If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before.
5
5
 
6
+ [https://docs.unity3d.com/jp/540/ScriptReference/Transform.SetParent.html](https://docs.unity3d.com/jp/540/ScriptReference/Transform.SetParent.html)
7
+
6
8
  ```C#
7
9
  gameObject.transform.SetParent(newParent, false);
8
10
  ```