Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- public enum TileType
- {
- Floor,
- WallEcorner,
- WallEfull,
- WallNcorner,
- WallNE,
- WallNfull,
- WallNW,
- WallScorner,
- WallSE,
- WallSfull,
- WallSW,
- WallWcorner,
- WallWFull
- }
- [ExecuteInEditMode]
- public class SnapToGrid : MonoBehaviour {
- public TileType tileType;
- public float height = 0.64f;
- public float width = 1.28f;
- public List<Sprite> allStyles = new List<Sprite>();
- private int sortingOrderNum;
- private void Start()
- {
- ChosenStyleEditor.OnStyleChange += OnChangingStyle;
- }
- private void Update()
- {
- transform.position = new Vector2(Mathf.Round(transform.position.x / width) * width, Mathf.Round(transform.position.y / height) * height);
- if (this.tileType == TileType.WallNcorner || this.tileType == TileType.WallNfull || this.tileType == TileType.WallNW || this.tileType == TileType.WallNE)
- {
- GetComponent<SpriteRenderer>().sortingOrder = -(int)(transform.position.y * 10) - 1;
- }
- else if (this.tileType == TileType.Floor)
- {
- GetComponent<SpriteRenderer>().sortingOrder = -32000;
- }
- else
- {
- GetComponent<SpriteRenderer>().sortingOrder = -(int)(transform.position.y * 10);
- }
- }
- private void OnChangingStyle(int num)
- {
- try
- {
- GetComponent<SpriteRenderer>().sprite = allStyles[num];
- }
- catch
- {
- Debug.LogError("Chosen style sprite is not set up in " + this.gameObject.name + "!");
- }
- }
- private void OnDestroy()
- {
- ChosenStyleEditor.OnStyleChange -= OnChangingStyle;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement