美文网首页
2D图片合成游戏

2D图片合成游戏

作者: 沉麟 | 来源:发表于2019-05-10 10:04 被阅读0次

2D图片合成游戏,主要是这个放在图片上的合成脚本

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

using System.Collections.Generic;

public class DragOnPic : BaseImage, IBeginDragHandler, IDragHandler, IEndDragHandler,IPointerExitHandler,IPointerClickHandler
{

    [SerializeField]
    private Text _id;
    public Text _Id
    {
        get
        {
            return _id;
        }
    }
    //记录下自己的父物体.
    private Transform myParent = null;

    public int giftId = -1;

    //Panel,使拖拽是显示在最上方.
    private Transform tempParent;

    private CanvasGroup cg;
    private RectTransform rt;

    //记录鼠标位置.
    private Vector3 newPosition;
    DragOnPic go;
    private GameObject targetObj = null;
    private bool achieveValue = false;

    void Awake()
    {
        //添加CanvasGroup组件用于在拖拽时忽略自己,从而检测到被交换的图片.
        if (gameObject.GetComponent<CanvasGroup>() == null)
        {
            cg = this.gameObject.AddComponent<CanvasGroup>();
        }
        else
        {
            cg = gameObject.GetComponent<CanvasGroup>();
        }
        
        rt = this.GetComponent<RectTransform>();
        tempParent = GameObject.Find("UIMgr").transform;

    }

    /// <summary>
    /// 开始拖拽
    /// </summary>
    public void OnBeginDrag(PointerEventData eventData)
    {
        GameObject target = eventData.pointerEnter;
        if (this.id == -1)
        {
            return;
        }
        //拖拽开始时记下自己的父物体.
        myParent = transform.parent;
        //拖拽开始时禁用检测.
        cg.blocksRaycasts = false;
        this.transform.SetParent(tempParent);

        //创建半透明飞机残影
        go = Instantiate(this, myParent);
        go.transform.localPosition = Vector3.zero;
        go.transform.GetComponent<Image>().color = new Color(1f, 1f, 1f, 0.3f);

        //显示可合并飞机背景标识
        if (AirportManager.Instance.CanMergePlane() != null)
        {
            foreach (var box in AirportManager.Instance.CanMergePlane())
            {
                if (box.IsNotEmpty() && box.PlaneID() == id && box.gameObject != myParent.parent.gameObject)
                {
                    box.bg.SetActive(true);
                }
            }
        }

        //显示可回收标识
        EventManager.Instance.TriggerEvent(EventKey.EventShowDel);

    }

    // n 4~12   item  0{row = 0, col = 0}
    // MaxCol = n/3 + n%3 == 0 ? 0 : 1;   // n = 4  2   n = 5 2
    // MaxRow = n/MaxCol + n%MaxCol == 0 ? 0 : 1;   // n = 4 2, n= 5 3
    // x = col*150-0.5*150*MaxCol+0.5*150
    // y = -row*120;

