private void tileOnGUI() { tileScrollPosition = EditorGUILayout.BeginScrollView(tileScrollPosition, true, true, GUILayout.Width(tileElement.contentRect.width), GUILayout.Width(tileElement.contentRect.height)); int rows = 0; int cols = 0; switch (layersPopupField.value) { case "Environment": for (int i = 0; i < environmentTiles.Count; i++) { if (i != 0 && i % 6 == 0) { rows++; cols = 0; } Rect drawRect = new Rect(cols * tileCell, rows * tileCell, tileCell, tileCell); Texture t = environmentTiles[i].sprite.sprite.texture; Rect spriteRect = environmentTiles[i].sprite.sprite.rect; spriteRect.x /= t.width; spriteRect.y /= t.height; //spriteRect.y = 1 - spriteRect.y; spriteRect.width /= t.width; spriteRect.height /= t.height; GUI.DrawTextureWithTexCoords(drawRect, t, spriteRect); cols++; } break; case "StaticObjects": for (int i = 0; i < staticObjectTiles.Count; i++) { if (i != 0 && i % 6 == 0) { rows++; cols = 0; } Rect drawRect = new Rect(cols * tileCell, rows * tileCell, tileCell, tileCell); Texture t = staticObjectTiles[i].sprite.sprite.texture; Rect spriteRect = staticObjectTiles[i].sprite.sprite.rect; spriteRect.x /= t.width; spriteRect.y /= t.height; //spriteRect.y = 1 - spriteRect.y; spriteRect.width /= t.width; spriteRect.height /= t.height; GUI.DrawTextureWithTexCoords(drawRect, t, spriteRect); cols++; } break; case "Enemies": for (int i = 0; i < enemySpawnerTiles.Count; i++) { if (i != 0 && i % 6 == 0) { rows++; cols = 0; } Rect drawRect = new Rect(cols * tileCell, rows * tileCell, tileCell, tileCell); Texture t = enemySpawnerTiles[i].sprite.sprite.texture; Rect spriteRect = enemySpawnerTiles[i].sprite.sprite.rect; spriteRect.x /= t.width; spriteRect.y /= t.height; //spriteRect.y = 1 - spriteRect.y; spriteRect.width /= t.width; spriteRect.height /= t.height; GUI.DrawTextureWithTexCoords(drawRect, t, spriteRect); cols++; } break; case "Players": for (int i = 0; i < playerTiles.Count; i++) { if (i != 0 && i % 6 == 0) { rows++; cols = 0; } Rect drawRect = new Rect(cols * tileCell, rows * tileCell, tileCell, tileCell); Texture t = playerTiles[i].sprite.sprite.texture; Rect spriteRect = playerTiles[i].sprite.sprite.rect; spriteRect.x /= t.width; spriteRect.y /= t.height; //spriteRect.y = 1 - spriteRect.y; spriteRect.width /= t.width; spriteRect.height /= t.height; GUI.DrawTextureWithTexCoords(drawRect, t, spriteRect); cols++; } break; } EditorGUILayout.LabelField("", GUILayout.Width(6 * tileCell), GUILayout.Height(rows * tileCell)); MarkDirtyRepaint(); LevelEditorWindow.RepaintWindow(); GUI.EndScrollView(); }
void OnMapMouseMove(MouseMoveEvent evt) { if (!paint) { return; } if (selectedScriptableObject == null) { return; } if ((evt.localMousePosition.x < mapElement.contentRect.width - 15) && (evt.localMousePosition.y < mapElement.contentRect.height - 15)) { int mapCellStartX = Mathf.FloorToInt(evt.localMousePosition.x + mapScrollPosition.x) / cell; int mapCellStartY = Mathf.FloorToInt(evt.localMousePosition.y + mapScrollPosition.y) / cell; //Debug.Log(evt.localMousePosition + mapScrollPosition); //Debug.Log((evt.localMousePosition + mapScrollPosition) / cell); Vector2Int drawPos = new Vector2Int(mapCellStartX * cell, mapCellStartY * cell); int selectedLayer = 0; switch (layersPopupField.value) { case "Environment": selectedLayer = 0; break; case "StaticObjects": selectedLayer = 1; break; case "Enemies": selectedLayer = 2; break; case "Players": selectedLayer = 3; break; } //for (int i=0;i<level.layers.Length;i++) //{ Event e = Event.current; if (level.layers[selectedLayer].position == null || level.layers[selectedLayer].position.Count <= 0) { level.layers[selectedLayer].position.Add(drawPos); EditorUtility.SetDirty(level); level.layers[selectedLayer].objectOnPosition.Add(selectedScriptableObject); EditorUtility.SetDirty(level); } foreach (Vector2Int vi in level.layers[selectedLayer].position) { if (vi == drawPos) { int index = level.layers[selectedLayer].position.IndexOf(vi); if (e.modifiers == EventModifiers.Shift) { level.layers[selectedLayer].position.RemoveAt(index); EditorUtility.SetDirty(level); level.layers[selectedLayer].objectOnPosition.RemoveAt(index); EditorUtility.SetDirty(level); break; } else { level.layers[selectedLayer].position[index] = drawPos; EditorUtility.SetDirty(level); level.layers[selectedLayer].objectOnPosition[index] = (selectedScriptableObject); EditorUtility.SetDirty(level); break; } } else { level.layers[selectedLayer].position.Add(drawPos); EditorUtility.SetDirty(level); level.layers[selectedLayer].objectOnPosition.Add(selectedScriptableObject); EditorUtility.SetDirty(level); break; } } //if (level.layers[i] == layersPopupField.value) //{ // Drawable temp; // if (ml.drawRects.TryGetValue(drawPos, out temp)) // { // Event e = Event.current; // if (e.modifiers == EventModifiers.Shift) // { // ml.drawRects.Remove(drawPos); // } // else // { // if (!string.IsNullOrEmpty(selectedDrawable.assetPath)) // { // temp.assetPath = selectedDrawable.assetPath; // temp.texCoords = selectedDrawable.texCoords; // if (ml.drawRects[drawPos].Equals(temp)) // { // ml.drawRects.Remove(drawPos); // } // else // { // ml.drawRects[drawPos] = temp; // } // } // } // } // else // { // Event e = Event.current; // if (e.modifiers == EventModifiers.Shift) // { // } // else // { // if (!string.IsNullOrEmpty(selectedDrawable.assetPath)) // { // ml.drawRects.Add(drawPos, selectedDrawable); // } // } // } //} //} } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); MarkDirtyRepaint(); LevelEditorWindow.RepaintWindow(); }
private void mapOnGUI() { mapScrollPosition = EditorGUILayout.BeginScrollView(mapScrollPosition, true, true, GUILayout.Width(mapElement.contentRect.width), GUILayout.Width(mapElement.contentRect.height)); //if (textureObjectField.value != null) //{ // Texture imageToDraw = (Texture)textureObjectField.value; // Rect drawRect = new Rect(0, 0, imageToDraw.width, imageToDraw.height); // Rect destRect = // GUI.DrawTextureWithTexCoords(drawRect, imageToDraw, drawRect,true); //} //Rect sizeRect = new Rect(0, 0, rows * cell, cols * cell); EditorGUILayout.LabelField("", GUILayout.Width(cols * cell), GUILayout.Height(rows * cell)); for (int i = 0; i <= rows; i++) { EditorGUI.DrawRect(new Rect(0, i * cell, cols * cell, 2), Color.white); } for (int j = 0; j <= cols; j++) { EditorGUI.DrawRect(new Rect(j * cell, 0, 2, rows * cell), Color.white); } for (int layerindex = 0; layerindex < 4; layerindex++) { for (int i = 0; i < level.layers[layerindex].position.Count; i++) { Rect drawRect = new Rect(cols * tileCell, rows * tileCell, tileCell, tileCell); Texture t; Rect spriteRect; if (layerindex == 0) { Sprite s = ((EnvironmentObject)(level.layers[layerindex].objectOnPosition[i])).sprite.sprite; t = s.texture; spriteRect = s.rect; } else if (layerindex == 1) { Sprite s = ((StaticObject)(level.layers[layerindex].objectOnPosition[i])).sprite.sprite; t = s.texture; spriteRect = s.rect; } else if (layerindex == 2) { Sprite s = ((EnemyObject)(level.layers[layerindex].objectOnPosition[i])).sprite.sprite; t = s.texture; spriteRect = s.rect; } else if (layerindex == 3) { Sprite s = ((PlayerObject)(level.layers[layerindex].objectOnPosition[i])).sprite.sprite; t = s.texture; spriteRect = s.rect; } else { return; } spriteRect.x /= t.width; spriteRect.y /= t.height; spriteRect.y = 1 - spriteRect.y; spriteRect.width /= t.width; spriteRect.height /= t.height; GUI.DrawTextureWithTexCoords(drawRect, t, spriteRect); //GUI.DrawTextureWithTexCoords(drawRect, imageToDraw, drawRect, true); } } MarkDirtyRepaint(); LevelEditorWindow.RepaintWindow(); GUI.EndScrollView(); }