- Home /
Figured out issue. No longer a problem.
Custom Grid Object Always Starts at 0,0 of editor window.
I'm trying to make a custom map editor. I have created a custom rpg_Grid object, which my custom editor window uses two of. One for a palette of tiles, the other for the actual map.
Issue is, although the grid draw's fine, when clicking on the editor window the red circle is where the origin point is for both grids. So clicking in the red circle, it returns 0, 0 for BOTH grids, as it appears to be starting both at the top most left position of the window...
Code: public rpg_Grid (int width, int height, float cellSize, Vector3 origin) { this.width = width; this.height = height; this.cellSize = cellSize; this.origin = origin;
this.gridArray = new rpg_Tile[width,height];
int i=0;
for(int y = 0; y < gridArray.GetLength(1); y++)
{
for(int x = 0; x < gridArray.GetLength(0); x++)
{
gridArray[x,y] = new rpg_Tile();
i++;
}
}
}
public rpg_Grid (int width, int height, float cellSize, rpg_Tile[] contents, Vector3 origin)
{
this.width = width;
this.height = height;
this.cellSize = cellSize;
this.origin = origin;
this.gridArray = new rpg_Tile[width,height];
int i=0;
for(int y = 0; y < gridArray.GetLength(1); y++)
{
for(int x = 0; x < gridArray.GetLength(0); x++)
{
if(contents[i] != null)
{
gridArray[x,y] = contents[i];
i++;
}
}
}
}
private Vector3 GetWorldPosition(int x, int y)
{
return new Vector3(x,y) * cellSize + origin;
}
public bool GetXY (Vector3 worldPosition, out int x, out int y)
{
x = Mathf.FloorToInt((worldPosition - origin).x / cellSize);
y = Mathf.FloorToInt((worldPosition - origin).y / cellSize);
if (x >= 0 && y >= 0 && x < width && y < height)
{
return true;
}
x= -1;
y = -1;
return false;
}
public rpg_Tile SetValue(int x, int y)
{
if (x >= 0 && y >= 0 && x < width && y < height)
{
selectedValue = gridArray[x,y];
return selectedValue;
}
return null;
}
public void DrawGrid(bool drawGrid)
{
if (!drawGrid)
{
Handles.DrawLine(GetWorldPosition(0, 0), GetWorldPosition(0, height));
Handles.DrawLine(GetWorldPosition(0, 0), GetWorldPosition(width, 0));
}
for(int y = 0; y < gridArray.GetLength(1); y++)
{
for(int x = 0; x < gridArray.GetLength(0); x++)
{
if (drawGrid)
{
Handles.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1));
Handles.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y));
}
if (gridArray[x,y] != null)
{
if (gridArray[x,y].spriteTexture != null)
GUI.DrawTexture(new Rect(x*cellSize,y*cellSize, cellSize, cellSize), gridArray[x,y].spriteTexture);
}
}
}
Handles.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height));
Handles.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height));
}
public void SetTile(int x, int y, rpg_Tile tile)
{
if (x >= 0 && y >= 0 && x < width && y < height)
{
gridArray[x,y] = tile;
Debug.Log("Set Tile Called");
}
}
}
//I'm using the event system to get the mouse position in my map editor window;
if (e.type == EventType.MouseDown)
{
Vector2 mousePos = e.mousePosition;
//// code;
}
Follow this Question
Related Questions
Confused about custom GameObjects,Custom GameObject confusion 0 Answers
Detect if build target is installed 1 Answer
How do I solve a BuildAssetBundles Compilation Error that only occurs in the Editor? 1 Answer
Destroy GameObject without "killing" Parameters 2 Answers
Arrays/Lists in the inspector of Editor Extension scripts 0 Answers