Skip to content

Instantly share code, notes, and snippets.

@JunShimura
Last active October 26, 2015 07:09
Show Gist options
  • Save JunShimura/8a21e4c1c3bdd6324b6b to your computer and use it in GitHub Desktop.
Save JunShimura/8a21e4c1c3bdd6324b6b to your computer and use it in GitHub Desktop.
PrefabとInstantiateの基本(2)効率的にBlockを並べる:「はじめてのUnity」のブロック崩しを改造しながら学ぶ ref: http://qiita.com/JunShimura/items/268d5a6ccce303999da8
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 () {
}
}
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;
}
}
}
Vector3 placePosition = new Vector3(-4,0,9);
toWall.position.z-topWall.localscale.z/2
toWall.position.x-topWall.localscale.x/2-blockPrefab.localscale.x/2
//幅を調整
Vector3 localscale = blockPrefab.localScale;
localscale.x = topWall.localScale.x / placeX;
blockPrefab.localScale = localscale;
public int placeZ; ////奥に並べる個数
public float totalDepth; ////奥に並べる座標
localscale.z = totalDepth / placeZ;
//配置
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;
}
}
}
Quaternion q = new Quaternion();
q= Quaternion.identity;
Instantiate(blockPrefab,placePosition,q);
toWall.position.x-topWall.localscale.x/2
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