159 lines
4.8 KiB
C#
159 lines
4.8 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using QFramework;
|
||
using System.Collections.Generic;
|
||
using UnityEngine.EventSystems;
|
||
using System;
|
||
|
||
namespace QFramework.Example
|
||
{
|
||
public class UIImageSelectMapData : UIPanelData
|
||
{
|
||
public class OptionItem
|
||
{
|
||
public string pic;
|
||
public Vector2 pos;
|
||
public Vector2 size;
|
||
}
|
||
public string scoreName;
|
||
public float totalScore;
|
||
public float rightScore;
|
||
public float wrongScore;
|
||
public string rightLabel;
|
||
public string wrongLabel;
|
||
public string finishedEvent;
|
||
public string rightBg;
|
||
public List<OptionItem> items = new List<OptionItem>();
|
||
|
||
}
|
||
public partial class UIImageSelectMap : UIPanel
|
||
{
|
||
ResLoader loader;
|
||
GameObject moveObj;
|
||
GameObject enterRight;
|
||
GameObject leftDragObj;
|
||
Dictionary<GameObject, UIImageSelectMapData.OptionItem> itemDatas = new Dictionary<GameObject, UIImageSelectMapData.OptionItem>();
|
||
protected override void OnInit(IUIData uiData = null)
|
||
{
|
||
mData = uiData as UIImageSelectMapData ?? new UIImageSelectMapData();
|
||
// please add init code here
|
||
loader = ResLoader.Allocate();
|
||
}
|
||
|
||
protected override void OnOpen(IUIData uiData = null)
|
||
{
|
||
mData = uiData as UIImageSelectMapData ?? new UIImageSelectMapData();
|
||
|
||
LeftContent.RemoveAllChildren();
|
||
RightContent.RemoveAllChildren();
|
||
itemDatas.Clear();
|
||
foreach (var item in mData.items)
|
||
{
|
||
var leftObj = GameObject.Instantiate(LeftItem, LeftContent);
|
||
var path = Global.imagePath + item.pic;
|
||
loader.Add2Load(path.ToNetImageResName(), (success, res) =>
|
||
{
|
||
if (success)
|
||
{
|
||
leftObj.GetComponent<Image>().sprite = Utility.GetSprite(res.Asset as Texture2D);
|
||
}
|
||
});
|
||
|
||
leftObj.OnBeginDragEvent(LeftOnBeginDrag);
|
||
leftObj.OnDragEvent(LeftOnDrag);
|
||
leftObj.OnEndDragEvent(LeftOnEndDrag);
|
||
|
||
var rightObj = GameObject.Instantiate(RightItem, RightContent);
|
||
rightObj.name = item.pic;
|
||
rightObj.rectTransform.anchoredPosition = item.pos;
|
||
rightObj.rectTransform.sizeDelta = item.size;
|
||
rightObj.OnPointerEnterEvent(RightOnEnter);
|
||
rightObj.OnPointerExitEvent(RightOnExit);
|
||
|
||
itemDatas.Add(leftObj.gameObject, item);
|
||
}
|
||
|
||
|
||
|
||
var rightBgPath = Global.imagePath + mData.rightBg;
|
||
loader.Add2Load(rightBgPath.ToNetImageResName(), (success, res) =>
|
||
{
|
||
if (success)
|
||
{
|
||
RightContent.GetComponent<Image>().sprite = Utility.GetSprite(res.Asset as Texture2D);
|
||
RightContent.GetComponent<Image>().SetNativeSize();
|
||
}
|
||
});
|
||
|
||
loader.LoadAsync();
|
||
|
||
|
||
|
||
}
|
||
|
||
private void RightOnExit(PointerEventData data)
|
||
{
|
||
enterRight = null;
|
||
}
|
||
|
||
private void RightOnEnter(PointerEventData data)
|
||
{
|
||
enterRight = data.pointerEnter.gameObject;
|
||
}
|
||
|
||
private void LeftOnBeginDrag(PointerEventData data)
|
||
{
|
||
leftDragObj = data.pointerDrag;
|
||
moveObj = GameObject.Instantiate(LeftItem.gameObject, gameObject.transform);
|
||
moveObj.GetComponent<Image>().raycastTarget = false;
|
||
}
|
||
|
||
private void LeftOnEndDrag(PointerEventData data)
|
||
{
|
||
if (enterRight)
|
||
{
|
||
if (enterRight.name == itemDatas[leftDragObj].pic)
|
||
{
|
||
Debug.LogError("<22><>ȷ");
|
||
enterRight.GetComponent<Image>().color = Color.green;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("<22><><EFBFBD><EFBFBD>");
|
||
enterRight.GetComponent<Image>().color = Color.red;
|
||
}
|
||
moveObj.transform.parent = enterRight.transform;
|
||
moveObj.transform.localPosition = Vector3.zero;
|
||
}
|
||
else
|
||
{
|
||
GameObject.Destroy(moveObj);
|
||
moveObj = null;
|
||
}
|
||
leftDragObj = null;
|
||
}
|
||
|
||
private void LeftOnDrag(PointerEventData data)
|
||
{
|
||
//data.pointerDrag.transform.position = Input.mousePosition;
|
||
if (moveObj)
|
||
{
|
||
moveObj.transform.position = Input.mousePosition;
|
||
}
|
||
}
|
||
|
||
|
||
protected override void OnShow()
|
||
{
|
||
}
|
||
|
||
protected override void OnHide()
|
||
{
|
||
}
|
||
|
||
protected override void OnClose()
|
||
{
|
||
}
|
||
}
|
||
}
|