Skip to content

Instantly share code, notes, and snippets.

@nkjzm
Last active April 15, 2016 01:02
Show Gist options
  • Save nkjzm/bcb502e6b8138d9c5efd487449bfbf04 to your computer and use it in GitHub Desktop.
Save nkjzm/bcb502e6b8138d9c5efd487449bfbf04 to your computer and use it in GitHub Desktop.
Unityで子オブジェクトをソフマップ上に配置するスクリプト
// This Script refered to CircleDeployer.cs
// http://kan-kikuchi.hatenablog.com/entry/CircleDeployer
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SofmapDeployer : MonoBehaviour
{
// 子の大きさ
[SerializeField]
private Vector2 size = Vector2.one;
private void Awake()
{
Deploy();
}
//Inspectorの内容が変更された時に実行
private void OnValidate()
{
Deploy();
}
//子をソフマップ状に配置する(ContextMenuで鍵マークの所にメニュー追加)
[ContextMenu("Deploy")]
private void Deploy()
{
//子を取得
List<GameObject> childList = new List<GameObject>();
foreach (Transform child in transform)
{
childList.Add(child.gameObject);
}
// 適切な列の長さを計算
int colLen = (int)(Mathf.Sqrt(childList.Count * 2));
Vector3 offset = new Vector3(size.x * (colLen & -2), 0, size.y * colLen - 1f) / 2;
//各オブジェクトをソフマップ状に配置
for (int i = 0; i < childList.Count; i++)
{
Vector3 childPostion = Vector3.zero;
int colIndex = ((i * 2) / colLen);
childPostion.x += size.x * colIndex;
childPostion.z += size.y * ((((i * 2) % colLen) & -2) + (colIndex & 1));
childPostion -= offset;
childList[i].transform.localPosition = childPostion;
// 名前を付け替える
childList[i].name = (i + 1).ToString();
childList[i].transform.SetAsLastSibling();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment