TC_Randomizer.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TerrainComposer2
  4. {
  5. [ExecuteInEditMode]
  6. public class TC_Randomizer : MonoBehaviour
  7. {
  8. public TC_ItemBehaviour item;
  9. public TC_RandomSettings r;
  10. public bool randomize;
  11. void Awake()
  12. {
  13. item = GetComponent<TC_ItemBehaviour>();
  14. }
  15. void Update()
  16. {
  17. if (randomize)
  18. {
  19. randomize = false;
  20. Randomize();
  21. }
  22. }
  23. void Randomize()
  24. {
  25. if (r == null || item == null) return;
  26. int amount = Random.Range(r.amount.x, r.amount.y);
  27. for (int i = 0; i < amount; i++)
  28. {
  29. Vector3 pos = new Vector3(Random.Range(r.posX.x, r.posX.y), 0, Random.Range(r.posZ.x, r.posZ.y));
  30. float rotY = Random.Range(r.rotY.x, r.rotY.y);
  31. float scaleX = Random.Range(r.scaleX.x, r.scaleX.y);
  32. Vector3 scale = new Vector3(scaleX, Random.Range(r.scaleY.x, r.scaleY.y), scaleX);
  33. TC_ItemBehaviour newItem = item.Duplicate(item.t.parent);
  34. newItem.t.position = pos;
  35. newItem.t.rotation = Quaternion.Euler(0, rotY, 0);
  36. newItem.t.localScale = scale;
  37. newItem.method = Method.Max;
  38. }
  39. TC.AutoGenerate();
  40. }
  41. }
  42. }