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);
//}
}
网友评论