Changing multiple identical gameobjects into one prefab?
I'm creating a game based on the Minecraft game 'Spleef'. I am doing a prototype currently, but I placed a plane, put a cube on it and copied and pasted that cube to fill up the plane. I made this a larger plane so there are 400 cubes currently on it. I renamed all of the cubes to 'Cube' and drug one into a Prefab folder to create a prefab out of it. I need to use a prefab in my script, and I don't want to have to go back and manually change each cube to the prefab version. Is there anyway to change all of the gameobjects named Cube to the prefab? Or how could I do this easier than placing and moving each cube one by one again?
Answer by dhore · Mar 25, 2016 at 01:32 AM
Hey @ltrout1999 :) good to see you again
I've just spent about an hour playing around with a script for you, so here it is:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class PlaceCubes : MonoBehaviour {
[MenuItem("MyMenu/PlaceCubes ")]
static void Place()
{
// change these to work for you:
string prefabName = "Cube"; // case sensitive
Vector3 gridSize = new Vector3(10, 1, 5); // ie. (10, 1, 5) will generate 10 cubes in the x direction, 1 in the y, and 5 in the z
Vector3 offset = new Vector3(1, 1, 1); // size of the cubes - default is (1, 1, 1)
string newCubeName = ""; // leave blank for default name eg. "Cube(Clone)"
GameObject cube = Resources.Load(prefabName, typeof(GameObject)) as GameObject;
for (int x = 0; x < gridSize.x; ++x)
{
for (int y = 0; y < gridSize.y; ++y)
{
for (int z = 0; z < gridSize.z; ++z)
{
Vector3 pos = new Vector3(x * offset.x, y * offset.y, z * offset.z);
GameObject go = Instantiate(cube, pos, cube.transform.rotation) as GameObject;
if (newCubeName != "")
go.name = newCubeName;
}
}
}
}
}
So what this'll do is it'll provide a Menu at the top (where File, Edit, etc are) called "MyMenu" with a Menu Item called "PlaceCubes", which when you click it will place cubes in the scene. (Note: the script does not need to be attached to any gameobjects to work).
There are some 'settings' at the top of the function which you'll need to manually change in the script to work to your liking - they are:
"prefabName" : the name of the prefab object. Note: the prefab must be in a folder called "Resources". If the object is then in a subfolder of Resources then this name should be the path to the object (using forward slashes). eg. if your prefab is at Assets/Resources/Prefabs/Cube then this name should be set to "Prefabs/Cube"
"gridSize" : the amount of cubes you want to generate in each direction.
"offset" : the size of the cube - used to space them apart (which can literally be used for spacing if you want).
"newCubeName" : a name for your new cubes.
I've tested this and it works fine on my end. But let me know if you have any issues :)
Enjoy, dhore.
Seems to work fairly well! Thank you!
Question, would there be anyway I could make it to where ins$$anonymous$$d of inputting the size in the code, to click on the button and it ask for a size there? That way I could use one code to quickly create a certain amount of blocks w/out having to change the code each time.
Not a problem :)
There probably is a way to do that, but I didn't have time to go looking when I wrote it as I was about to head out for the weekend. I also kind of wanted to add that feature too, but yeah.. no time which sucked...