Translate

Editor Script to create Pyramid from basic shapes in unity3d

Hello,
You might have seen my previous post "How to create a staircase using editor scripts in Unity".
Here you find a script to create a pyramid like structure.

All you have to follow the procedure to create the basic building block of pyramid. In staircase I used a cube like structure as basic building block. This time I am using a capsule with some texture applied over it.
My capsule looks like:



Now the same procedure as Stairs, Just replace the script with the following one.
Script name: PyramidGenerator.cs
The script I have written is in C#. the class extends Editor Window class of Unity. We do not need any extra plugin to run this script but while placing it in the project , you need to place it in "Editor" folder.
The script is as follows.
Script:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class PyramidGenerator : EditorWindow {
public GameObject cratePrefab;//prefab of building block
GameObject crate,Pyramid;//building blocks and final pyramid
int pyrHeight;//height or number of layers
float offset=0.25f;
[MenuItem("Pyramid/Create Pyramid")]//menu to show the window
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(PyramidGenerator));
}
void OnGUI()
{
//Enter the height and basic block's reference
pyrHeight=EditorGUILayout.IntField("Height of Pyramid :",pyrHeight);
cratePrefab =(GameObject)EditorGUILayout.ObjectField("Base Game Object :",cratePrefab, typeof(GameObject), true);

//Create the pyramid
if(GUILayout.Button("Create"))
{
Pyramid=new GameObject("Pyramid");
pyrHeight--;
for(;pyrHeight>0;pyrHeight--)
{
for(int pyrLength=pyrHeight*2;pyrLength>0;pyrLength--)
{
for(int pyrWidth=pyrHeight*2;pyrWidth>0;pyrWidth--)
{
crate=GameObject.Instantiate(cratePrefab,new Vector3(0,0,0),Quaternion.identity)as GameObject;
crate.transform.position=new Vector3(pyrLength+offset,crate.transform.position.y+offset,pyrWidth+offset);
crate.gameObject.name="Crate";
crate.transform.parent=Pyramid.transform;
}

}
offset=offset+crate.transform.localScale.x;
}
//Create the top most block
crate=GameObject.Instantiate(cratePrefab,new Vector3(0,0,0),Quaternion.identity)as GameObject;
crate.gameObject.name="Crate";
crate.transform.position=new Vector3(crate.transform.position.x,crate.transform.position.y+offset,crate.transform.position.z);
offset=offset+crate.transform.localScale.x*0.5f;
crate.transform.position=new Vector3(crate.transform.position.x+offset,crate.transform.position.y,crate.transform.position.z+offset);
crate.transform.parent=Pyramid.transform;
}
}


}

And if you create pyramid of height 10, it 'll look like,

So, Friends this is how you can create a pyramid using an editor script in unity3d.

For any comments and suggestions please write to me in the following comment box..
Thanks