Advertisement
Guest User

Randomiser

a guest
Feb 22nd, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. //Our random shape class/object
  6. public class RandomShape
  7. {
  8. public Sprite BG;
  9. public Sprite FG;
  10. public Color Color;
  11. }
  12.  
  13. //The class to create a random shape/color/fg color/bg color etc...
  14. public class Randomiser : MonoBehaviour
  15. {
  16. [Tooltip ("The random pictures generated from the randomiser..")]
  17. public List<RandomShape> Pictures = new List<RandomShape>();
  18. [Tooltip ("The number of iterations for randomisation.")]
  19. public int maxPictures = 3;
  20. // [Tooltip ("The background sprites")]
  21. // public SpriteRenderer[] bgSprites;
  22. //[Tooltip ("The foreground sprites")]
  23. //public SpriteRenderer[] fgSprites;
  24. // [Tooltip ("The possible colours.")]
  25.  
  26. //Enter the number of bgSprites you want to add in the inspector
  27. public Sprite[] bgSprites_;
  28. public Sprite[] fgSprites_;
  29. public Color[] Colors;
  30.  
  31. // Start is called before the first frame update
  32. void Start()
  33. {
  34.  
  35. for(int i = 0; i < maxPictures; i++)
  36. {
  37. RandomShape pic = new RandomShape();
  38. pic.BG = bgSprites_[GeneratePictures(bgSprites_.Length -1)];
  39. pic.FG = fgSprites_[GeneratePictures(fgSprites_.Length -1)];
  40. pic.Color = Colors[GeneratePictures(Colors.Length -1)];
  41. Debug.Log(pic.Color);
  42. Pictures.Add(pic);
  43. }
  44.  
  45. PlacePictures();
  46.  
  47.  
  48. }
  49.  
  50. int GeneratePictures(int ItemsLength)
  51. {
  52. int randomNumber = Random.Range(0,ItemsLength);
  53. return randomNumber;
  54. }
  55.  
  56.  
  57.  
  58. void PlacePictures()
  59. {
  60. float Spacing = 5f;
  61.  
  62. for(int i = 0; i < Pictures.Count-1; i++)
  63. {
  64.  
  65. GameObject newPicture = new GameObject();
  66. newPicture.AddComponent<SpriteRenderer>().sprite = Pictures[i].BG;
  67.  
  68. Instantiate(newPicture, new Vector3(0,i+Spacing,1), Quaternion.identity);
  69.  
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement