Improving Performance of Map Loading

My project will load map chunks from the resources, and only load the map chunks nearby. This works perfectly.
The issue is right now I’m adding pathfinding to my project and I’m using raycasting to determine if an area is blocked by an object and is walkable.

This cycles through the entire map and sends out raycasts to determine unwalkable areas, and the Y points of each height level’s floor.

private ArrayList clipMap = new ArrayList ();
private ArrayList heightMap = new ArrayList ();
private int squareCount = (int)(Config.MAP_SIZE / Config.CLIP_MAP_SQUARE_SIZE);
private	Color[] colors = new Color[] {Color.red, Color.blue, Color.green, Color.yellow, Color.cyan};

public void BuildClipMap (float baseX, float baseZ, int HighestFloor)
{
	for (int i = 0; i < HighestFloor; i++) {			
		int layerMask = (1 << Config.OBJECT_MAP_LAYER + i);
		for (int x2 = 0; x2 < squareCount; x2+= 1) {
			for (int z2 = 0; z2 < squareCount; z2+= 1) {
				float x = x2 * Config.CLIP_MAP_SQUARE_SIZE;
				float z = z2 * Config.CLIP_MAP_SQUARE_SIZE;
				RaycastHit hit = new RaycastHit ();
				if (Physics.Linecast (new Vector3 (x + baseX + (Config.CLIP_MAP_SQUARE_SIZE / 2), 0, z + baseZ + (Config.CLIP_MAP_SQUARE_SIZE / 2)), 
					new Vector3 (x + baseX + (Config.CLIP_MAP_SQUARE_SIZE / 2), Config.MAP_HEIGHT + 100, z + baseZ + (Config.CLIP_MAP_SQUARE_SIZE / 2)), out hit, layerMask)) {
					if (hit.collider != null) {
						clipMap.Add (new ClipMapEntry (i, Mathf.FloorToInt (x / Config.CLIP_MAP_SQUARE_SIZE), Mathf.FloorToInt (z / Config.CLIP_MAP_SQUARE_SIZE)));
						Debug.DrawLine (new Vector3 (x + baseX + (Config.CLIP_MAP_SQUARE_SIZE / 2), hit.point.y, z + baseZ + (Config.CLIP_MAP_SQUARE_SIZE / 2)), new Vector3 (x + baseX + (Config.CLIP_MAP_SQUARE_SIZE / 2), hit.collider.bounds.size.y + hit.point.y, z + baseZ + (Config.CLIP_MAP_SQUARE_SIZE / 2)), colors.Length > i ? colors  *: Color.gray, 120.0f);*
  •   				}*
    
  •   			}					*
    
  •   		}*
    
  •   	}*
    
  •   }*
    
  • }*

  • public void BuildHeightMap (float baseX, float baseZ, int HighestFloor)*

  • {*

  •   for (int i = 0; i < HighestFloor; i++) {*
    
  •   	int layerMask = (1 << Config.FLOOR_MAP_LAYER + i);*
    
  •   	for (int x2 = 0; x2 < squareCount; x2+= 1) {*
    
  •   		for (int z2 = 0; z2 < squareCount; z2+= 1) {*
    

float x = x2 * Config.CLIP_MAP_SQUARE_SIZE;
float z = z2 * Config.CLIP_MAP_SQUARE_SIZE;

  •   			RaycastHit hit2 = new RaycastHit ();*
    
  •   			if (Physics.Linecast (new Vector3 (x + baseX + (Config.CLIP_MAP_SQUARE_SIZE / 2), Config.MAP_HEIGHT + 100, z + baseZ + (Config.CLIP_MAP_SQUARE_SIZE / 2)),* 
    
  •   				new Vector3 (x + baseX + (Config.CLIP_MAP_SQUARE_SIZE / 2), 0, z + baseZ + (Config.CLIP_MAP_SQUARE_SIZE / 2)), out hit2, layerMask)) {*
    
  •   				if (hit2.collider != null) {*
    
  •   					heightMap.Add (new HeightMapEntry (i, Mathf.FloorToInt (x / Config.CLIP_MAP_SQUARE_SIZE), Mathf.FloorToInt (z / Config.CLIP_MAP_SQUARE_SIZE), hit2.point.y));*
    
  •   					Debug.DrawLine (new Vector3 (x + baseX + (Config.CLIP_MAP_SQUARE_SIZE / 2), hit2.point.y, z + baseZ + (Config.CLIP_MAP_SQUARE_SIZE / 2)), new Vector3 (x + baseX + (Config.CLIP_MAP_SQUARE_SIZE / 2), hit2.point.y + 0.15f, z + baseZ + (Config.CLIP_MAP_SQUARE_SIZE / 2)), Color.white, 120.0f);*
    
  •   				}*
    
  •   			}*
    
  •   		}*
    
  •   	}*
    
  •   }*
    
  • }*
    I can set the CLIP_MAP_SQUARE_SIZE to be bigger or smaller (bigger = faster, but much less exact). I want it to be 0.5, but calling this method on each map chunk (250x250) takes about 2000MS at 0.5, and 500MS at 1.0.
    I need to reduce that time by a lot, and have 0.5 load at around 200MS Max. Does anyone have any ideas? I’d really appreciate it.
    This is what these methods do (visually with debug):
    ![alt text][1]
    [1]: http://img832.imageshack.us/img832/8194/m8y0.png

After optimizing this like crazy by using Lists instead of ArrayLists, and also pre-computing the data the speed was relatively the same.

But, due to precomputing the data, loading the data used none of Unity’s functions and could be run using a seperate thread. I started using seperate threads per map chunk, and this caused my entire computer to lag as it was using my entire processor. To fix this I isolated it to just one seperate thread, and it’s running flawlessly now at 9 Milliseconds per map chunk, instead of 500 Milliseconds per map chunk earlier.

Thanks to all who helped me!!

I wrote my own secondary thread handler if anybody wishes to use it:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

public class SecondaryThread {
	
	public delegate void Action();
	private static Thread secondThread;
	private static bool stopThread;
	private static List<Action> functionList = new List<Action>();

	public static void addTask(Action method) {
			functionList.Add (method);
			if(secondThread == null) {
			 	secondThread = new Thread(new ThreadStart(loop));
				secondThread.Start ();
			}
	}
	
	public static void close() {
		if(secondThread == null)
			return;
		stopThread = true;
		secondThread.Interrupt();	
		secondThread.Abort();
	}
	
	private static void loop() {
		while (stopThread == false) {
				for(int i = 0; i < functionList.Count; i++) {
					Action entry = (Action) functionList*;*
  •  			entry();*
    
  •  			functionList.RemoveAt(i);*
    
  •  		}*
    
  •  }*
    
  • } *
    }
    Use it like this:
    private void methodName() {
    doStuff();
    }

SecondaryThread.addTask(methodName);