ItemsGenerator.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using UnityEngine;
  2. namespace SoftMasking.Samples {
  3. public class ItemsGenerator : MonoBehaviour {
  4. public RectTransform target;
  5. public Sprite image;
  6. public int count;
  7. public string baseName;
  8. public Item itemPrefab;
  9. static readonly Color[] colors = new[] {
  10. Color.red,
  11. Color.green,
  12. Color.blue,
  13. Color.cyan,
  14. Color.yellow,
  15. Color.magenta,
  16. Color.gray
  17. };
  18. public void Generate() {
  19. DestroyChildren();
  20. var startColor = Random.Range(0, colors.Length - 1);
  21. for (int i = 0; i < count; ++i) {
  22. var item = Instantiate(itemPrefab);
  23. item.transform.SetParent(target, false);
  24. item.Set(
  25. string.Format("{0} {1:D2}", baseName, i + 1),
  26. image,
  27. colors[(startColor + i) % colors.Length],
  28. Random.Range(0.4f, 1),
  29. Random.Range(0.4f, 1));
  30. }
  31. }
  32. void DestroyChildren() {
  33. while (target.childCount > 0)
  34. DestroyImmediate(target.GetChild(0).gameObject);
  35. }
  36. }
  37. }