Set tree instance prototypIndex?

I know that undocumented APIs are use at your own risk, if at all, but I was hoping for a bit of help under the hood with the Terrain API.

Since it turns out that each tree type you add to a terrain has a performance impact, I am creating a slimmed down version of our terrain for integrated graphics. As part of that simplification, I want to reassign the tree prototypes of tree instances that are already placed in the landscape. After reassigning all the trees of one tree type to another type, the tree type that was vacated would be removed from the terrain.

The code below successfully accesses the tree prototype index, but doesn’t seem to set it. I was wondering whether there’s either some other function that should be called to set the index, or whether there’s some other update function that needs to be called to refresh the terrain’s contents overall after setting it. Or, maybe in fact that is read-only after the tree instance is added and I’m out of luck.

	var fromPrototypeIndex: int;
	var toPrototypeIndex: int;
        private var terrainData : TerrainData;

	for (var i:int=0; i<terrainData.treeInstances.length; i++)
		{
			Debug.Log("is: "+terrainData.treeInstances[i].prototypeIndex); // this works
			if (terrainData.treeInstances[i].prototypeIndex == fromPrototypeIndex)
			{
				// this doesn't work -- appears to be read-only even thouggh there is a setter function
				terrainData.treeInstances[i].prototypeIndex = toPrototypeIndex;
				Debug.Log("new: "+terrainData.treeInstances[i].prototypeIndex);
			}
		}
        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(terrainData));

I had many problems manipulating the tree prototypes from code, and finally abandoned the idea.

Unity guys, any news or answers about this ?
We definitively need this to optimize our terrains…

Something like this

TreeInstance[] oldTrees = terrainData.treeInstances; 
terrainData.treeInstances = new TreeInstance[0];
for (int i = 0; i < oldTrees.Length; i++)
{
	switch (oldTrees[i].prototypeIndex)
	{
		case 0:
			oldTrees[i].prototypeIndex = 1;
			break;
		//etc.
	}
	terrain.AddTreeInstance(oldTrees[i]);
}

You might need to do this afterwards to get the proper y locations for the trees…

terrainData.SetHeights(0, 0, terrainData.GetHeights(0,0,1,1));

Ok, i’m just stupid. TreeInstance is a struct so modifying its values won’t do a thing since it’s a copy.
Thanks for your help. BTW the trees seem to be well placed even if I don’t call the SetHeights method.

Interesting, maybe there is a private property of TreeInstance which set’s it’s height (makes sense). You’ll want to make sure that the collider is updating properly too though… without the call to SetHeights it’s likely keeping the collider from before you updated the trees, could be a problem if your replacement tree is a different size from the original. You could test that pretty easily by replacing an old tree with a colliderless tree and seeing what happens.