修改代码,修改成不分顺序,进行判断对错

This commit is contained in:
李浩 2025-05-29 18:31:39 +08:00
parent 28dba51958
commit b554bdd976
5 changed files with 450 additions and 371 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,80 +1,5 @@
%YAML 1.1 %YAML 1.1
%TAG !u! tag:unity3d.com,2011: %TAG !u! tag:unity3d.com,2011:
--- !u!1 &420915011696631178
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8326800272042336731}
- component: {fileID: 6107971669464005120}
- component: {fileID: 4530685853539565497}
m_Layer: 0
m_Name: Image
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
--- !u!224 &8326800272042336731
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 420915011696631178}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 3838833872239473580}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &6107971669464005120
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 420915011696631178}
m_CullTransparentMesh: 1
--- !u!114 &4530685853539565497
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 420915011696631178}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 0.49019608}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 21300000, guid: 3a00983ef5623fc408efcbb92a4907c3, type: 3}
m_Type: 0
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!1 &1679127656931134385 --- !u!1 &1679127656931134385
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -112,8 +37,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1} m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1} m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 921, y: -268.95117} m_AnchoredPosition: {x: 851.9, y: -268.95117}
m_SizeDelta: {x: 259, y: 101} m_SizeDelta: {x: 118.7996, y: 101}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &4674184409838659037 --- !u!222 &4674184409838659037
CanvasRenderer: CanvasRenderer:
@ -800,7 +725,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1} m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 1, y: 1} m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0.0009442579} m_AnchoredPosition: {x: 0, y: 0.0009866873}
m_SizeDelta: {x: 0, y: 300} m_SizeDelta: {x: 0, y: 300}
m_Pivot: {x: 0, y: 1} m_Pivot: {x: 0, y: 1}
--- !u!114 &1181747651547168674 --- !u!114 &1181747651547168674
@ -2048,7 +1973,7 @@ MonoBehaviour:
m_HandleRect: {fileID: 6608813160943937009} m_HandleRect: {fileID: 6608813160943937009}
m_Direction: 2 m_Direction: 2
m_Value: 0 m_Value: 0
m_Size: 1 m_Size: 0.9999988
m_NumberOfSteps: 0 m_NumberOfSteps: 0
m_OnValueChanged: m_OnValueChanged:
m_PersistentCalls: m_PersistentCalls:
@ -2088,8 +2013,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 0.5} m_AnchorMin: {x: 1, y: 0.5}
m_AnchorMax: {x: 1, y: 0.5} m_AnchorMax: {x: 1, y: 0.5}
m_AnchoredPosition: {x: -45.8, y: 0.9} m_AnchoredPosition: {x: -61.2656, y: 0.9}
m_SizeDelta: {x: 70, y: 70} m_SizeDelta: {x: 100.9316, y: 70}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &6611614184386924169 --- !u!222 &6611614184386924169
CanvasRenderer: CanvasRenderer:
@ -2238,7 +2163,6 @@ RectTransform:
- {fileID: 4103523224226093892} - {fileID: 4103523224226093892}
- {fileID: 5456883306320455666} - {fileID: 5456883306320455666}
- {fileID: 8333043509333940129} - {fileID: 8333043509333940129}
- {fileID: 8326800272042336731}
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.9 MiB

View File

@ -1,179 +0,0 @@
fileFormatVersion: 2
guid: 3a00983ef5623fc408efcbb92a4907c3
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 12
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
flipGreenChannel: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
vTOnly: 0
ignoreMipmapLimit: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 1
aniso: 2
mipBias: 0
wrapU: 1
wrapV: 1
wrapW: 0
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 8
textureShape: 1
singleChannelComponent: 0
flipbookRows: 1
flipbookColumns: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
ignorePngGamma: 0
applyGammaDecoding: 0
swizzle: 50462976
cookieLightType: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 8192
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 8192
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: iPhone
maxTextureSize: 8192
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Android
maxTextureSize: 8192
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Windows Store Apps
maxTextureSize: 8192
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: WebGL
maxTextureSize: 8192
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Server
maxTextureSize: 8192
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID: 5e97eb03825dee720800000000000000
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
nameFileIdTable: {}
mipmapLimitGroupName:
pSDRemoveMatte: 0
userData:
assetBundleName:
assetBundleVariant:

View File

