Guest User

Untitled

a guest
Jan 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Tilemaps;
  3.  
  4. /// <summary>
  5. /// A component that is also a Tile in a Tilemap. If the Tilemap is not
  6. /// specified it will default to the Tilemap in the parent Game Object.
  7. /// </summary>
  8. public class TileComponent : MonoBehaviour
  9. {
  10. public Tilemap tilemap;
  11.  
  12. Tile tile;
  13. Vector3 curPos;
  14.  
  15. public virtual void Start()
  16. {
  17. if (tilemap == null)
  18. {
  19. tilemap = GetComponentInParent<Tilemap>();
  20. }
  21.  
  22. tile = ScriptableObject.CreateInstance<Tile>();
  23. curPos = transform.position;
  24. SetTile(transform.position);
  25. }
  26.  
  27. /// <summary>
  28. /// Sets a tile representing the game object to the given location in the
  29. /// tilemap. Call this method whenever the game object moves to a new
  30. /// location on the tilemap.
  31. /// </summary>
  32. public void SetTile(Vector3 newPos)
  33. {
  34. tilemap.SetTile(tilemap.WorldToCell(curPos), null);
  35. tilemap.SetTile(tilemap.WorldToCell(newPos), tile);
  36. curPos = newPos;
  37. }
  38.  
  39. public virtual void OnDestroy()
  40. {
  41. tilemap.SetTile(tilemap.WorldToCell(curPos), null);
  42. }
  43. }
Add Comment
Please, Sign In to add comment