RemoveTree.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using UnityEngine;
  2. using System.Collections;
  3. using TerrainComposer2;
  4. public class RemoveTree : MonoBehaviour {
  5. public bool removeTree;
  6. public int index = 0;
  7. public GameObject prefab;
  8. void Update () {
  9. if (removeTree)
  10. {
  11. removeTree = false;
  12. RemoveTreeAtIndex(index);
  13. }
  14. }
  15. void RemoveTreeAtIndex(int index)
  16. {
  17. TerrainData terrainData = TC_Area2D.current.terrainAreas[0].terrains[0].terrain.terrainData;
  18. prefab = terrainData.treePrototypes[0].prefab;
  19. TreeInstance tree = terrainData.GetTreeInstance(index);
  20. float height = tree.heightScale;
  21. float width = tree.widthScale;
  22. Vector3 pos = tree.position;
  23. pos.Scale(terrainData.size);
  24. pos -= new Vector3(1024, 0, 1024);
  25. float rotation = tree.rotation * Mathf.Rad2Deg;
  26. tree.heightScale = 0;
  27. tree.widthScale = 0;
  28. terrainData.SetTreeInstance(index, tree);
  29. GameObject go = (GameObject)Instantiate(prefab, pos, Quaternion.Euler(0, rotation, 0));
  30. go.transform.localScale = new Vector3(width, height, width);
  31. this.index++;
  32. }
  33. }