/// <summary> /// /// </summary> private void InitEdgeObjects() { _edges = new EdgeObject[_graph.EdgeCount]; for (int i = 0; i < _graph.EdgeCount; i++) { var e = _graph.GetEdge(i); var p0 = _vertices[e.Start].transform.position; var p1 = _vertices[e.End].transform.position; var eObj = Instantiate(_edgeObject, transform); eObj.Index = i; var xform = eObj.transform; var dir = p1 - p0; var mag = dir.magnitude; // scale to edge length xform.localScale = new Vector3(0.1f, mag * 0.5f, 0.1f); // translate to edge mid point xform.localPosition = (p0 + p1) * 0.5f; //Vector3.Lerp(p0, p1, 0.5f); // align up with edge direction xform.localRotation = Quaternion.FromToRotation(xform.up, dir); // cache it _edges[i] = eObj; } }
/// <summary> /// /// </summary> private void Awake() { _graph = EdgeGraph.Factory.CreateTriangleGrid(_countX, _countY); // initialize game objects InitVertexObjects(); InitEdgeObjects(); // initialize additonal attributes _vertexDistances = new float[_graph.VertexCount]; _edgeLengths = new float[_graph.EdgeCount]; _edgeTraffic = new int[_graph.EdgeCount]; // initialize edge lengths for (int i = 0; i < _graph.EdgeCount; i++) { var e = _graph.GetEdge(i); var p0 = _vertices[e.Start].transform.localPosition; var p1 = _vertices[e.End].transform.localPosition; _edgeLengths[i] = Vector3.Distance(p0, p1) + UnityEngine.Random.Range(0, 0.001f); } }