示例#1
0
        void GenerateTiles()
        {
            if (mapType == 0)                            //Dungeon type map. Now wall tile prefabs must be modified such that sprites are changed.
            {
                Texture2D tex = new Texture2D(256, 256); // 1. Create spritesheet
                int       dim = 16;
                for (int i = 0; i < 8; i++)              //gen wall tiles
                {
                    Color col           = new Color();
                    Color col2          = new Color();
                    Color col3          = new Color();
                    Color dcol2         = new Color();
                    Color dcol3         = new Color();
                    float wallRCol      = Random.Range(0.3f, 0.3f);
                    float wallGCol      = Random.Range(0.3f, 0.3f);
                    float wallBCol      = Random.Range(0.3f, 0.3f);
                    float wallRCol2     = wallRCol - 0.02f;
                    float wallGCol2     = wallGCol - 0.02f;
                    float wallBCol2     = wallBCol - 0.02f;
                    float wallRCol3     = wallRCol - 0.04f;
                    float wallGCol3     = wallGCol - 0.04f;
                    float wallBCol3     = wallBCol - 0.04f;
                    float destWallRCol2 = wallRCol - 0.04f;
                    float destWallGCol2 = wallGCol - 0.04f;
                    float destWallBCol2 = wallBCol - 0.04f;
                    float destWallRCol3 = wallRCol - 0.08f;
                    float destWallGCol3 = wallGCol - 0.08f;
                    float destWallBCol3 = wallBCol - 0.08f;
                    col.r   = wallRCol;
                    col.g   = wallGCol;
                    col.b   = wallBCol;
                    col.a   = 1.0f;
                    col2.r  = wallRCol2;
                    col2.g  = wallGCol2;
                    col2.b  = wallBCol2;
                    col2.a  = 1.0f;
                    col3.r  = wallRCol3;
                    col3.g  = wallGCol3;
                    col3.b  = wallBCol3;
                    col3.a  = 1.0f;
                    dcol2.r = destWallRCol2;
                    dcol2.g = destWallGCol2;
                    dcol2.b = destWallBCol2;
                    dcol2.a = 1.0f;
                    dcol3.r = destWallRCol3;
                    dcol3.g = destWallGCol3;
                    dcol3.b = destWallBCol3;
                    dcol3.a = 1.0f;
                    for (int k = (i * dim); k < (dim + (i * dim)); k++)
                    {
                        for (int j = 0; j < 16; j++)                     //Create 8 16x16 pixel sprites of wall type

                        {
                            float random = Random.Range(0, 3);
                            if (j + 2 < dim)
                            {
                                if (random == 1)
                                {
                                    tex.SetPixel(j, k, col);                                      //first layer
                                    tex.SetPixel(j + 16, k, col);                                 //second layer
                                    tex.SetPixel(++j, k, col);                                    //first layer
                                    tex.SetPixel(j + 16, k, col);                                 //second layer
                                }
                                else if (random == 2)
                                {
                                    tex.SetPixel(j, k, col2);
                                    tex.SetPixel(j + 16, k, dcol2);
                                    tex.SetPixel(++j, k, col2);
                                    tex.SetPixel(j + 16, k, dcol2);
                                }
                                else
                                {
                                    tex.SetPixel(j, k, col3);
                                    tex.SetPixel(j + 16, k, dcol3);
                                    tex.SetPixel(++j, k, col3);
                                    tex.SetPixel(j + 16, k, dcol3);
                                }
                            }
                            else
                            {
                                if (random == 1)
                                {
                                    tex.SetPixel(j, k, col);
                                    tex.SetPixel(j + 16, k, col);
                                }
                                else if (random == 2)
                                {
                                    tex.SetPixel(j, k, col2);
                                    tex.SetPixel(j + 16, k, dcol2);
                                }
                                else
                                {
                                    tex.SetPixel(j, k, col3);
                                    tex.SetPixel(j + 16, k, dcol3);
                                }
                            }
                        }
                    }
                }
                for (int i = 0; i < 8; i++)                   // gen floor tiles
                {
                    Color col       = new Color();
                    Color col2      = new Color();
                    Color col3      = new Color();
                    float wallRCol  = Random.Range(0.5f, 0.5f);
                    float wallGCol  = Random.Range(0.5f, 0.5f);
                    float wallBCol  = Random.Range(0.5f, 0.5f);
                    float wallRCol2 = wallRCol - 0.02f;
                    float wallGCol2 = wallGCol - 0.02f;
                    float wallBCol2 = wallBCol - 0.02f;
                    float wallRCol3 = wallRCol - 0.04f;
                    float wallGCol3 = wallGCol - 0.04f;
                    float wallBCol3 = wallBCol - 0.04f;
                    col.r  = wallRCol;
                    col.g  = wallGCol;
                    col.b  = wallBCol;
                    col.a  = 1.0f;
                    col2.r = wallRCol2;
                    col2.g = wallGCol2;
                    col2.b = wallBCol2;
                    col2.a = 1.0f;
                    col3.r = wallRCol3;
                    col3.g = wallGCol3;
                    col3.b = wallBCol3;
                    col3.a = 1.0f;

                    for (int k = (i * dim); k < (dim + (i * dim)); k++)
                    {
                        for (int j = 32; j < 48; j++)                     //Create 8 16x16 pixel sprites of floor type
                        {
                            float random  = Random.Range(0, 3);
                            float random2 = Random.Range(0, 2);
                            if (j + 2 < 48 && random2 == 0)
                            {
                                if (random == 1)
                                {
                                    tex.SetPixel(j, k, col);
                                    tex.SetPixel(++j, k, col);
                                }
                                else if (random == 2)
                                {
                                    tex.SetPixel(j, k, col2);
                                    tex.SetPixel(++j, k, col2);
                                }
                                else
                                {
                                    tex.SetPixel(j, k, col3);
                                    tex.SetPixel(++j, k, col3);
                                }
                            }
                            else
                            {
                                if (random == 1)
                                {
                                    tex.SetPixel(j, k, col);
                                }
                                else if (random == 2)
                                {
                                    tex.SetPixel(j, k, col2);
                                }
                                else
                                {
                                    tex.SetPixel(j, k, col3);
                                }
                            }
                        }
                    }
                }
                for (int i = 0; i < 8; i++)                   //gen outer wall tiles
                {
                    Color col       = new Color();
                    Color col2      = new Color();
                    Color col3      = new Color();
                    float wallRCol  = Random.Range(0.25f, 0.25f);
                    float wallGCol  = Random.Range(0.25f, 0.25f);
                    float wallBCol  = Random.Range(0.25f, 0.25f);
                    float wallRCol2 = wallRCol - 0.04f;
                    float wallGCol2 = wallGCol - 0.04f;
                    float wallBCol2 = wallBCol - 0.04f;
                    float wallRCol3 = wallRCol - 0.08f;
                    float wallGCol3 = wallGCol - 0.08f;
                    float wallBCol3 = wallBCol - 0.08f;
                    col.r  = wallRCol;
                    col.g  = wallGCol;
                    col.b  = wallBCol;
                    col.a  = 1.0f;
                    col2.r = wallRCol2;
                    col2.g = wallGCol2;
                    col2.b = wallBCol2;
                    col2.a = 1.0f;
                    col3.r = wallRCol3;
                    col3.g = wallGCol3;
                    col3.b = wallBCol3;
                    col3.a = 1.0f;
                    for (int k = (i * dim); k < (dim + (i * dim)); k++)
                    {
                        for (int j = 48; j < 64; j++)                     //Create 8 16x16 pixel sprites of floor type

                        {
                            float random  = Random.Range(0, 3);
                            float random2 = Random.Range(0, 2);
                            if (j + 2 < 64 && random2 == 0)
                            {
                                if (random == 1)
                                {
                                    tex.SetPixel(j, k, col);
                                    tex.SetPixel(++j, k, col);
                                }
                                else if (random == 2)
                                {
                                    tex.SetPixel(j, k, col2);
                                    tex.SetPixel(++j, k, col2);
                                }
                                else
                                {
                                    tex.SetPixel(j, k, col3);
                                    tex.SetPixel(++j, k, col3);
                                }
                            }
                            else
                            {
                                if (random == 1)
                                {
                                    tex.SetPixel(j, k, col);
                                }
                                else if (random == 2)
                                {
                                    tex.SetPixel(j, k, col2);
                                }
                                else
                                {
                                    tex.SetPixel(j, k, col3);
                                }
                            }
                        }
                    }
                }
                if (!Directory.Exists(Application.dataPath + "/Resources/"))
                {
                    Directory.CreateDirectory(Application.dataPath + "/Resources/");
                }
                byte[] bytes = tex.EncodeToPNG();
                DestroyImmediate(tex);
                File.WriteAllBytes(Application.dataPath + "/Resources/DungeonSprites.png", bytes);

                AssetDatabase.ImportAsset("Assets/Resources/DungeonSprites.png", ImportAssetOptions.ForceUpdate);
                AssetDatabase.Refresh();
                Texture2D       myTexture = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Resources/DungeonSprites.png", typeof(Texture2D));
                string          path      = AssetDatabase.GetAssetPath(myTexture);
                TextureImporter ti        = AssetImporter.GetAtPath(path) as TextureImporter;
                ti.textureType         = TextureImporterType.Sprite;
                ti.spritePixelsPerUnit = 16;
                ti.spriteImportMode    = SpriteImportMode.Multiple;
                ti.mipmapEnabled       = false;
                ti.filterMode          = FilterMode.Point;
                ti.isReadable          = true;
                List <SpriteMetaData> newData = new List <SpriteMetaData>();

                int SliceWidth  = 16;
                int SliceHeight = 16;
                for (int i = 0; i < myTexture.width; i += SliceWidth)
                {
                    for (int j = myTexture.height; j > 0; j -= SliceHeight)
                    {
                        SpriteMetaData smd = new SpriteMetaData();
                        smd.pivot     = new Vector2(0.5f, 0.5f);
                        smd.alignment = 0;
                        smd.name      = (myTexture.height - j) / SliceHeight + ", " + i / SliceWidth;
                        smd.rect      = new Rect(i, j - SliceHeight, SliceWidth, SliceHeight);
                        newData.Add(smd);
                    }
                }
                ti             = AssetImporter.GetAtPath(path) as TextureImporter;
                ti.spritesheet = newData.ToArray();
                AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
                Object[] s = Resources.LoadAll("DungeonSprites");

                for (int i = 0; i < 8; i++)
                {
                    GameObject     w        = wallTiles [i];
                    SpriteRenderer sr       = w.GetComponent <SpriteRenderer> ();
                    int            rwsprite = Random.Range(0, 7);
                    sr.sprite = (Sprite)s [129 + 16 * rwsprite];
                    Wall dest = w.GetComponent <Wall> ();
                    dest.dmgSprite = (Sprite)s [130 + 16 * rwsprite];
                    GameObject f = floorTiles [i];
                    sr        = f.GetComponent <SpriteRenderer> ();
                    sr.sprite = (Sprite)s[131 + 16 * Random.Range(0, 7)];
                    GameObject ow = outerWallTiles [i];
                    sr        = ow.GetComponent <SpriteRenderer> ();
                    sr.sprite = (Sprite)s[132 + 16 * Random.Range(0, 7)];
                }
            }
        }