public void CreateMap(int row, int col, Vector2 cellSize) { startPos = transform.position; this.cellSize = cellSize; this.row = row; this.column = col; cells = new MapCellDisplay[col, row]; GameObject prefab = Resources.Load(mapCellPrefab) as GameObject; if (prefab == null) { Debug.LogError("Can not load prefab:" + mapCellPrefab); return; } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { GameObject go = GameObject.Instantiate(prefab); go.transform.parent = this.transform; go.name = i + "_" + j; MapCellDisplay mcd = go.GetComponent <MapCellDisplay> (); MapCellData data = new MapCellData(); data.row = i; data.col = j; data.sortOrder = 0; data.sortLayer = go.GetComponent <SpriteRenderer> ().sortingLayerName; data.resName = go.GetComponent <SpriteRenderer> ().sprite.name; data.cellType = (int)CELL_TYPE.WALL; mcd.Init(this, data); cells [j, i] = mcd; } } }
void FromMapData(MapData md) { GameObject prefab = Resources.Load(mapCellPrefab) as GameObject; if (prefab == null) { Debug.LogError("Can not load prefab:" + mapCellPrefab); return; } startPos = new Vector3((float)md.startPosX, (float)md.startPosY, (float)md.startPosZ); transform.position = startPos; this.cellSize = new Vector2((float)md.cellWidth, (float)md.cellHeight); this.row = md.row; this.column = md.column; cells = new MapCellDisplay[column, row]; // foreach (MapCellData item in md.cells) { GameObject go = GameObject.Instantiate(prefab); go.transform.parent = this.transform; go.name = item.col + "_" + item.row; MapCellDisplay mcd = go.GetComponent <MapCellDisplay> (); mcd.Init(this, item); cells [item.col, item.row] = mcd; } // foreach (DecorationData item in md.decorations) { GameObject go = new GameObject(); go.AddComponent <SpriteRenderer> (); go.transform.parent = this.transform; go.name = item.resName; go.transform.position = new Vector3((float)item.posX, (float)item.posY, (float)item.posZ); go.transform.rotation = Quaternion.Euler(new Vector3((float)item.eulerX, (float)item.eulerY, (float)item.eulerZ)); go.transform.localScale = new Vector3((float)item.scaleX, (float)item.scaleY, (float)item.scaleZ); if (!string.IsNullOrEmpty(item.resName)) { go.GetComponent <SpriteRenderer> ().sprite = Resources.Load("Sprites/" + item.resName, typeof(Sprite)) as Sprite; } else { go.GetComponent <SpriteRenderer> ().sprite = null; } go.GetComponent <SpriteRenderer> ().sortingOrder = item.sortOrder; go.GetComponent <SpriteRenderer> ().sortingLayerName = item.sortLayer; } }
void OnEnable() { generator = new SerializedObject(target); m_mapDisplay = (MapCellDisplay)target; m_mapDisplay.Init(); IsToLeft = generator.FindProperty("IsToLeft"); IsToRight = generator.FindProperty("IsToRight"); IsToUp = generator.FindProperty("IsToUp"); IsToDown = generator.FindProperty("IsToDown"); IsToLeftUp = generator.FindProperty("IsToLeftUp"); IsToLeftDown = generator.FindProperty("IsToLeftDown"); IsToRightUp = generator.FindProperty("IsToRightUp"); IsToRightDown = generator.FindProperty("IsToRightDown"); IsAllOpen = generator.FindProperty("IsAllOpen"); IsAllClose = generator.FindProperty("IsAllClose"); IsObstacle = generator.FindProperty("IsObstacle"); }