Skip to content

Instantly share code, notes, and snippets.

@TeraokaAkihiro
Created August 26, 2017 08:24
Show Gist options
  • Save TeraokaAkihiro/6ae6c0332a09172764d9ebcdaf9cebcb to your computer and use it in GitHub Desktop.
Save TeraokaAkihiro/6ae6c0332a09172764d9ebcdaf9cebcb to your computer and use it in GitHub Desktop.
WallpaperEngine向け、タイル表示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TileController : MonoBehaviour {
public GameObject tilePrefab;
public GameObject[,] tile;
private int tileX,tileY;
private Vector2 tileSize;
void Start ()
{
tileX = 16;
tileY = 9;
tileSize = new Vector2(Screen.width / tileX, Screen.height / tileY);
tile = new GameObject[tileX,tileY];
SetTile(1,1);SetTile(0,1);
}
void Update ()
{
}
public void RemoveTile(int x, int y)
{
CheckTileRange(x, y);
if(tile[x, y])
{
Destroy(tile[x,y]);
}
}
public void SetTile(int x,int y)
{
CheckTileRange(x, y);
Vector3 pos = GetTilePos(x, y);
tile[x,y] = Instantiate(tilePrefab, pos, Quaternion.identity) as GameObject;
tile[x,y].transform.localScale = new Vector3(tileSize.x,tileSize.y,1);
}
public void SetTiles(GameObject[] tiles)
{
CheckTileRange(tiles.GetLength(0), tiles.GetLength(1));
}
public Vector3 GetTilePos(int x, int y)
{
return new Vector3(tileSize.x*x + tileSize.x/2, tileSize.y*y + tileSize.y/2, 0);
}
public void CheckTileRange(int x, int y)
{
if((tileX < x) || (tileY < y))
{
Debug.LogWarning("Out of range x or y");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment