drakon-firestone

LabirynthGenerator

Jun 7th, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [System.Serializable]
  6. public class ColorToPrefab
  7. {
  8. public Color color;
  9. public Transform prefab;
  10. }
  11.  
  12. public class LabirynthGenerator : MonoBehaviour
  13. {
  14. [SerializeField] private Texture2D map;
  15. [SerializeField] private ColorToPrefab[] colorMappings;
  16. [SerializeField] private float offset = 5f;
  17.  
  18.  
  19. private void GenerateTile(int x, int z)
  20. {
  21. Color pixelColor = map.GetPixel(x, z);
  22.  
  23. if (pixelColor.a == 0) return;
  24.  
  25. foreach (ColorToPrefab mapping in colorMappings)
  26. {
  27. // jeżeli kolory się nie zgadzają - idziemy do kolejnego
  28. if (!mapping.color.Equals(pixelColor)) continue;
  29.  
  30. Vector3 position = new Vector3(x, 0, z) * offset;
  31.  
  32. Instantiate(mapping.prefab, position, Quaternion.identity, transform);
  33. }
  34. }
  35.  
  36. private void ClearLabirynth()
  37. {
  38. while (transform.childCount > 0)
  39. {
  40. DestroyImmediate(transform.GetChild(0).gameObject);
  41. }
  42. }
  43.  
  44.  
  45. public void GenerateLabirynth()
  46. {
  47. ClearLabirynth();
  48.  
  49. for (int x = 0; x < map.width; x++)
  50. {
  51.  
  52. for (int z = 0; z < map.height; z++)
  53. {
  54. GenerateTile(x, z);
  55. }
  56. }
  57. }
  58.  
  59. }
  60.  
Add Comment
Please, Sign In to add comment