void ImportImage(BaseMesh _target, Texture2D _sourceTexture) { if (_sourceTexture == null) { EditorUtility.DisplayDialog("Error!", "Import image field is empty. " + "Please assign any image from project to import it as a level.\n " + "And please make that texture read/write enabled in import settings.", "Got it!"); return; } //Just to make the texture readable from Import settings. string path = AssetDatabase.GetAssetPath(_sourceTexture); TextureImporter importSetting = (TextureImporter)AssetImporter.GetAtPath(path); importSetting.isReadable = true; importSetting.generateMipsInLinearSpace = false; importSetting.npotScale = TextureImporterNPOTScale.None; AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate); //Parsing image and converting it into Map. _target.GridColumnCount = _sourceTexture.width; _target.GridRowCount = _sourceTexture.height; GridArray [] tempGridArray = new GridArray[_target.GridColumnCount]; for (int i = 0; i < tempGridArray.GetLength(0); i++) { tempGridArray[i] = new GridArray(new Grid [_target.GridRowCount]); } for (int i = 0; i < _target.GridColumnCount; i++) { for (int j = 0; j < _target.GridRowCount; j++) { Color readColor = _sourceTexture.GetPixel(i, j); bool filled; if (((readColor.r + readColor.g + readColor.b) / 3) < 0.5f) { filled = true; } else { filled = false; } tempGridArray[i].gridRow[j] = new Grid(filled, i, j, new Vector3(_target.GridXOffset + i * _target.gridScale + _target.gridScale / 2, _target.yOffset, _target.GridZOffset + j * _target.gridScale + _target.gridScale / 2)); } } _target.gridArrayColumn = tempGridArray; }
void RefreshGridArray() { if (gridArrayColumn == null) { gridArrayColumn = new GridArray [gridColumnCount]; for (int i = 0; i < gridArrayColumn.GetLength(0); i++) { gridArrayColumn[i] = new GridArray(new Grid [gridRowCount]); } } GridArray [] tempGridArray = new GridArray[gridColumnCount]; for (int i = 0; i < tempGridArray.GetLength(0); i++) { tempGridArray[i] = new GridArray(new Grid [gridRowCount]); } for (int i = 0; i < gridColumnCount; i++) { for (int j = 0; j < gridRowCount; j++) { if (i < gridArrayColumn.GetLength(0) && gridArrayColumn[i] != null && j < gridArrayColumn[i].gridRow.GetLength(0) && gridArrayColumn [i].gridRow [j] != null) { tempGridArray[i].gridRow[j] = gridArrayColumn[i].gridRow[j]; tempGridArray[i].gridRow[j].pos = new Vector3(gridXOffset + i * gridScale + gridScale / 2, yOffset, gridZOffset + j * gridScale + gridScale / 2); } else { tempGridArray[i].gridRow[j] = new Grid(false, i, j, new Vector3(gridXOffset + i * gridScale + gridScale / 2, yOffset, gridZOffset + j * gridScale + gridScale / 2)); } } } gridArrayColumn = null; gridArrayColumn = tempGridArray; }