/* * loadNavigationMeshFromFile method will load all the data for the navigation mesh from the file * Paramters: none * Return: none */ public void loadNavigationMeshFromFile() { startTime = Time.realtimeSinceStartup; string filePath = Application.loadedLevelName + "NavigationMesh.dat"; Stream TestFileStream = File.OpenRead(filePath); BinaryFormatter deserializer = new BinaryFormatter(); positionToStart = 0; AIPolygonHolderData dataholder = (AIPolygonHolderData)deserializer.Deserialize(TestFileStream); AIPolygon[] polygons = new AIPolygon[dataholder.polygonData.Length]; for (int count = 0; count < dataholder.polygonData.Length; count++) { Vector3[] vertices = new Vector3[dataholder.polygonData [count].vertices.Length / 3]; int id = dataholder.polygonData [count].id; int[] neighbors = dataholder.polygonData [count].neighbors; int neighborsHeld = dataholder.polygonData [count].neighborCount; bool gotGoal = dataholder.polygonData [count].gotGoal; bool gotAgent = dataholder.polygonData [count].gotAgent; int agentID = dataholder.polygonData[count].agentID; for (int count2 = 0; count2 < vertices.Length; count2++) { vertices [count2] = new Vector3(dataholder.polygonData [count].vertices [count2, 0], dataholder.polygonData [count].vertices [count2, 1], dataholder.polygonData [count].vertices [count2, 2]); } polygons [count] = new AIPolygon(); polygons [count].setData(vertices, id, neighborsHeld, neighbors, gotGoal, gotAgent, agentID); } AINavigationMeshData.initialPolygonCount = getSurfacePolygonCount(); AINavigationMeshData.obstacleCount = staticObjects.Length; surfacePolygonHolders = new AIPolygonHolder[1]; surfacePolygonHolders [0] = new AIPolygonHolder(); surfacePolygonHolders [0].setAll(polygons, polygons.Length); positionToStart = 0; AINavigationMeshData.timeTaken = Time.realtimeSinceStartup - startTime; AINavigationMeshData.finalPolygonCount = getSurfacePolygonCount(); TestFileStream.Close(); writeNavigationMeshToFile(); }
/* * writeNavigationMeshToFile method will serialize the data for a navigation mesh and write it all do a file * Parameters: none * Return: none */ void writeNavigationMeshToFile() { string tempstring = ""; int agents = 1 + GameObject.FindGameObjectsWithTag("Agent").Length; int hasAgentCount = 1; AIPolygon[] polygons = surfacePolygonHolders [positionToStart].getPolygons(); AIPolygonHolderData dataHolder = new AIPolygonHolderData(); dataHolder.polygonData = new AIPolygonData[polygons.Length]; tempstring += goalPosition.x + "\n" + goalPosition.z + "\n"; tempstring += agents + "\n"; tempstring += agentStart.x + "\n" + agentStart.z + "\n"; tempstring += surfacePolygonHolders [positionToStart].getMinPolygonLength() + "\n"; tempstring += polygons.Length + "\n"; for (int count = 0; count < polygons.Length; count++) { float[,] vertices = new float[polygons [count].getVerticesCount(), 3]; for (int count2 = 0; count2 < polygons[count].getVerticesCount(); count2++) { Vector3 vertex = polygons [count].getVectorAtIndex(count2); vertices [count2, 0] = vertex.x; vertices [count2, 1] = vertex.y; vertices [count2, 2] = vertex.z; } tempstring += polygons[count].getID() + "\n"; if (polygons[count].getHasGoal() == true) { tempstring += "1\n"; } else { tempstring += "0\n"; } if (polygons[count].getHasAgent() == true) { tempstring += polygons[count].getAgentID() + "\n"; hasAgentCount++; } else { tempstring += "0\n"; } tempstring += polygons[count].getCenterVector().x + " " + polygons[count].getCenterVector().z + "\n"; tempstring += polygons[count].getNeighborsHeld() + "\n"; for (int count2 = 0; count2 < polygons[count].getNeighborsHeld(); count2++) { tempstring += polygons[count].getNeighborAt(count2) + "\n"; } dataHolder.polygonData [count] = new AIPolygonData(); dataHolder.polygonData [count].vertices = vertices; dataHolder.polygonData [count].neighborCount = polygons [count].getNeighborsHeld(); dataHolder.polygonData [count].neighbors = polygons [count].getNeighbors(); dataHolder.polygonData [count].gotGoal = polygons [count].getHasGoal(); dataHolder.polygonData [count].gotAgent = polygons [count].getHasAgent(); dataHolder.polygonData[count].agentID = polygons[count].getAgentID(); dataHolder.polygonData [count].id = polygons [count].getID(); } Debug.Log(tempstring); string filePath = Application.loadedLevelName + "NavigationMesh.dat"; Stream writer = File.Create(filePath); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(writer, dataHolder); writer.Close(); StreamWriter writer1 = new StreamWriter(Application.loadedLevelName + "Polygons.txt"); writer1.Write(tempstring); Debug.Log(tempstring); writer1.Close(); }