Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. public enum TileType
  4. {
  5. Floor,
  6. WallEcorner,
  7. WallEfull,
  8. WallNcorner,
  9. WallNE,
  10. WallNfull,
  11. WallNW,
  12. WallScorner,
  13. WallSE,
  14. WallSfull,
  15. WallSW,
  16. WallWcorner,
  17. WallWFull
  18. }
  19.  
  20. [ExecuteInEditMode]
  21. public class SnapToGrid : MonoBehaviour {
  22. public TileType tileType;
  23. public float height = 0.64f;
  24. public float width = 1.28f;
  25. public List<Sprite> allStyles = new List<Sprite>();
  26. private int sortingOrderNum;
  27. private void Start()
  28. {
  29. ChosenStyleEditor.OnStyleChange += OnChangingStyle;
  30. }
  31. private void Update()
  32. {
  33. transform.position = new Vector2(Mathf.Round(transform.position.x / width) * width, Mathf.Round(transform.position.y / height) * height);
  34. if (this.tileType == TileType.WallNcorner || this.tileType == TileType.WallNfull || this.tileType == TileType.WallNW || this.tileType == TileType.WallNE)
  35. {
  36. GetComponent<SpriteRenderer>().sortingOrder = -(int)(transform.position.y * 10) - 1;
  37. }
  38. else if (this.tileType == TileType.Floor)
  39. {
  40. GetComponent<SpriteRenderer>().sortingOrder = -32000;
  41. }
  42. else
  43. {
  44. GetComponent<SpriteRenderer>().sortingOrder = -(int)(transform.position.y * 10);
  45. }
  46. }
  47.  
  48. private void OnChangingStyle(int num)
  49. {
  50. try
  51. {
  52. GetComponent<SpriteRenderer>().sprite = allStyles[num];
  53. }
  54. catch
  55. {
  56. Debug.LogError("Chosen style sprite is not set up in " + this.gameObject.name + "!");
  57. }
  58. }
  59.  
  60. private void OnDestroy()
  61. {
  62. ChosenStyleEditor.OnStyleChange -= OnChangingStyle;
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement