Advertisement
Sabotender

GridExample

Apr 10th, 2014
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TileDrawer : MonoBehaviour {
  5.     public GameObject prefab;
  6.     public Vector2 gridSize;
  7.  
  8.     // Use this for initialization
  9.     void Start () {
  10.         for(int x = 0; x < gridSize.x; x++) {
  11.             for(int y = 0; y < gridSize.y; y++) {
  12.                 //GameObject go = (GameObject)Instantiate(prefab);
  13.                 GameObject go = new GameObject();
  14.                 go.transform.position = new Vector3(x,0,y);
  15.                 go.tag = "Grid";
  16.                 go.name = "Grid";
  17.                 BoxCollider col = go.AddComponent<BoxCollider>();
  18.                 col.isTrigger = true;
  19.             }
  20.         }
  21.     }
  22.    
  23.     // Update is called once per frame
  24.     void Update () {
  25.         if(Input.GetMouseButton(0)) {
  26.             CreateTile();
  27.         }
  28.     }
  29.  
  30.     void OnDrawGizmos() {
  31.         for(int x = 0; x < gridSize.x; x++) {
  32.             for(int y = 0; y < gridSize.y; y++) {
  33.                 for(int z = 0; z < 10; z++) {
  34.                 Gizmos.DrawWireCube(new Vector3(x,z,y), Vector3.one);
  35.                 }
  36.             }
  37.         }
  38.     }
  39.  
  40.     void CreateTile() {
  41.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  42.         RaycastHit hit;
  43.         if(Physics.Raycast(ray, out hit)) {
  44.             if(hit.collider.gameObject.tag == "Grid") {
  45.                 Vector3 pos = hit.collider.transform.position;
  46.                 GameObject go = (GameObject)Instantiate(prefab);
  47.                 go.transform.position = new Vector3(pos.x, pos.y, pos.z);
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement