Image(Canvas)の座標変更でハマった話【Unity】
小一時間悩んだのでメモとして残します。
失敗例
以下のパターンだとずれました。
transform.position= new Vector2((100,100)); transform.localPosition= new Vector2((100,100)); GetComponent<RectTransform>().position= new Vector2((100,100)); GetComponent<RectTransform>().localPosition= new Vector2((100,100));
成功例
GetComponent<RectTransform>().anchoredPosition= new Vector2((100,100));
なぜうまくいったのか
Imageのアンカーを変更していたので座標の基準点が変わっていました。
つまり、アンカーのことも考えないと座標が合わないということです。
なお、特にアンカーを変更せずにそのままの座標で合わせると、localPositionでもうまくいく時があります。
ですが、この場合でもlocalPositionとanchoredPositionの位置が同じなので、anchoredPositionでやると無難だと思います。