@ -6,6 +6,7 @@ using UnityEngine.EventSystems;
using System; using System;
using TMPro; using TMPro;
using System.Xml; using System.Xml;
using System.Linq;
namespace QFramework.Example namespace QFramework.Example
{ {
@ -48,6 +49,10 @@ namespace QFramework.Example
GameObject leftDragObj; GameObject leftDragObj;
Dictionary<GameObject, UIImageSelectMapData.OptionItem> itemDatas = new Dictionary<GameObject, UIImageSelectMapData.OptionItem>(); Dictionary<GameObject, UIImageSelectMapData.OptionItem> itemDatas = new Dictionary<GameObject, UIImageSelectMapData.OptionItem>();
Dictionary<GameObject, RightDataItem> leftAndRightMap = new Dictionary<GameObject, RightDataItem>(); Dictionary<GameObject, RightDataItem> leftAndRightMap = new Dictionary<GameObject, RightDataItem>();
// 新增:存储分类行对象和数据的映射
Dictionary<GameObject, UIImageSelectMapData.Row> rowMap = new Dictionary<GameObject, UIImageSelectMapData.Row>();
protected override void OnInit(IUIData uiData = null) protected override void OnInit(IUIData uiData = null)
{ {
mData = uiData as UIImageSelectMapData ?? new UIImageSelectMapData(); mData = uiData as UIImageSelectMapData ?? new UIImageSelectMapData();
@ -63,26 +68,33 @@ namespace QFramework.Example
RightContent.RemoveAllChildren(); RightContent.RemoveAllChildren();
itemDatas.Clear(); itemDatas.Clear();
leftAndRightMap.Clear(); leftAndRightMap.Clear();
rowMap.Clear(); // 清空行映射
foreach (var row in mData.rows) foreach (var row in mData.rows)
{ {
GameObject rowObj = GameObject.Instantiate(RightRowItem.gameObject, RightContent); GameObject rowObj = GameObject.Instantiate(RightRowItem.gameObject, RightContent);
rowObj.transform.Find("Type").GetComponent<TextMeshProUGUI>().text = row.name.Replace("\\n", "\n"); var typeText = rowObj.transform.Find("Type").GetComponent<TextMeshProUGUI>();
typeText.text = row.name.Replace("\\n", "\n");
rowObj.name = row.name.Replace("\\n", "\n");
Transform itemContent = rowObj.transform.Find("Items"); Transform itemContent = rowObj.transform.Find("Items");
// 存储行对象和数据的映射
rowMap.Add(rowObj, row);
foreach (var item in row.items) foreach (var item in row.items)
{ {
var rightObj = GameObject.Instantiate(RightItem, itemContent); var rightObj = GameObject.Instantiate(RightItem, itemContent);
var nameText = rightObj.transform.Find("Name").GetComponent<TextMeshProUGUI>(); var nameText = rightObj.transform.Find("Name").GetComponent<TextMeshProUGUI>();
nameText.text = item.name; nameText.text = item.name;
nameText.color = Color.white; nameText.color = Color.white;
rightObj.name = item.pic; rightObj.name = item.name;
rightObj.transform.Find("PicBg/Pic").GetComponent<Image>().color = new Color(1, 1, 1, 0); rightObj.transform.Find("PicBg/Pic").GetComponent<Image>().color = new Color(1, 1, 1, 0);
rightObj.OnPointerEnterEvent(RightOnEnter); rightObj.OnPointerEnterEvent(RightOnEnter);
rightObj.OnPointerExitEvent(RightOnExit); rightObj.OnPointerExitEvent(RightOnExit);
rightObj.OnPointerClickEvent(RightOnClick); rightObj.OnPointerClickEvent(RightOnClick);
var leftObj = GameObject.Instantiate(LeftItem, LeftContent); var leftObj = GameObject.Instantiate(LeftItem, LeftContent);
leftObj.name = item.pic; leftObj.name = row.name + "|" + item.name;
var path = Global.deviceIconsPath + item.pic; var path = Global.deviceIconsPath + item.pic;
loader.Add2Load(path.ToNetImageResName(), (success, res) => loader.Add2Load(path.ToNetImageResName(), (success, res) =>
{ {
@ -105,9 +117,6 @@ namespace QFramework.Example
} }
loader.LoadAsync(); loader.LoadAsync();
} }
private void OnModuleQuitHandler(OnModuleQuit quit) private void OnModuleQuitHandler(OnModuleQuit quit)
@ -127,12 +136,8 @@ namespace QFramework.Example
enterRight.transform.Find("Name").GetComponent<TextMeshProUGUI>().color = Color.white; enterRight.transform.Find("Name").GetComponent<TextMeshProUGUI>().color = Color.white;
leftAndRightMap.Remove(trans.gameObject); leftAndRightMap.Remove(trans.gameObject);
} }
} }
private void RightOnExit(PointerEventData data) private void RightOnExit(PointerEventData data)
{ {
enterRight = null; enterRight = null;
@ -150,69 +155,117 @@ namespace QFramework.Example
moveObj.name = moveObj.name.Replace("(Clone)", ""); moveObj.name = moveObj.name.Replace("(Clone)", "");
moveObj.GetComponent<RectTransform>().sizeDelta = LeftContent.GetComponent<GridLayoutGroup>().cellSize; moveObj.GetComponent<RectTransform>().sizeDelta = LeftContent.GetComponent<GridLayoutGroup>().cellSize;
moveObj.GetComponent<Image>().raycastTarget = false; moveObj.GetComponent<Image>().raycastTarget = false;
} }
private void LeftOnEndDrag(PointerEventData data) private void LeftOnEndDrag(PointerEventData data)
{ {
if (enterRight && leftAndRightMap.ContainsKey(enterRight) == false) if (enterRight == null || leftAndRightMap.ContainsKey(enterRight))
{ return;
TextMeshProUGUI nameText = enterRight.transform.Find("Name").GetComponent<TextMeshProUGUI>(); TextMeshProUGUI nameText = enterRight.transform.Find("Name").GetComponent<TextMeshProUGUI>();
bool isRight = false; bool isRight = false;
if (enterRight.name == itemDatas[leftDragObj].pic)
// 获取正确的分类数据
UIImageSelectMapData.Row categoryRow = GetCategoryRow(enterRight);
if (categoryRow == null)
{
Debug.LogError("无法找到对应的分类数据");
return;
}
var draggedItem = itemDatas[leftDragObj];
bool foundMatch = CheckItemInCategory(draggedItem, categoryRow);
// 更新UI反馈
if (foundMatch)
{ {
nameText.color = Color.green; nameText.color = Color.green;
isRight = true; isRight = true;
Debug.Log($"正确匹配: {draggedItem.name} 到分类 {categoryRow.name}");
} }
else else
{ {
nameText.color = Color.red; nameText.color = Color.red;
Debug.Log($"错误匹配: {draggedItem.name} 到分类 {categoryRow.name}");
} }
// 更新图片显示
Image img = enterRight.transform.Find("PicBg/Pic").GetComponent<Image>(); Image img = enterRight.transform.Find("PicBg/Pic").GetComponent<Image>();
img.sprite = moveObj.GetComponent<Image>().sprite; img.sprite = moveObj.GetComponent<Image>().sprite;
img.color = Color.white; img.color = Color.white;
//nameText.text = itemDatas[leftDragObj].name;
leftAndRightMap.Add(enterRight, new RightDataItem() { rightObj = leftDragObj, isRight = isRight }); // 记录匹配结果
leftAndRightMap.Add(enterRight, new RightDataItem { rightObj = leftDragObj, isRight = isRight });
leftDragObj.SetActive(false); leftDragObj.SetActive(false);
if (isRight && Check()) // 检查当前分类是否完成
if (isRight && CheckCategoryCompletion(categoryRow))
{ {
if (string.IsNullOrEmpty(mData.finishedEvent) == false) if (!string.IsNullOrEmpty(mData.finishedEvent))
{ {
StringEventSystem.Global.Send(mData.finishedEvent); StringEventSystem.Global.Send(mData.finishedEvent);
Debug.Log($"分类 {categoryRow.name} 已完成,触发完成事件");
} }
} }
} // 清理拖拽对象
GameObject.Destroy(moveObj); GameObject.Destroy(moveObj);
moveObj = null; moveObj = null;
leftDragObj = null; leftDragObj = null;
} }
public bool Check() // 获取分类行数据
private UIImageSelectMapData.Row GetCategoryRow(GameObject targetObj)
{ {
foreach (var item in leftAndRightMap) Transform parentTransform = targetObj.transform.parent;
// 查找行对象
while (parentTransform != null && parentTransform != RightContent.transform)
{ {
if (item.Value.isRight == false) if (rowMap.ContainsKey(parentTransform.gameObject))
{ {
return rowMap[parentTransform.gameObject];
}
parentTransform = parentTransform.parent;
}
return null;
}
// 检查项目是否在分类中
private bool CheckItemInCategory(UIImageSelectMapData.OptionItem item, UIImageSelectMapData.Row categoryRow)
{
return categoryRow.items.Exists(i => i.pic == item.pic || i.name == item.name);
}
// 检查分类是否完成匹配
private bool CheckCategoryCompletion(UIImageSelectMapData.Row categoryRow)
{
if (categoryRow == null)
return false; return false;
// 查找该分类需要匹配的项目数
int required = categoryRow.items.Count;
// 查找该分类已正确匹配的项目数
int actual = 0;
if (leftAndRightMap.Any(p => p.Value.isRight && GetCategoryRow(p.Key) == categoryRow))
{
actual = leftAndRightMap.Count(p => p.Value.isRight && GetCategoryRow(p.Key) == categoryRow);
} }
}
return leftAndRightMap.Count == itemDatas.Count; // 判断是否完成
return actual >= required;
} }
private void LeftOnDrag(PointerEventData data) private void LeftOnDrag(PointerEventData data)
{ {
//data.pointerDrag.transform.position = Input.mousePosition;
if (moveObj) if (moveObj)
{ {
moveObj.transform.position = Input.mousePosition; moveObj.transform.position = Input.mousePosition;
} }
} }
protected override void OnShow() protected override void OnShow()
{ {
} }