Beispiel #1
0
    public override void OnInspectorGUI()
    {
        GUILayout.BeginVertical();
        GUILayout.Label("Path Information", EditorStyles.boldLabel);
        GUILayout.Label("Number of Nodes: " + Graph.NodeCount);
        GUILayout.Label("Root ID: " + Graph.graph[0].ID);
        GUILayout.Label("Root Position : " + Graph.graph[0].position);
        GUILayout.Label("Neighbors: ", EditorStyles.boldLabel);
        for (int j = 0; j < Graph.graph.Count; j++)
        {
            GUILayout.Label("Node #" + Graph.graph[j].ID + " at: " + Graph.graph[j].position, EditorStyles.boldLabel);
            for (int i = 0; i < Graph.graph[j].neighbors.Count; i++)
            {
                GUILayout.Label("Node " + Graph.graph[j].neighbors[i].ID + " at: " + Graph.graph[j].neighbors[i].position);
            }
        }

        if (GUILayout.Button("Add Node"))
        {
            Graph.AddNodes(1);
            Graph.GetNodeList()[Graph.NodeCount - 1].position = _target.transform.position + new Vector3(1, 0, 0);
            Graph.ConnectNodes(0, Graph.NodeCount - 1, 1);
            GameObject tempObj = new GameObject("");
            tempObj.transform.position = _target.transform.position + new Vector3(1, 0, 0);
            pathNodeContainer tempNode = tempObj.AddComponent(typeof(pathNodeContainer)) as pathNodeContainer;
            tempNode.Node = Graph.GetNodeList()[Graph.NodeCount - 1];
            tempNode.GenerateName();
            tempObj.transform.parent = _target.transform;
        }
        Graph.graph[0].position = _target.transform.position;
        GUILayout.EndVertical();
    }
    void Update()
    {
        if (CurrentDistanceNodeIDInt != DistanceNodeIDInt)
        {
            CurrentDistanceNodeIDInt = DistanceNodeIDInt;
            GetDistances();
        }
        if (CurrentPathRootNodeIDInt != PathRootNodeIDInt || CurrentPathEndNodeIDInt != PathEndNodeIDInt)
        {
            CurrentPathRootNodeIDInt = PathRootNodeIDInt;
            CurrentPathEndNodeIDInt  = PathEndNodeIDInt;
            GetPaths();
        }

        if (cam == null)
        {
            cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(typeof(Camera)) as Camera;
        }
        if (!GUIMouseOver())
        {
            GetPoint();
            if (PlaceNodes && Input.GetMouseButtonDown(0))
            {
                Nodes.Add(GameObject.CreatePrimitive(PrimitiveType.Sphere));
                Nodes[Nodes.Count - 1].transform.position = NodePoint;
                Nodes[Nodes.Count - 1].name  = (Nodes.Count - 1).ToString();
                Nodes[Nodes.Count - 1].layer = 13;
                Labels.Add(Instantiate(Textmesh) as GameObject);
                Labels[Labels.Count - 1].transform.position = (NodePoint);
                tempMesh       = Labels[Labels.Count - 1].GetComponent(typeof(TextMesh)) as TextMesh;
                tempMesh.text  = (Labels.Count - 1).ToString();
                tempMesh.color = Color.white;
                testGraph.AddNodes(1);
                GetDistances();
            }
            if (ConnectNodes)
            {
                if (Input.GetMouseButtonDown(0))
                {
                    A = IDPoint;
                }

                if (ConnectNodes && Input.GetMouseButtonUp(0))
                {
                    B = IDPoint;
                }

                if (A == B)
                {
                    A = -1;
                    B = -1;
                }
            }

            if (A >= 0 && B >= 0 && B != A)
            {
                testGraph.ConnectNodes(A, B, weightF);
                bool copy  = false;
                int  index = 0;
                foreach (GameObject obj in Lines)
                {
                    if (obj.name.Contains(A + " to " + B + " Connection") || obj.name.Contains(B + " to " + A + " Connection"))
                    {
                        copy = true;
                        break;
                    }
                    index++;
                    copy = false;
                }
                if (copy == false)
                {
                    Lines.Add(new GameObject(A + " to " + B + " Connection"));
                    Text.Add(Instantiate(Textmesh) as GameObject);
                    LineRenderer tempLine = Lines[Lines.Count - 1].AddComponent(typeof(LineRenderer)) as LineRenderer;
                    tempLine.GetComponent <Renderer>().material = LineBlack;
                    tempLine.SetWidth(0.3f, 0.3f);
                    tempLine.SetPosition(0, Nodes[A].transform.position);
                    tempLine.SetPosition(1, Nodes[B].transform.position);

                    Text[Text.Count - 1].transform.position = (Nodes[A].transform.position + Nodes[B].transform.position) / 2;

                    tempMesh       = Text[Text.Count - 1].GetComponent(typeof(TextMesh)) as TextMesh;
                    tempMesh.text  = weightF.ToString();
                    tempMesh.color = Color.red;
                    GetDistances();
                }
                else
                {
                    tempMesh      = Text[index].GetComponent(typeof(TextMesh)) as TextMesh;
                    tempMesh.text = weightF.ToString();
                    GetDistances();
                }
                GetPaths();
                Debug.Log("Connect:" + A + " to " + B);
                A = -1;
                B = -1;
            }
        }
    }
