private void AddNodeClick(MBGraph target) { GameObject addedNode = target.AddNode(); Selection.activeGameObject = addedNode; EditorSceneManager.MarkSceneDirty(target.gameObject.scene); EditorUtility.SetDirty(target); }
private void GenerateGrid(MBGraph target) { ClearNodes(target); float width = target.__gridColumns * target.__gridCellSize; float height = target.__gridRows * target.__gridCellSize; Vector3 position = new Vector3(target.transform.position.x - (width / 2), target.transform.position.y, target.transform.position.z - (height / 2)); // 1st pass: generate nodes MBGraphNode[,] nodeGrid = new MBGraphNode[target.__gridRows, target.__gridColumns]; for (int r = 0; r < target.__gridRows; ++r) { float startingX = position.x; for (int c = 0; c < target.__gridColumns; ++c) { bool shouldAddNode = true; if (target.__detectCollisions) { List <Collider> colliders = FilterColliders(target, Physics.OverlapSphere(position, target.__collisionRadius)); shouldAddNode = colliders.Count == 0; } if (shouldAddNode) { GameObject addedNodeObj = target.AddNode(); addedNodeObj.transform.position = position; nodeGrid[r, c] = addedNodeObj.GetComponent <MBGraphNode>(); } position = new Vector3(position.x + target.__gridCellSize, position.y, position.z); } position = new Vector3(startingX, position.y, position.z + target.__gridCellSize); } // 2nd pass: generate edges for (int r = 0; r < target.__gridRows; ++r) { for (int c = 0; c < target.__gridColumns; ++c) { if (nodeGrid[r, c] == null) { continue; } // NWES edges if (c > 0 && nodeGrid[r, c - 1] != null) { ConnectGridNodes(target, nodeGrid[r, c], nodeGrid[r, c - 1]); } if (c < target.__gridColumns - 1 && nodeGrid[r, c + 1] != null) { ConnectGridNodes(target, nodeGrid[r, c], nodeGrid[r, c + 1]); } if (r > 0 && nodeGrid[r - 1, c] != null) { ConnectGridNodes(target, nodeGrid[r, c], nodeGrid[r - 1, c]); } if (r < target.__gridRows - 1 && nodeGrid[r + 1, c] != null) { ConnectGridNodes(target, nodeGrid[r, c], nodeGrid[r + 1, c]); } // diagonal edges if (target.__gridType == MBGraph.EGridType.EightWay) { if (r > 0 && c > 0 && nodeGrid[r - 1, c - 1] != null) { ConnectGridNodes(target, nodeGrid[r, c], nodeGrid[r - 1, c - 1]); } if (r < target.__gridRows - 1 && c < target.__gridColumns - 1 && nodeGrid[r + 1, c + 1] != null) { ConnectGridNodes(target, nodeGrid[r, c], nodeGrid[r + 1, c + 1]); } if (r > 0 && c < target.__gridColumns - 1 && nodeGrid[r - 1, c + 1] != null) { ConnectGridNodes(target, nodeGrid[r, c], nodeGrid[r - 1, c + 1]); } if (r < target.__gridRows - 1 && c > 0 && nodeGrid[r + 1, c - 1] != null) { ConnectGridNodes(target, nodeGrid[r, c], nodeGrid[r + 1, c - 1]); } } } } }