    /// <summary>
    /// 拖拽
    /// </summary>
    void IDragHandler.OnDrag(PointerEventData eventData)
    {

        GameObject target = eventData.pointerEnter;
        if (this.id == -1)
        {
            return;
        }
        Debug.Log(target);
        //被选中的飞机图片压暗
        if (target && target.transform.parent.tag == "Box")
        {
            if (targetObj != null && targetObj != target)
            {
                targetObj.GetComponent<Image>().color = Color.white;
            }
            target.transform.GetComponent<Image>().color = new Color(0.8f, 0.8f, 0.8f, 1f);
            targetObj = target;
        }

        
        //拖拽时图片跟随鼠标移动.
        RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, Input.mousePosition, eventData.enterEventCamera, out newPosition);
        transform.position = newPosition;
        
    }

    /// <summary>
    /// 停止拖拽
    /// </summary>
    public void OnEndDrag(PointerEventData eventData)
    {
        if (this.id == -1)
        {
            return;
        }
        //删除半透明飞机残影
        if (myParent.childCount > 0)
        {
            for (int i = 0; i < myParent.childCount; i++)
            {
                Destroy(myParent.GetChild(i).gameObject);
            }
        }

        //隐藏可合并背景标识
        if (AirportManager.Instance.CanMergePlane() != null)
        {
            foreach (var box in AirportManager.Instance.CanMergePlane())
            {
                if (box.IsNotEmpty() && box.PlaneID() == id)
                {
                    box.bg.SetActive(false);
                }
            }
        }

        //隐藏可回收标识
        EventManager.Instance.TriggerEvent(EventKey.EventHideDel);

        //获取鼠标下面的物体.
        GameObject target = eventData.pointerEnter;

        if (target == null)
        {
            //Debug.LogError("拖拽位置有误:" + target.name);
            transform.SetParent(myParent);
            transform.localPosition = Vector3.zero;

            AirportManager.Instance.SaveAirInfo();
            //拖拽结束时启用检测.
            cg.blocksRaycasts = true;
            MainLayer.intiii.OnRefresh();
            return;
        }

        //如果能检测到物体.
        if (target && target.tag == "Box" || target.transform.parent.tag == "Box")
        {
            //如果是盒子
            if (target.tag == "Box")
            {
                //如果盒子里没东西
                if (target.transform.childCount == 0 && target.tag == "Box")
                {
                    //Debug.LogError("空盒子:" + target.name);
                    transform.SetParent(target.transform);
                    transform.localPosition = Vector3.zero;
                    
                }
                else
                {
                    //Debug.LogError("有物体:" + target.transform.GetChild(0).name);
                    transform.SetParent(myParent);
                    transform.localPosition = Vector3.zero;
                    
                }               
            }
            else if (target.transform.parent.tag == "Box")
            {

                //如果盒子里面物体id相同
                if (target.GetComponent<BaseImage>() && target.GetComponent<BaseImage>().id == id)
                {
                    //Debug.LogError("合成:" + target.name);
                    if (id == 28)
                    {
                        transform.SetParent(target.transform.parent);
                        transform.localPosition = Vector3.zero;
                        target.transform.SetParent(myParent);
                        target.transform.localPosition = Vector3.zero;
                        return;
                    }

                    Destroy(target);                    
                    transform.SetParent(target.transform.parent);
                    transform.localPosition = Vector3.zero;
                    
                    
                    AppData.Instance.curLevelExp += Mathf.Pow(2, id - 2)*2;
                    target.transform.parent.parent.GetComponent<Box>().ShowMerge(gameObject);
                    if (AppData.Instance.curMaxTankLevel < id+1)
                    {
                        AppData.Instance.curMaxTankLevel++;
                        AppData.Instance.tankIds.Add(AppData.Instance.curMaxTankLevel);
                        UIMgr.Instance.ShowDlg(Enums.DlgId.DlgMerge,id);
                        //UpdateId();
                        //target.transform.parent.parent.GetComponent<Box>().ShowMerge(gameObject);
                        //transform.GetComponent<Image>().sprite = ResourceManager.Instance.GetResource<Sprite>(string.Format("0{0}", id));                        
                        AirportManager.Instance.SaveAirInfo();

                    }
                    else
                    {
                        //target.transform.parent.parent.GetComponent<Box>().ShowMerge(gameObject);
                        
                    }
                    achieveValue = false;
                    //foreach (KeyValuePair<int, int> kvp in AppData.Instance.achieveDic)
                    //{
                    //    if (kvp.Key == id + 1)
                    //    {
                    //        achieveValue = true;
                    //    }
                    //}

                    //if (achieveValue)
                    //{
                    //    AppData.Instance.achieveDic[id + 1]++;
                    //    achieveValue = false;
                    //}

                    //UIMgr.Instance.GetDlg<TaskDlg>(Enums.DlgId.DlgTask).OnRefresh();
                    //UIMgr.Instance.GetDlg<ListingDlg>(Enums.DlgId.DlgListing).OnRefresh();
                    //UIMgr.Instance.GetLayer<MainLayer>(Enums.LayerId.LayerMain).ShowRedDot();
                    
                }
                else if (target.GetComponent<BaseImage>() && target.GetComponent<BaseImage>().id < 1)
                {
                    transform.SetParent(myParent);
                    transform.localPosition = Vector3.zero;
                }
                else
                {
                    //Debug.LogError("交换位置:" + target.name);
                    transform.SetParent(target.transform.parent);
                    transform.localPosition = Vector3.zero;
                    target.transform.SetParent(myParent);
                    target.transform.localPosition = Vector3.zero;
                   
                }
            }
            
        }
        else if (target.tag == "DelArea")
        {
            target.transform.GetChild(0).GetComponent<Animator>().Play("CloudAnim");
            transform.SetParent(target.transform);
            transform.localPosition = Vector3.zero;
            transform.gameObject.SetActive(false);
            Destroy(this);
            
            //AppData.Instance.coinCount += (int)(Mathf.Pow(3.1f, id - 2) * 1600 * Mathf.Pow((id > 1 ? 1.175f : 1.07f), AppData.Instance.coinRatesDic[id]));
            UIMgr.Instance.GetLayer<MainLayer>(Enums.LayerId.LayerMain).DelAreaPlane(Mathf.Pow(3.1f, id - 2) * 1600 * Mathf.Pow((id > 1 ? 1.175f : 1.07f), AppData.Instance.coinRatesDic[id]));
            //Debug.LogError(Mathf.Pow(3.1f, id - 2) * 1600 * Mathf.Pow((id > 1 ? 1.175f : 1.07f), AppData.Instance.coinRatesDic[id]));
        }
        else if (target.tag == "AirportMain")
        {
            //Debug.LogError("展示当前飞机:" + target.name);
            SoundManager.Instance.Play(Enums.SoundId.SwithTank);
            transform.SetParent(myParent);
            transform.localPosition = Vector3.zero;
            AppData.Instance.curTank = id;
            MainTanks.Instance.showPlayer.SelectTank(AppData.Instance.curTank);
        }
        else
        {
            //Debug.LogError("拖拽位置有误:" + target.name);
            transform.SetParent(myParent);
            transform.localPosition = Vector3.zero;
            
        }
        AirportManager.Instance.SaveAirInfo();
        //拖拽结束时启用检测.
        cg.blocksRaycasts = true;
        MainLayer.intiii.OnRefresh();
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        GameObject target = eventData.pointerEnter;
        if (target.transform.parent.tag == "Box")
        {
            target.transform.GetComponent<Image>().color = new Color(1f, 1f, 1f, 1f);
        }
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        GameObject target = eventData.pointerEnter;
        
        if (target.GetComponent<BaseImage>() && target.GetComponent<BaseImage>().id == -1)
        {
            SoundManager.Instance.Play(Enums.SoundId.BoxOpen);
            target.transform.parent.parent.transform.GetComponent<Box>()._particle.Play();
            GameObject go = Instantiate(Resources.Load<GameObject>("TankUIPrefabs/Tank"), target.transform.parent);
            DragOnPic dragOnPic = go.GetComponent<DragOnPic>();
            if (target.GetComponent<DragOnPic>().giftId != -1)
            {
                dragOnPic.id = target.GetComponent<DragOnPic>().giftId;
            }
            dragOnPic.Refresh();
            dragOnPic.GetComponent<Image>().sprite = Resources.Load<Sprite>(string.Format("TankSprites/{0}", dragOnPic.id));
            go.transform.localPosition = Vector3.zero;            
            target.gameObject.SetActive(false);
            Destroy(target.gameObject);
            AirportManager.Instance.SaveAirInfo();
        }

    }

   
    public override void Refresh() {
        base.Refresh();
        if (_id != null) {
            _id.text = id.ToString();
        }
    }

    //private void ShowUpLv()
    //{
    //    AppData.Instance.curLevelExp -= (int)(Mathf.Pow(2.2f, AppData.Instance.playerLevel - 1) * 6);
    //    AppData.Instance.playerLevel += 1;
    //    UIMgr.Instance.ShowDlg(Enums.DlgId.DlgUpgrade);
    //}

}