Beispiel #3
0
    public override void OnInspectorGUI()
    {
        GraphContainer = _target.transform.parent.GetComponent(typeof(SimpleAIGraphContainer)) as SimpleAIGraphContainer;

        if (_target.Node != Graph.GetNodeList()[_target.Node.ID])
        {
            _target.Node = Graph.GetNodeList()[_target.Node.ID];
        }

        GUILayout.BeginVertical();
        if (_target.Node != null)
        {
            GUILayout.Label("Node Information", EditorStyles.boldLabel);
            GUILayout.Label("ID: " + _target.Node.ID);
            GUILayout.BeginHorizontal();
            GUILayout.Label("Passthrough?: ");
            if (_target.Node.passThrough)
            {
                if (GUILayout.Button("True"))
                {
                    _target.Node.passThrough = false;
                }
            }
            else
            {
                if (GUILayout.Button("False"))
                {
                    _target.Node.passThrough = true;
                }
            }
            GUILayout.EndHorizontal();
            GUILayout.Label("Position: " + _target.Node.position);
            GUILayout.Label("Neighbors: ", EditorStyles.boldLabel);
            for (int i = 0; i < _target.Node.neighbors.Count; i++)
            {
                GUILayout.Label("Node " + _target.Node.neighbors[i].ID);
            }
        }

        _target.Node.position = _target.transform.position;

        if (GUILayout.Button("Add Node") && GraphContainer != null)
        {
            Graph.AddNodes(1);
            Graph.GetNodeList()[GraphContainer.GetGraph().NodeCount - 1].position = _target.transform.position + new Vector3(1, 0, 0);
            Graph.ConnectNodes(_target.Node.ID, Graph.NodeCount - 1, 1);
            Graph.ConnectNodes(Graph.NodeCount - 1, _target.Node.ID, 1);
            GameObject tempObj = new GameObject("");
            tempObj.transform.position = _target.transform.position + new Vector3(1, 0, 0);
            pathNodeContainer tempNode = tempObj.AddComponent(typeof(pathNodeContainer)) as pathNodeContainer;
            tempNode.Node = Graph.GetNodeList()[Graph.NodeCount - 1];
            tempNode.GenerateName();
            tempObj.transform.parent = GraphContainer.transform;
        }

        if (GUILayout.Button("Merge Closest Node") && GraphContainer != null)
        {
            int   closestID = _target.Node.ID;
            float minDist   = Mathf.Infinity;
            for (int i = 0; i < Graph.NodeCount; i++)
            {
                float dist = Vector3.Distance(Graph.GetNodeList()[i].position, _target.transform.position);
                if (dist < minDist && Graph.GetNodeList()[i].ID != _target.Node.ID)
                {
                    minDist   = dist;
                    closestID = Graph.GetNodeList()[i].ID;
                }
            }
            if (closestID != _target.Node.ID)
            {
                if (EditorUtility.DisplayDialog("Merge nodes?", "The closest node is node " + closestID + ", would you like to merge these nodes together?", "Merge", "Cancel"))
                {
                    GraphContainer.MergeNodes(_target.Node.ID, closestID);
                    destroyMe = true;
                }
            }
        }

        if (GUILayout.Button("Delete Node") && GraphContainer != null)
        {
            if (EditorUtility.DisplayDialog("Delete Selected Node?", "Are you sure you want to delete node " + _target.Node.ID + "?", "Delete", "Cancel"))
            {
                GraphContainer.DeleteNode(_target.Node.ID);
                destroyMe = true;
            }
        }
        GUILayout.EndVertical();
        if (destroyMe)
        {
            DestroyImmediate(_target.gameObject);
        }
    }