250 lines
10 KiB
C#
250 lines
10 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using QFramework;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using System.Linq;
|
||
using DG.Tweening;
|
||
using static OperationController;
|
||
|
||
namespace QFramework.Example
|
||
{
|
||
public class UIToolsData : UIPanelData
|
||
{
|
||
public List<string> devices;
|
||
public string answer;
|
||
public bool SetActive = true;
|
||
public string rightLable;
|
||
public string wrongLabel;
|
||
public string rightEvent;
|
||
public string wrongEvent;
|
||
public float rightScore;
|
||
public float wrongScore;
|
||
public float totalScore;
|
||
public string scoreStepName;
|
||
public float autoHideResult = -1;
|
||
public bool random = false;
|
||
public float scrollSpeed = 25;
|
||
public string position;
|
||
public int wrongCount = 0;
|
||
}
|
||
public partial class UITools : UIPanel
|
||
{
|
||
ResLoader mResLoader;
|
||
public List<string> answers;
|
||
int curWrongCount = 0;
|
||
protected override void OnInit(IUIData uiData = null)
|
||
{
|
||
mData = uiData as UIToolsData ?? new UIToolsData();
|
||
mResLoader = ResLoader.Allocate();
|
||
// please add init code here
|
||
TypeEventSystem.Global.Register<OnModuleQuit>(OnModuleQuit).UnRegisterWhenGameObjectDestroyed(gameObject);
|
||
}
|
||
|
||
private void OnStepChanged(StepStatusOnChange change)
|
||
{
|
||
Hide();
|
||
}
|
||
|
||
protected override void OnOpen(IUIData uiData = null)
|
||
{
|
||
mData = uiData as UIToolsData ?? new UIToolsData();
|
||
curWrongCount = 0;
|
||
TypeEventSystem.Global.Register<StepStatusOnChange>(OnStepChanged).UnRegisterWhenDisabled(gameObject);
|
||
if (mData.totalScore > 0)
|
||
{
|
||
ScoreController.Instance.Add(mData.scoreStepName, mData.totalScore);
|
||
}
|
||
mData = uiData as UIToolsData ?? new UIToolsData();
|
||
if (string.IsNullOrEmpty(mData.answer) == false)
|
||
{
|
||
answers = mData.answer.Split(',')?.ToList();
|
||
}
|
||
Content.RemoveAllChildren();
|
||
if (mData.devices.Count > 0 && mData.random)
|
||
{
|
||
Utility.Shuffle(mData.devices);
|
||
}
|
||
Scroll.scrollSensitivity = mData.scrollSpeed;
|
||
foreach (var device in mData.devices)
|
||
{
|
||
var item = DeviceController.Instance.GetDevice(device);
|
||
if (item == null)
|
||
{
|
||
Debug.LogError(device + ":û<><C3BB><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD>Ӧ<EFBFBD><D3A6>Device<63><65><EFBFBD><EFBFBD>");
|
||
return;
|
||
}
|
||
GameObject obj = GameObject.Instantiate(ItemPrefab.gameObject, Content);
|
||
obj.name = item.Name;
|
||
obj.transform.Find("Name").GetComponent<TextMeshProUGUI>().text = item.Name;
|
||
Image icon = obj.transform.Find("IconBg/Icon").GetComponent<Image>();
|
||
var localImageUrl = Global.deviceIconsPath + item.Icon;
|
||
GameObject right = icon.transform.Find("Right").gameObject;
|
||
GameObject wrong = icon.transform.Find("Wrong").gameObject;
|
||
GameObject Selected = icon.transform.Find("Selected").gameObject;
|
||
mResLoader.Add2Load(localImageUrl.ToNetImageResName(),
|
||
(bool success, IRes res) =>
|
||
{
|
||
if (success)
|
||
{
|
||
icon.sprite = Utility.GetSprite(res.Asset as Texture2D);
|
||
}
|
||
});
|
||
Button btn = obj.GetComponent<Button>();
|
||
btn.onClick.AddListener(() =>
|
||
{
|
||
|
||
if (answers != null)
|
||
{
|
||
if (answers.Contains(item.Name))
|
||
{
|
||
SetSelected(obj, true);
|
||
if (mData.SetActive)
|
||
{
|
||
DeviceController.Instance.GetDeviceObj(device).SetActive(true);
|
||
}
|
||
ScoreController.Instance.Add(mData.scoreStepName, mData.rightScore);
|
||
answers.Remove(item.Name);
|
||
if (answers.Count <= 0)
|
||
{
|
||
if (string.IsNullOrEmpty(mData.rightLable) == false)
|
||
{
|
||
var data = new UIResultTipData();
|
||
data.label = mData.rightLable;
|
||
data.isRight = true;
|
||
data.callback = () =>
|
||
{
|
||
if (string.IsNullOrEmpty(mData.rightEvent) == false)
|
||
{
|
||
StringEventSystem.Global.Send(mData.rightEvent);
|
||
}
|
||
};
|
||
data.autoHideTime = mData.autoHideResult;
|
||
UIKit.OpenPanelAsync<UIResultTip>(uiData: data, canvasLevel: UILevel.PopUI).ToAction().StartGlobal();
|
||
}
|
||
else
|
||
{
|
||
if (string.IsNullOrEmpty(mData.rightEvent) == false)
|
||
{
|
||
if (curWrongCount >= mData.wrongCount)
|
||
{
|
||
StringEventSystem.Global.Send(mData.rightEvent);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
ScoreController.Instance.Add(mData.scoreStepName, mData.wrongScore);
|
||
if (string.IsNullOrEmpty(mData.wrongLabel) == false)
|
||
{
|
||
var data = new UIResultTipData();
|
||
data.label = mData.wrongLabel;
|
||
data.isRight = false;
|
||
data.callback = () =>
|
||
{
|
||
|
||
if (string.IsNullOrEmpty(mData.wrongEvent) == false)
|
||
{
|
||
if (curWrongCount >= mData.wrongCount)
|
||
{
|
||
StringEventSystem.Global.Send(mData.wrongEvent);
|
||
}
|
||
}
|
||
SetSelected(obj, false);
|
||
};
|
||
data.autoHideTime = mData.autoHideResult;
|
||
UIKit.OpenPanelAsync<UIResultTip>(uiData: data, canvasLevel: UILevel.PopUI).ToAction().Start(this);
|
||
}
|
||
else
|
||
{
|
||
if (string.IsNullOrEmpty(mData.wrongEvent) == false)
|
||
{
|
||
if (curWrongCount >= mData.wrongCount)
|
||
{
|
||
StringEventSystem.Global.Send(mData.wrongEvent);
|
||
}
|
||
}
|
||
SetSelected(obj, false);
|
||
|
||
}
|
||
}
|
||
}
|
||
this.curWrongCount++;
|
||
});
|
||
}
|
||
mResLoader.LoadAsync();
|
||
Scroll.verticalNormalizedPosition = 1;
|
||
|
||
|
||
switch (mData.position)
|
||
{
|
||
case "left":
|
||
// <20><>ê<EFBFBD><C3AA><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2>м䣨Middle-Right<68><74>
|
||
bg.rectTransform.anchorMin = new Vector2(0, 0.5f); // <20><><EFBFBD><EFBFBD>ê<EFBFBD>㣨<EFBFBD>Ҳࣩ
|
||
bg.rectTransform.anchorMax = new Vector2(0, 0.5f); // <20><><EFBFBD><EFBFBD>ê<EFBFBD>㣨<EFBFBD>Ҳࣩ
|
||
|
||
// <20><>λ<EFBFBD><CEBB>ƫ<EFBFBD>ƹ<EFBFBD><C6B9>㣨<EFBFBD><E3A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ê<EFBFBD>㣩
|
||
bg.rectTransform.anchoredPosition = Vector2.zero;
|
||
|
||
// <20><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>Ϊ<EFBFBD>Ҳ<EFBFBD><D2B2>е㣨Ӱ<E3A3A8><D3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>/<2F><>ת<EFBFBD><D7AA><EFBFBD>ģ<EFBFBD>
|
||
bg.rectTransform.pivot = new Vector2(0, 0.5f);
|
||
break;
|
||
case "right":
|
||
default:
|
||
// <20><>ê<EFBFBD><C3AA><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2>м䣨Middle-Right<68><74>
|
||
bg.rectTransform.anchorMin = new Vector2(1, 0.5f); // <20><><EFBFBD><EFBFBD>ê<EFBFBD>㣨<EFBFBD>Ҳࣩ
|
||
bg.rectTransform.anchorMax = new Vector2(1, 0.5f); // <20><><EFBFBD><EFBFBD>ê<EFBFBD>㣨<EFBFBD>Ҳࣩ
|
||
|
||
// <20><>λ<EFBFBD><CEBB>ƫ<EFBFBD>ƹ<EFBFBD><C6B9>㣨<EFBFBD><E3A3A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ê<EFBFBD>㣩
|
||
bg.rectTransform.anchoredPosition = Vector2.zero;
|
||
|
||
// <20><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>Ϊ<EFBFBD>Ҳ<EFBFBD><D2B2>е㣨Ӱ<E3A3A8><D3B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>/<2F><>ת<EFBFBD><D7AA><EFBFBD>ģ<EFBFBD>
|
||
bg.rectTransform.pivot = new Vector2(1, 0.5f);
|
||
break;
|
||
}
|
||
|
||
}
|
||
|
||
public void SetSelected(GameObject item, bool isRight)
|
||
{
|
||
Transform icon = item.transform.Find("IconBg/Icon");
|
||
GameObject right = icon.Find("Right").gameObject;
|
||
GameObject wrong = icon.Find("Wrong").gameObject;
|
||
GameObject Selected = icon.Find("Selected").gameObject;
|
||
Selected.SetActive(true);
|
||
if (isRight)
|
||
{
|
||
right.SetActive(true);
|
||
wrong.SetActive(false);
|
||
}
|
||
else
|
||
{
|
||
right.SetActive(false);
|
||
wrong.SetActive(true);
|
||
}
|
||
icon.GetComponent<Image>().color = new Color(255f / 255f, 255f / 255f, 255f / 255f, 51f / 255f);
|
||
item.GetComponent<Button>().onClick.RemoveAllListeners();
|
||
}
|
||
|
||
protected override void OnShow()
|
||
{
|
||
}
|
||
|
||
protected override void OnHide()
|
||
{
|
||
}
|
||
|
||
protected override void OnClose()
|
||
{
|
||
//TypeEventSystem.Global.UnRegister<OnModuleQuit>(OnModuleQuit);
|
||
}
|
||
|
||
public void OnModuleQuit(OnModuleQuit arg)
|
||
{
|
||
Hide();
|
||
}
|
||
}
|
||
}
|