相关文章

  • 2D图片合成游戏

    2D图片合成游戏,主要是这个放在图片上的合成脚本

  • Unity 2D精灵

    什么是Sprite精灵 2D游戏中的游戏物体,实际上是对图片的处理。把图片的格式转换成可以直接用在2D游戏中的格式...

  • 人工智能画廊H5 | 我和绘画机器人Andy作品展

    【案例】我和绘画机器人Andy作品展 【类型】游戏型-图片合成 美图的这个H5采用的是图片合成式的游戏,对于此种类...

  • 《土豆荣耀》重构笔记(四)创建背景动画

    前言   在2D游戏中,游戏背景常常由一张、或者多张图片构成。而图片是静态的,为了不让游戏背景那么一成不变,显得更...

  • 一阶段day16-02Pygame

    Pygame是一个用Python写2D游戏的第三方库 1、游戏初始化 2、创建游戏窗口 显示图片 窗口.blit(...

  • 引擎01——plist图集使用方法

    1、在游戏中使用多张图片合成的图集作为美术资源好处一:合成图集时会去除每张图片周围的空白区域,加上可以在整体上实施...

  • 对图片的设置

    合成图片。下载图片。拉伸图片 //合成图片 UIImageView *imageView = [[UIImageV...

  • 图片合成

    两张图片拼接 参考:https://www.feiyeda.top/http://csshengyao.cn/

  • 图片合成

    前段时间遇到产品一个需求,在移动端上合成生成二维码,并且合成背景图和二维码给用户保存; 当时我在想,链接拼接生成二...

  • js利用canvas合成图片

    转自 前端老徐合成图片 递归方式合成图片

网友评论

      本文标题:2D图片合成游戏

      本文链接:https://www.haomeiwen.com/subject/xzjooqtx.html