LoginSignup
3
0

More than 3 years have passed since last update.

【Unity】Addressables1.8.3で読み込んだオブジェクトをSetParentすると初回だけlocalPosition, localRotationの値が意図した物にならない

Last updated at Posted at 2020-05-05

概要

Addressablesで読み込んだオブジェクトを SetParent すると初回だけ localPosition, localRotation の値が意図した物にならない。(二回目以降の同じ処理では意図したとおりの値になる)

元々 Resources.Load で書いていた箇所を Addressables.LoadAssetAsync に変更して発生、実装は下記の通り。

// var prefab = Resources.Load<GameObject>(path);
var prefab = await Addressables.LoadAssetAsync<GameObject>(path).Task;   
var childGameObject = Instantiate(prefab, position, rotation);
childGameObject.transform.SetParent(parentGameObject.transform, true);

環境

  • Unity 2018.4.22f1
  • Addressables 1.8.3

暫定対策

SetParent 後に localPositionlocalRotation を直接変更する。

var prefab = await Addressables.LoadAssetAsync<GameObject>(path).Task;   
var childGameObject = Instantiate(prefab, position, rotation);
childGameObject.transform.SetParent(parentGameObject, true);
// これを追加した
childGameObject.transform.localPosition = new Vector3(0f, 0f, 0f);
childGameObject.transform.localRotation = Quaternion.Euler(0f,180f, 270f);

やったけど駄目だったこと

  • async/await を使わずに Completed でコールバックを設定
  • SetParent ではなく childGameObject.transform.parent = parentGameObject を使用
  • SetParent の第二引数を false に変更
  • Addressables.LoadAssetAsync を2回実行して、2回目の結果を使用
  • await 以降の処理を別メソッドに切り出し
  • SetParent(parent) → SetParent(null) → SetParent(parent)

最後に

Resources.Load の時はうまく出来ていたので、Addressables のバグに思えるが、もしかしたら物凄く初歩的なミスをしているのかもしれない気もする。

何の処理が影響してどういう理屈でうまく動作しないのか全く検討がつかなかったので、何か分かる方がいらっしゃいましたらご教示頂けますと幸いです。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0