- Home /
Instantiating A Grass Prefab Multiple Times
So I want to instantiate a grass prefab I have multiple times. The goal is to instantiate what will, to the player, look like a field of grass, and then make it cuttable with something like Piecemaker. However, as a person new to coding (although I have a VERY basic knowledge of C++) I'm having trouble getting this done. I've looked into Object.Instantiate, and it might do what I want, but again I'm a noob coder and have very little idea how to use it appropriately. Also, I realize the large amount of system resources that this would probably take up, so if anyone has a different method of creating cuttable fields of grass, please let me know.
Answer by Jaap Kreijkamp · Jan 23, 2012 at 01:11 AM
Using game objects for every individual patch of grass probably means having lots and lots of game objects, resulting in slow down. I don't know if Unity Terrain engine is an option for you, if so you could make grass detail objects and dynamically update the amount of grass detail in the terrain.
void UpdateGrass(float x, float y, int ) {
int offsetX = (x * myTerrain.terrainData.detailResolution / TERRAIN_WIDTH);
int offsetY = (y * myTerrain.terrainData.detailResolution / TERRAIN_HEIGHT);
int[,] newDetail = new int[,] { {0} };
myTerrain.terrainData.SetDetailLayer(offsetX, offsetY, GRASS_LAYER, newDetailAmount);
}
If terrain engine isn't an option, consider combing the instances of the gameobjects, there should be some examples around on the Unity wiki. If you can't find anything just drop me a note. Of course, combining them makes it a bit harder to remove an individual patch.
If you just want to work with dynamically instantiate the grass and not worry about performance, look in the scripting reference for `Object.Instantiate'. Should give a fairly decent description of how it works.
Yeah, I wrote a pretty crappy script that, for some reason unknown to the world in general, sort of works. It uses Object.Instantiate. And by terrain engine, are you referring to the default Unity terrain engine? And my final noob question is: what do you mean by detail objects? Sorry for my persistent and probably annoying questions, but I really want to learn Unity and learn it well. Thanks!
Whoops, sorry. Forgot about the Oracle o' Google, and when I googled Detail object I found Detail $$anonymous$$esh. Thanks!
You already answered the question, it's part if the Unity Terrain Engine. You basically have splatmap (terrain textures), Trees and Detail objects. The detail objects is a way to quickly render lots of detail to the terrain with normally a short distance cutoff. The detail objects are placed procedural, so you have no full control over where a patch ends of exactly within the 'tile' but you say how much detail instances should be in the cell. You have two types of detail, objects, which have a mesh and detail textures/billboards, these generate rectangles with your texture using a transparency shader. This is the cheapest way of adding detail.
The terrain engine isn't available for mobile platforms by the way.
That's odd. I added my grass prefab to the detail mesh, but when I paint on the terrain nothing happens.
So I realize it's been a while since I last looked at this thread, but I'm still interested in finding a cool solution to this. I realized that there's a free fake mesh slicer at http://unitycoder.com/blog/2011/08/09/fake-mesh-slicer-v3-0/ which would probably work for cutting the grass, so my questions now are: how can i paint a mesh as if it was grass, and 2: are those meshes actual meshes or are they changed by the terrain engine?