Created
September 3, 2023 01:56
-
-
Save pemby/336fcb7dda795d8d0cbdd4456bf19b6a to your computer and use it in GitHub Desktop.
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 System.Collections; | |
using UnityEngine; | |
public class Main : MonoBehaviour | |
{ | |
public int numberOfCubes = 20; | |
public float cubeScale = 0.2f; | |
public float spacing = 1f; | |
public int[,] aConfig = new int[,] { | |
{0,0,1,1,1,0,0}, | |
{0,1,0,0,0,1,0}, | |
{1,0,0,0,0,0,1}, | |
{1,1,1,1,1,1,1}, | |
{1,0,0,0,0,0,1}, | |
{1,0,0,0,0,0,1}, | |
{1,0,0,0,0,0,1} | |
}; | |
int[,] bConfig = new int[,] { | |
{1,1,1,1,0,0,0}, | |
{1,0,0,0,1,0,0}, | |
{1,1,1,1,0,0,0}, | |
{1,0,0,0,1,0,0}, | |
{1,1,1,1,0,0,0}, | |
}; | |
int[,] cConfig = new int[,] { | |
{0,1,1,1,1,0}, | |
{1,0,0,0,0,1}, | |
{1,0,0,0,0,0}, | |
{1,0,0,0,0,0}, | |
{1,0,0,0,0,1}, | |
{0,1,1,1,1,0}, | |
}; | |
int[,] dConfig = new int[,] { | |
{1,1,1,0,0,0}, | |
{1,0,0,1,0,0}, | |
{1,0,0,1,0,0}, | |
{1,0,0,1,0,0}, | |
{1,1,1,0,0,0}, | |
}; | |
int[,] eConfig = new int[,] { | |
{1,1,1,1,0}, | |
{1,0,0,0,0}, | |
{1,1,1,1,0}, | |
{1,0,0,0,0}, | |
{1,1,1,1,0}, | |
}; | |
int[,] fConfig = new int[,] { | |
{1,1,1,1,0}, | |
{1,0,0,0,0}, | |
{1,1,1,1,0}, | |
{1,0,0,0,0}, | |
{1,0,0,0,0}, | |
}; | |
int[,] gConfig = new int[,] { | |
{0,1,1,1,1,0}, | |
{1,0,0,0,0,1}, | |
{1,0,0,0,0,0}, | |
{1,0,0,1,1,1}, | |
{1,0,0,0,0,1}, | |
{0,1,1,1,1,0}, | |
}; | |
int[,] hConfig = new int[,] { | |
{1,0,0,0,1}, | |
{1,0,0,0,1}, | |
{1,1,1,1,1}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1}, | |
}; | |
int[,] iConfig = new int[,] { | |
{1,1,1}, | |
{0,1,0}, | |
{0,1,0}, | |
{0,1,0}, | |
{1,1,1}, | |
}; | |
int[,] jConfig = new int[,] { | |
{0,0,1,1,1}, | |
{0,0,0,1,0}, | |
{0,0,0,1,0}, | |
{0,0,0,1,0}, | |
{0,1,1,1,0}, | |
}; | |
int[,] kConfig = new int[,] { | |
{1,0,0,0,1}, | |
{1,0,0,1,0}, | |
{1,0,1,0,0}, | |
{1,1,0,0,0}, | |
{1,0,1,0,0}, | |
{1,0,0,1,0}, | |
{1,0,0,0,1} | |
}; | |
int[,] lConfig = new int[,] { | |
{1,0,0,0}, | |
{1,0,0,0}, | |
{1,0,0,0}, | |
{1,0,0,0}, | |
{1,0,0,0}, | |
{1,0,0,0}, | |
{1,1,1,1} | |
}; | |
int[,] mConfig = new int[,] { | |
{1,0,0,0,1}, | |
{1,1,0,1,1}, | |
{1,0,1,0,1}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1} | |
}; | |
int[,] nConfig = new int[,] { | |
{1,0,0,0,1}, | |
{1,1,0,0,1}, | |
{1,0,1,0,1}, | |
{1,0,0,1,1}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1} | |
}; | |
int[,] oConfig = new int[,] { | |
{0,1,1,1,0}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1}, | |
{0,1,1,1,0} | |
}; | |
int[,] pConfig = new int[,] { | |
{1,1,1,1}, | |
{1,0,0,1}, | |
{1,0,0,1}, | |
{1,1,1,1}, | |
{1,0,0,0}, | |
{1,0,0,0}, | |
{1,0,0,0} | |
}; | |
int[,] qConfig = new int[,] { | |
{0,1,1,1,0}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1}, | |
{1,0,0,0,1}, | |
{1,0,1,0,1}, | |
{1,0,0,1,1}, | |
{0,1,1,1,1} | |
}; | |
int[,] rConfig = new int[,] { | |
{1,1,1,1}, | |
{1,0,0,1}, | |
{1,0,0,1}, | |
{1,1,1,1}, | |
{1,0,1,0}, | |
{1,0,0,1}, | |
{1,0,0,0} | |
}; | |
int[,] sConfig = new int[,] { | |
{0,1,1,1,1,0}, | |
{1,0,0,0,0,1}, | |
{1,0,0,0,0,0}, | |
{0,1,1,1,1,0}, | |
{0,0,0,0,0,1}, | |
{1,1,1,1,1,0} | |
}; | |
int[,] tConfig = new int[,] { | |
{1,1,1,1,1,1}, | |
{0,0,1,0,0,0}, | |
{0,0,1,0,0,0}, | |
{0,0,1,0,0,0}, | |
{0,0,1,0,0,0}, | |
{0,0,1,0,0,0} | |
}; | |
int[,] uConfig = new int[,] { | |
{1,0,0,0,0,1}, | |
{1,0,0,0,0,1}, | |
{1,0,0,0,0,1}, | |
{1,0,0,0,0,1}, | |
{1,0,0,0,0,1}, | |
{0,1,1,1,1,0} | |
}; | |
int[,] vConfig = new int[,] { | |
{1,0,0,0,0,1}, | |
{1,0,0,0,0,1}, | |
{1,0,0,0,0,1}, | |
{0,1,0,0,1,0}, | |
{0,0,1,1,0,0}, | |
{0,0,0,0,0,0} | |
}; | |
int[,] wConfig = new int[,] { | |
{1,0,0,0,0,1}, | |
{1,0,0,0,0,1}, | |
{1,0,0,0,0,1}, | |
{1,0,1,0,0,1}, | |
{0,1,0,1,0,0}, | |
{0,0,0,0,0,0} | |
}; | |
int[,] xConfig = new int[,] { | |
{1,0,0,0,0,1}, | |
{0,1,0,0,1,0}, | |
{0,0,1,1,0,0}, | |
{0,1,0,0,1,0}, | |
{1,0,0,0,0,1}, | |
{0,0,0,0,0,0} | |
}; | |
int[,] yConfig = new int[,] { | |
{1,0,0,0,0,1}, | |
{0,1,0,0,1,0}, | |
{0,0,1,1,0,0}, | |
{0,0,1,0,0,0}, | |
{0,0,1,0,0,0}, | |
{0,0,0,0,0,0} | |
}; | |
private int[,] zConfig = new int[,] | |
{ | |
{ 1, 1, 1, 1, 1, 1 }, | |
{ 0, 0, 0, 0, 1, 0 }, | |
{ 0, 0, 0, 1, 0, 0 }, | |
{ 0, 0, 1, 0, 0, 0 }, | |
{ 0, 1, 0, 0, 0, 0 }, | |
{ 1, 1, 1, 1, 1, 1 } | |
}; | |
private IEnumerator Start() | |
{ | |
yield return StartCoroutine(SpawnLetterA(aConfig)); | |
} | |
private IEnumerator SpawnLetterA(int[,] ints) | |
{ | |
// Check if "Cube" tag exists, and if not, create it | |
if (!TagExists("Cube")) | |
{ | |
AddTag("Cube"); | |
} | |
for (int i = ints.GetLength(0) - 1; i >= 0; i--) | |
{ | |
for (int j = 0; j < ints.GetLength(1); j++) | |
{ | |
if (ints[i, j] == 1) | |
{ | |
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); | |
cube.transform.localScale = Vector3.one * cubeScale; | |
var position = cube.transform.position; | |
position = new Vector3(j * (cubeScale + spacing), (ints.GetLength(0) - i - 1) * (cubeScale + spacing), 0); | |
cube.transform.position = position; | |
cube.tag = "Cube"; | |
SplitCube(cube, 3); // split into 2x2x2 smaller cubes | |
yield return new WaitForSeconds(0.01f); | |
} | |
} | |
} | |
yield return new WaitForSeconds(3f); | |
yield return StartCoroutine(ExplodeCubes()); | |
} | |
private IEnumerator ExplodeCubes() | |
{ | |
GameObject[] cubes = GameObject.FindGameObjectsWithTag("Cube"); | |
// Shuffle the array of cubes using the Fisher-Yates algorithm | |
for (int i = 0; i < cubes.Length - 1; i++) | |
{ | |
int randomIndex = Random.Range(i, cubes.Length); | |
GameObject temp = cubes[randomIndex]; | |
cubes[randomIndex] = cubes[i]; | |
cubes[i] = temp; | |
} | |
// Create a new material with the fire shader | |
Material cubeMaterial = new Material(Shader.Find("Custom/FireShader")); | |
cubeMaterial.SetColor("_Color", new Color(1f, 0.5f, 0f)); | |
cubeMaterial.SetFloat("_Distortion", 1f); | |
// Assign the material to each cube and add a rigidbody for the explosion | |
foreach (GameObject cube in cubes) | |
{ | |
cube.GetComponent<MeshRenderer>().material = cubeMaterial; | |
Rigidbody rb = cube.AddComponent<Rigidbody>(); | |
float explosionForce = Random.Range(100f, 500f); | |
float explosionRadius = Random.Range(2f, 5f); | |
rb.AddExplosionForce(explosionForce, transform.position, explosionRadius); | |
} | |
// Wait for a short time and then destroy the material and all the cubes | |
yield return new WaitForSeconds(2f); | |
Destroy(cubeMaterial); | |
foreach (GameObject cube in cubes) | |
{ | |
Destroy(cube); | |
} | |
} | |
/*private IEnumerator ExplodeCubes() | |
{ | |
GameObject[] cubes = GameObject.FindGameObjectsWithTag("Cube"); | |
foreach (GameObject cube in cubes) | |
{ | |
Rigidbody rb = cube.AddComponent<Rigidbody>(); | |
rb.AddExplosionForce(500f, transform.position, 5f); | |
yield return new WaitForSeconds(0.000000000005f); | |
} | |
}*/ | |
public void SplitCube(GameObject cube, int numSplits) | |
{ | |
Vector3 originalSize = cube.transform.localScale; | |
Vector3 smallSize = originalSize / 2; | |
for (int i = 0; i < numSplits; i++) | |
{ | |
for (int j = 0; j < numSplits; j++) | |
{ | |
for (int k = 0; k < numSplits; k++) | |
{ | |
// create smaller cube | |
GameObject smallCube = GameObject.CreatePrimitive(PrimitiveType.Cube); | |
smallCube.tag = "Cube"; | |
// set the position and scale of smaller cube | |
float x = (i + 0.5f) * smallSize.x; | |
float y = (j + 0.5f) * smallSize.y; | |
float z = (k + 0.5f) * smallSize.z; | |
smallCube.transform.position = cube.transform.position + new Vector3(x - (originalSize.x / 2), y - (originalSize.y / 2), z - (originalSize.z / 2)); | |
smallCube.transform.localScale = smallSize; | |
} | |
} | |
} | |
// destroy original cube | |
Destroy(cube); | |
} | |
// Helper method to check if a tag exists | |
private bool TagExists(string tag) | |
{ | |
string[] tags = UnityEditorInternal.InternalEditorUtility.tags; | |
for (int i = 0; i < tags.Length; i++) | |
{ | |
if (tags[i] == tag) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
// Helper method to add a new tag | |
private void AddTag(string tag) | |
{ | |
UnityEditorInternal.InternalEditorUtility.AddTag(tag); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment