LoginSignup
0
0

More than 5 years have passed since last update.

Unity Resourceファイルから読み込む際に親子関係を意識した読み込み備忘録

Posted at

ただmainStageObjの子どもとして、prefab前のtransform情報をもったまま読み込みメモ。

パッとでで来ないのでメモ。

mainStageObj
∟Fence(リソース名)

GameManager.cs

GameObject collectionObj = (GameObject)Resources.Load("Prefabs/Hoge/GenerateDecoObj/" + Constant.GENERATE_DECO_OBJS[growthLvInt]); //as GameObject;

GameObject collectionObjInstance;
if (growthLvInt < 3)
{
    collectionObjInstance = (GameObject)GameObject.Instantiate(collectionObj, collectionObj.transform.position, collectionObj.transform.rotation);
}
else
{
    // 2以降はあて TODO
    // // -14f 〜 14f
    float randomX = (UnityEngine.Random.value * 1000f) - 500f;
    float randomY = 500f + (UnityEngine.Random.value * 300f);

    collectionObjInstance = (GameObject)GameObject.Instantiate(collectionObj, new Vector3(randomX, randomY, -6.65f), Quaternion.Euler(0.0f, 0.0f, 0.0f));

}


if(mainStageObj){

    collectionObjInstance.transform.parent = mainStageObj.gameObject.transform;

    // prefab化前の親子関係と同じ状態で、prefabの座標をそのまま使う。
    collectionObjInstance.transform.localPosition = collectionObj.transform.localPosition;
    collectionObjInstance.transform.localScale = collectionObj.transform.localScale;
}
0
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
0
0