示例#1
0
    void AddToCells()
    {
        GetComponent <BuildControl>().cells.Clear();

        for (int i = 0; i < cells.Count; i++)
        {
            // meshRenderer.sharedMaterial = Resources.Load("White") as Material;

            if (makeSkyscraperTraditional)
            {
                cells[i].AddComponent <TraditionalSkyscraper>();

                //add to build control list
                GetComponent <BuildControl>().cells.Add(cells[i]);
                //  cell.GetComponent<MeshRenderer>().enabled = false;
            }
        }

//        Debug.Log("cells count before extrude loop = " + cells.Count);
        //now we have found adjacents, we can scale cells
        for (int i = 0; i < cells.Count; i++)
        { //set layer here
            cells[i].layer = LayerMask.NameToLayer("Cells");
            if (extrudeCells)
            {
                ExtrudeCell ex = cells[i].AddComponent <ExtrudeCell>();
                ex.uniqueVertices = true;
                //call start straight away
                ex.Start();

                //also extrude any cells that were used to merge cells(we use them to compare points later)
                if (cells[i].GetComponent <MergeCell>() != null)
                {
                    MergeCell mergeCell = cells[i].GetComponent <MergeCell>();
                    for (int j = 0; j < mergeCell.previousCells.Count; j++)
                    {
                        mergeCell.previousCells[j].SetActive(true);
                        ExtrudeCell extrudeCell = mergeCell.previousCells[j].AddComponent <ExtrudeCell>();
                        extrudeCell.onlyScale = true;
                        extrudeCell.Start();
                        mergeCell.previousCells[j].SetActive(false);
                    }
                }
            }
        }


        if (doBuildControl)
        {
            GetComponent <BuildControl>().enabled = true;
        }

        // yield break;
    }
    void AddToCells()
    {
        for (int i = 0; i < meshVerts.Count; i++)
        {
            //create a game object for each cell in the mesh list
            GameObject cell = new GameObject();
            cell.transform.parent = this.gameObject.transform;
            cell.name             = "Cell";
            // cell.tag = "Cell";


            //create a mesh from the already populated lists
            Mesh mesh = new Mesh();
            mesh.vertices  = meshVerts[i];
            mesh.triangles = meshTris[i];

            MeshFilter meshFilter = cell.AddComponent <MeshFilter>();
            meshFilter.mesh = mesh;


            MeshRenderer meshRenderer = cell.AddComponent <MeshRenderer>();


            meshRenderer.sharedMaterial = Resources.Load("White") as Material;

            if (makeSkyscraper)
            {
                cell.AddComponent <SkyscraperFromVoronoiCell>();

                //add to build control list
                GetComponent <BuildControl>().cells.Add(cell);
                //  cell.GetComponent<MeshRenderer>().enabled = false;
            }


            //bottleneck protection, build a 100 at a time
            //  if (i != 0 && i % 100 == 0)
            //       yield return new WaitForEndOfFrame();


            //master list of cells
            cells.Add(cell);
        }

        //work out which cells are adjacent to each cell, save in a list
        for (int i = 0; i < cells.Count; i++)
        {
            //set layer here
            cells[i].layer = LayerMask.NameToLayer("Cells");

            List <GameObject> adjacents = new List <GameObject>();

            Vector3[] thisVertices = cells[i].GetComponent <MeshFilter>().mesh.vertices;
            for (int j = 0; j < cells.Count; j++)
            {
                //don't check own cell
                if (i == j)
                {
                    continue;
                }

                Vector3[] otherVertices = cells[j].GetComponent <MeshFilter>().mesh.vertices;

                for (int a = 0; a < thisVertices.Length; a++)
                {
                    for (int b = 0; b < otherVertices.Length; b++)
                    {
                        //if we have a match, add "other" cell to a list of adjacents for this cell
                        if (thisVertices[a] == otherVertices[b])
                        {
                            adjacents.Add(cells[j]);

                            //force out of the loops
                            a = thisVertices.Length;
                            break;
                        }
                    }
                }
            }

            //adjacentCells.Add(adjacents); //removing
            //add to list and save it on game object. Doing it this way allows us to hot reload, if we save it all in a list here, it won't serialize
            // AdjacentCells aj = cells[i].AddComponent<AdjacentCells>();
            // aj.adjacentCells = adjacents;
        }


        //now we have found adjacents, we can scale cells
        for (int i = 0; i < cells.Count; i++)
        {
            if (extrudeCells)
            {
                ExtrudeCell ex = cells[i].AddComponent <ExtrudeCell>();
                ex.uniqueVertices = true;
                //call start straight away
                ex.Start();
            }
        }


        if (doBuildControl)
        {
            GetComponent <BuildControl>().enabled = true;
        }

        // yield break;
    }