Created
February 9, 2018 05:17
-
-
Save Domiii/01e6b4c49426294b797448851eb3ff0b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Spawns a stack of bricks | |
*/ | |
using UnityEngine; | |
using System.Collections; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public class StackSpawner : MonoBehaviour | |
{ | |
public int nBricks = 10; | |
public float breakForce = 1000; | |
public float breakTorque = 1000; | |
public Rigidbody brickPrefab; | |
public float Gap = 0f; | |
void Start () | |
{ | |
CreateBricks (); | |
} | |
public void CreateBricks () | |
{ | |
Clear (); | |
Rigidbody lastBrick = null; | |
for (var i = 0; i < nBricks; ++i) { | |
var brick = Instantiate (brickPrefab); | |
brick.transform.SetParent (transform, false); | |
var cubeSize = brick.GetComponent<Collider> ().bounds.extents; | |
var y = i * (2 * cubeSize.y + Gap); | |
brick.transform.localPosition = new Vector3 (0, y, 0); | |
if (lastBrick) { | |
var joint = brick.gameObject.AddComponent<FixedJoint> (); | |
joint.connectedBody = lastBrick; | |
joint.breakForce = breakForce; | |
joint.breakTorque = breakTorque; | |
} | |
lastBrick = brick; | |
} | |
} | |
public void Clear () | |
{ | |
for (var i = transform.childCount - 1; i >= 0; --i) { | |
DestroyImmediate (transform.GetChild (i).gameObject); | |
} | |
} | |
} | |
#if UNITY_EDITOR | |
[CustomEditor (typeof(StackSpawner))] | |
public class StackSpawnerEditor : Editor | |
{ | |
public override void OnInspectorGUI () | |
{ | |
base.OnInspectorGUI (); | |
var t = (StackSpawner)target; | |
if (GUILayout.Button ("Build")) { | |
t.CreateBricks (); | |
} | |
if (GUILayout.Button ("Clear")) { | |
t.Clear (); | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment