Last active
October 26, 2015 07:09
-
-
Save JunShimura/8a21e4c1c3bdd6324b6b to your computer and use it in GitHub Desktop.
PrefabとInstantiateの基本(2)効率的にBlockを並べる:「はじめてのUnity」のブロック崩しを改造しながら学ぶ ref: http://qiita.com/JunShimura/items/268d5a6ccce303999da8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class BlockControl : MonoBehaviour { | |
public Transform blockPrefab; | |
// Use this for initialization | |
void Start () { | |
//配置する座標を設定 | |
Vector3 placePosition = new Vector3(-4,0,9); | |
//配置する回転角を設定 | |
Quaternion q = new Quaternion(); | |
q= Quaternion.identity; | |
//配置 | |
Instantiate(blockPrefab,placePosition,q); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlockControl : MonoBehaviour { | |
public Transform blockPrefab; | |
public Transform topWall; //上の壁の参照を追加 | |
public int placeX; ////横に並べる個数 | |
public int placeZ; ////奥に並べる個数 | |
public float totalDepth; ////奥に並べる座標 | |
// Use this for initialization | |
void Start () { | |
//配置する座標を設定 | |
Vector3 placePosition = new Vector3( | |
topWall.position.x-topWall.localScale.x/2+blockPrefab.localScale.x/2, | |
0, | |
topWall.position.z-topWall.localScale.z/2-blockPrefab.localScale.z/2); | |
//配置する回転角を設定 | |
Quaternion q = new Quaternion(); | |
q= Quaternion.identity; | |
//幅と奥行きを調整 | |
Vector3 localscale = blockPrefab.localScale; | |
localscale.x = topWall.localScale.x / placeX; | |
localscale.z = totalDepth / placeZ; | |
blockPrefab.localScale = localscale; | |
//配置 | |
for (int i = 0; i < placeZ; i++) | |
{ | |
Vector3 currentPlacePosition | |
= placePosition | |
- Vector3.forward * blockPrefab.localScale.z * i; | |
for(int j = 0; j < placeX; j++) | |
{ | |
Instantiate(blockPrefab, currentPlacePosition, q); | |
currentPlacePosition.x += blockPrefab.transform.localScale.x; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vector3 placePosition = new Vector3(-4,0,9); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
toWall.position.z-topWall.localscale.z/2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
toWall.position.x-topWall.localscale.x/2-blockPrefab.localscale.x/2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//幅を調整 | |
Vector3 localscale = blockPrefab.localScale; | |
localscale.x = topWall.localScale.x / placeX; | |
blockPrefab.localScale = localscale; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int placeZ; ////奥に並べる個数 | |
public float totalDepth; ////奥に並べる座標 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
localscale.z = totalDepth / placeZ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//配置 | |
for (int i = 0; i < placeZ; i++) | |
{ | |
Vector3 currentPlacePosition | |
= placePosition | |
- Vector3.forward * blockPrefab.localScale.z * i; | |
for(int j = 0; j < placeX; j++) | |
{ | |
Instantiate(blockPrefab, currentPlacePosition, q); | |
currentPlacePosition.x += blockPrefab.transform.localScale.x; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Quaternion q = new Quaternion(); | |
q= Quaternion.identity; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Instantiate(blockPrefab,placePosition,q); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
toWall.position.x-topWall.localscale.x/2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
toWall.position.x-topWall.localscale.x/2+blockPrefab.localscale.x/2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment