AutoTile 自动拼接(六 大结局) 学习与实践

昨天在网上找了一些资源图片,这回就不用担心 背景资源不多的问题了,现在我一边 制作,一边发布文章。 

 

 

各种各样,500多个,这里还是特别感谢 ,万恶的资本主义,不设密码就给我分享。

 

在制作前,大家看下这里的图片,和我之前的图片规格,有点不一样,他们大了一号。

细心的朋友,可以拿这张 ,和我 之前的图比较下。

哎,为了这500多张的 图,我也只能稍微改改 切图了,但做法都是一样的。

里面有一些 动画图,需要切一下,我推荐

这个软件,360会认为木马,其实没什么事情的,看你自己了。

 

很方便吧,其实 不用切,改下程序也可以,不过我懒。。。。。。

u3d中也是按16*16格子切,当然最后就有48张图了。

 

好了,前期准备先说到这里,下面 开始 编写代码

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using LitJson;
using System;

public class myDown : MonoBehaviour
{
    public Transform trs;
    public Transform go;
    int _x, _y;
    void Start()
    {
        _x = go.GetComponent<ResourcesSprite>().Widths - 1;
        _y = go.GetComponent<ResourcesSprite>().Heights - 1;
    }

    void Update()
    {

    }

    void OnFingerDown(FingerDownEvent e)
    { /* your code here */
        RaycastHitGet(e.Position);
    }
    void OnDrag(DragGesture gesture)
    {
         DragGesture dragGesture = (gesture.State == GestureRecognitionState.Ended) ? null : gesture;
         if (dragGesture)
         {
             RaycastHitGet(gesture.Position);
         }
    }
    void RaycastHitGet(Vector3 e)
    {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(e), Vector2.zero);
        if (hit)
        {
            if (hit.collider.tag == "sence")
            {
                int type = 0;

                for (int i = 0; i < trs.childCount; i++)
                {
                    if (trs.GetChild(i).GetComponent<Toggle>().isOn)
                    {
                        type = i;
                    }
                }
                switch (type)
                {
                    case 0: OnOneFourthFind(hit.collider.transform, "play0");
                        break;
                    case 1: OnOneFourthFind(hit.collider.transform, "play1");
                        break;
                    case 2: OnOneFourthFind(hit.collider.transform, "play2");
                        break;
                    case 3: OnOneFourthFind(hit.collider.transform, "play3");
                        break;
                    case 4: OnOneFourthFind(hit.collider.transform, "play4");
                        break;
                    case 5: OnOneFourthFind(hit.collider.transform, "play5");
                        break;
                    case 6: OnOneFourthFind(hit.collider.transform, "play6");
                        break;
                    case 7: OnOneFourthFind(hit.collider.transform, "play7");
                        break;
                    case 8: OnOneFourthFind(hit.collider.transform, "play8");
                        break;
                    case 9: OnOneFourthFind(hit.collider.transform, "play9");
                        break;
                    case 10: OnOneFourthFind(hit.collider.transform, "play10");
                        break;
                    case 11: OnOneFourthFind(hit.collider.transform, "play11");
                        break;
                    case 12: OnOneFourthFind(hit.collider.transform, "play12");
                        break;
                    case 13: OnOneFourthFind(hit.collider.transform, "play13");
                        break;
                    case 14: OnOneFourthFind(hit.collider.transform, "play14");
                        break;
                    case 15: OnOneFourthFind(hit.collider.transform, "play15");
                        break;
                    case 16: OnOneFourthFind(hit.collider.transform, "play16");
                        break;
                    case 17: OnOneFourthFind(hit.collider.transform, "play17");
                        break;
                    case 18: OnOneFourthFind(hit.collider.transform, "play18");
                        break;
                    case 19: OnOneFourthFind(hit.collider.transform, "play19");
                        break;
                    case 20: OnOneFourthFind(hit.collider.transform, "play20");
                        break;
                    case 21: OnOneFourthFind(hit.collider.transform, "play21");
                        break;
                    case 22: OnOneFourthFind(hit.collider.transform, "play22");
                        break;
                    case 23: OnOneFourthFind(hit.collider.transform, "play23");
                        break;
                    case 24: OnOneFourthFind(hit.collider.transform, "play24");
                        break;
                    case 25: OnOneFourthFind(hit.collider.transform, "play25");
                        break;
                    case 26: OnOneFourthFind(hit.collider.transform, "play26");
                        break;
                    case 27: OnOneFourthFind(hit.collider.transform, "play27");
                        break;
                    case 28: OnOneFourthFind(hit.collider.transform, "play28");
                        break;
                    case 29: OnOneFourthFind(hit.collider.transform, "play29");
                        break;
                    case 30: OnOneFourthFind(hit.collider.transform, "play30");
                        break;
                    case 31: OnOneFourthFind(hit.collider.transform, "play31");
                        break;
                    case 32: OnOneFourthFind(hit.collider.transform, "play32");
                        break;


                    default: break;
                }
            }
        }
    }



    //一对四 查找坐标
    void OnOneFourthFind(Transform tr, string keys)
    {
        int[] st = new int[4];
        st[0] = 12;
        st[1] = 17;
        st[2] = 42;
        st[3] = 47;
        for (int i = 0; i < 4; i++)
        {
            AutoTileShow autoTileShow1 = tr.GetChild(i).GetComponent<AutoTileShow>();
            autoTileShow1.keys = keys;
            autoTileShow1.Weight = st[i];
        }
        tr.GetComponent<keyRoot>().keys = keys;
        string[] bit = OnWeightRange(tr);
        ArrayList arr = OnWeightRangeTr(bit, tr);
        arr.Add(tr);
        for (int ii = 0; ii < arr.Count; ii++)
        {
            Transform aa = arr[ii] as Transform;
            OnWeightNumberFind(aa, aa.GetComponent<keyRoot>().keys);
        }
    }
    //八方向对象获得
    ArrayList OnWeightRangeTr(string[] bit, Transform tts)
    {
        uint[] bbt = new uint[8];
        ArrayList arr = new ArrayList();
        for (int i = 0; i < 8; i++)
        {
            if (bit[i] != "0")
            {
                arr.Add(tts.parent.Find(bit[i]));
            }
        }
        return arr;
    }

    //八方向范围获得
    string[] OnWeightRange(Transform tts)
    {
        string[] tt = tts.name.Split(',');
        int xx = int.Parse(tt[0].ToString());
        int yy = int.Parse(tt[1].ToString());
        string[] bit = new string[8];

        bit[0] = (xx - 1) + "," + (yy + 1);
        bit[1] = xx + "," + (yy + 1);
        bit[2] = (xx + 1) + "," + (yy + 1);
        bit[3] = (xx + 1) + "," + (yy);
        bit[4] = (xx + 1) + "," + (yy - 1);
        bit[5] = xx + "," + (yy - 1);
        bit[6] = (xx - 1) + "," + (yy - 1);
        bit[7] = (xx - 1) + "," + yy;

        if (xx == 0)
        {
            bit[0] = "0";
            bit[6] = "0";
            bit[7] = "0";
        }
        if (xx == _x)
        {
            bit[2] = "0";
            bit[3] = "0";
            bit[4] = "0";
        }
        if (yy == 0)
        {
            bit[4] = "0";
            bit[5] = "0";
            bit[6] = "0";
        }
        if (yy == _y)
        {
            bit[0] = "0";
            bit[1] = "0";
            bit[2] = "0";
        }
        return bit;
    }
    //权值检索
    void OnWeightNumberFind(Transform tts, string keys)
    {
        string[] bit = OnWeightRange(tts);
        uint[] bbt = new uint[8];
        for (int i = 0; i < 8; i++)
        {
            if (bit[i] != "0")
            {
                if (tts.parent.Find(bit[i]).GetComponent<keyRoot>().keys == keys)
                {
                    bbt[i] = OnConvert(i);
                }
                else
                {
                    bbt[i] = 0;
                }
            }
            else
            {
                bbt[i] = 0;
            }
        }

        uint tb = 0;
        for (int i = 0; i < 8; i++)
        {
            tb |= bbt[i];
        }
        for (int k = 0; k < 4; k++)
        {
            tts.GetChild(k).GetComponent<AutoTileShow>().Weight = (int)(GameModel.getInstance().weightSprite[tb][k]);
            tts.GetChild(k).GetComponent<AutoTileShow>().open = true;
        }
    }
    //转换
    uint OnConvert(int i)
    {
        uint ty = 0;
        switch (i)
        {
            case 0: ty = 1;
                break;
            case 1: ty = 2;
                break;
            case 2: ty = 4;
                break;
            case 3: ty = 8;
                break;
            case 4: ty = 16;
                break;
            case 5: ty = 32;
                break;
            case 6: ty = 64;
                break;
            case 7: ty = 128;
                break;
        }
        return ty;
    }
}
using UnityEngine;
using System.Collections;

public class AutoTileShow : MonoBehaviour
{
    int i = 0;
    private Sprite m_Image;
    public bool open = true;
    Sprite[] sp;

    void Start()
    {
        if (!gameObject.GetComponent<SpriteRenderer>())
        {
            m_Image = gameObject.AddComponent<SpriteRenderer>().sprite;
        }

        sp = new Sprite[4];//图片数据临时存放 
    }

    float timebattle;
    public string keys;
    public int Weight;
    int maxTotal = 4;
    void Update()
    {

        timebattle -= Time.deltaTime;
        if (timebattle <= 0)
        {
            timebattle = .2f;//闪烁频率 

            if (open)
            {
                switch (keys)
                {
                    case "play0":
                    case "play2":
                    case "play3":
                    case "play4":
                    case "play5":
                    case "play6":
                    case "play7":
                    case "play8":
                    case "play9":
                    case "play10":
                    case "play11":
                    case "play12":
                    case "play13":
                    case "play14":
                    case "play15":
                    case "play16":
                    case "play17":
                    case "play18":
                    case "play19":
                    case "play20":
                    case "play21":
                    case "play22":
                    case "play23":

                        maxTotal = 1; 
                        break;
                    case "play24":
                    case "play25":

                        maxTotal = 3;
                        break;

                    case "play26":
                    case "play27":
                    case "play28":
                    case "play29":
                    case "play30":
                    case "play31":
                    case "play32":

                        maxTotal = 4;
                        break;
                    default: break;
                }
                sp = OnDirectionAutoTile(keys, Weight);

                open = false;
            }
            if (i > maxTotal - 1)
            {
                i = 0;
            }
            m_Image = sp[i];
            gameObject.GetComponent<SpriteRenderer>().sprite = m_Image;
            i++;
        }
    }

    public Sprite[] OnDirectionAutoTile(string key, int number)
    {
        Sprite[] mapTile = new Sprite[4];
        mapTile = GameModel.getInstance()._TileSprite[number + "," + key];
        return mapTile;
    }
}

这么一贴,估计没几个朋友有兴趣继续看下去了。

好吧。上demo

链接:http://pan.baidu.com/s/1hrdKyHm 密码:h6wl

 

有什么不懂的,可以在 年后10天,提问

posted on 2016-02-03 13:12  太粗难进  阅读(1087)  评论(2编辑  收藏  举报