Advertisement
Guest User

GameMap_Blockmap

a guest
Dec 19th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class GameMap_Blockmap : MonoBehaviour {
  6.  
  7.     public GameObject BasicBlock;
  8.     public Vector2 Mapsize = Vector2.zero;
  9.  
  10.     private Mesh BasicMesh, CurrentMesh;
  11.     private Vector3 Meshsize, InstantPos;
  12.     private int ix,iy;
  13.  
  14.     public class BlockmapClass{
  15.         public GameObject Block;
  16.         public int TileID;
  17.         public int[] TopVertex;
  18.         public int[] GroundVertex;
  19.  
  20.         public BlockmapClass(){
  21.             Block = new GameObject("BasicBlock");
  22.             TileID = 0;
  23.             TopVertex = new int[4];
  24.             GroundVertex = new int[4];
  25.         }
  26.     }
  27.    
  28.     public BlockmapClass[][] Blockmap;
  29.    
  30.     // Use this for initialization
  31.     void Start () {
  32.         CreateMap();
  33.     }
  34.  
  35.  
  36.     public void CreateMap(){
  37.         StartCoroutine(CreateBlocks());
  38.     }
  39.  
  40.     IEnumerator CreateBlocks(){
  41.         BasicMesh = BasicBlock.GetComponent<MeshFilter>().sharedMesh;
  42.         Meshsize = BasicMesh.bounds.size;
  43.         Blockmap = new BlockmapClass[(int)Mapsize.y][];
  44.         for(iy=0; iy<Mapsize.y; iy++){
  45.             Blockmap[iy] = new BlockmapClass[(int)Mapsize.x];
  46.         }
  47.         yield return new WaitForEndOfFrame();
  48.         for(iy=0; iy<Mapsize.y; iy++){
  49.             for(ix=0; ix<Mapsize.x; ix++){
  50.                 Debug.Log(iy + "/" + ix);
  51.                 InstantPos.x = Mapsize.x*Meshsize.x;
  52.                 InstantPos.y = 0;
  53.                 InstantPos.z = Mapsize.y*Meshsize.z;
  54.  
  55.                 Blockmap[iy][ix].Block = Instantiate(BasicBlock, InstantPos, Quaternion.Euler(Vector3.zero)) as GameObject;
  56.                
  57.                 Blockmap[iy][ix].TileID = 0;
  58.                
  59.                 CurrentMesh = Blockmap[iy][ix].Block.GetComponent<MeshFilter>().sharedMesh;
  60.                
  61.                 Blockmap[iy][ix].TopVertex[0] = CurrentMesh.triangles[0];
  62.                 Blockmap[iy][ix].TopVertex[1] = CurrentMesh.triangles[1];
  63.                 Blockmap[iy][ix].TopVertex[2] = CurrentMesh.triangles[2];
  64.                 Blockmap[iy][ix].TopVertex[3] = CurrentMesh.triangles[3];
  65.  
  66.                 Blockmap[iy][ix].GroundVertex[0] = CurrentMesh.triangles[4];
  67.                 Blockmap[iy][ix].GroundVertex[1] = CurrentMesh.triangles[5];
  68.                 Blockmap[iy][ix].GroundVertex[2] = CurrentMesh.triangles[6];
  69.                 Blockmap[iy][ix].GroundVertex[3] = CurrentMesh.triangles[7];
  70.  
  71.                 Blockmap[iy][ix].Block.transform.parent = transform;
  72.             }
  73.             yield return new WaitForEndOfFrame();
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement