public static NavMeshOcTree Create(GameObject[] gameObjects, bool containChilds, float angle, int maxDepth, bool forceSetDepth) { Vector3 max = new Vector3(-Mathf.Infinity, -Mathf.Infinity, -Mathf.Infinity); Vector3 min = new Vector3(Mathf.Infinity, Mathf.Infinity, Mathf.Infinity); float maxArea = 0; List <NavMeshTriangle> triangles = new List <NavMeshTriangle>(); for (int i = 0; i < gameObjects.Length; i++) { if (!gameObjects[i].activeSelf) { continue; } FindTriangle(gameObjects[i].transform, triangles, angle, ref max, ref min, ref maxArea); if (containChilds) { FindTriangleInChild(gameObjects[i].transform, triangles, angle, ref max, ref min, ref maxArea); } } Vector3 size = max - min; if (size.x <= 0) { size.x = 0.1f; } if (size.y <= 0) { size.y = 0.1f; } if (size.z <= 0) { size.z = 0.1f; } Vector3 center = min + size * 0.5f; NavMeshOcTree ocTree = new NavMeshOcTree(center, size * 1.1f, 7); for (int i = 0; i < triangles.Count; i++) { triangles[i].SetMaxDepth(maxDepth, maxArea, forceSetDepth); ocTree.Add(triangles[i]); } return(ocTree); }
public static bool Save(NavMeshOcTree tree, string path) { try { BinaryFormatter bf = new BinaryFormatter(); FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write); bf.Serialize(fs, tree); fs.Close(); return(true); } catch (System.Exception e) { Debug.LogException(e); return(false); } }
public static NavMeshOcTree Load(string path) { try { BinaryFormatter bf = new BinaryFormatter(); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); NavMeshOcTree tree = bf.Deserialize(fs) as NavMeshOcTree; fs.Close(); return(tree); } catch (System.Exception e) { Debug.LogException(e); return(null); } }