108 lines
2.9 KiB
C#
108 lines
2.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
public class TrainingDeviceItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
|
|
{
|
|
/// <summary>
|
|
/// 当前选中的设备选项
|
|
/// </summary>
|
|
public static TrainingDeviceItem SelectedItem;
|
|
/// <summary>
|
|
/// 所有的设备选项
|
|
/// </summary>
|
|
public static List<TrainingDeviceItem> TrainingDeviceItemList = new List<TrainingDeviceItem>();
|
|
/// <summary>
|
|
/// 鼠标悬浮状态背景
|
|
/// </summary>
|
|
public Image SelectedBackground;
|
|
/// <summary>
|
|
/// 选中状态
|
|
/// </summary>
|
|
public Image SelectedStatus;
|
|
/// <summary>
|
|
/// 设备图片
|
|
/// </summary>
|
|
public Image DeviceIcon;
|
|
|
|
/// <summary>
|
|
/// 训练数据
|
|
/// </summary>
|
|
public TrainingDeviceData TrainingDeviceData;
|
|
|
|
public void Init(TrainingDeviceData data, Sprite _sprite)
|
|
{
|
|
TrainingDeviceData = data;
|
|
if (!TrainingDeviceItemList.Contains(this))
|
|
TrainingDeviceItemList.Add(this);
|
|
if (_sprite != null)
|
|
{
|
|
DeviceIcon.sprite = _sprite;
|
|
}
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
//未选设备时禁止选择选项
|
|
if (DeviceItem.SelectedItem == null)
|
|
return;
|
|
|
|
if (SelectedItem != null)
|
|
{
|
|
//已选中再次点击取消选中
|
|
if (SelectedItem == this)
|
|
{
|
|
SelectedItem.SetSelectStatus(false);
|
|
SelectedItem = null;
|
|
}
|
|
else
|
|
{
|
|
SelectedItem.SetSelectStatus(false);
|
|
if (SelectedItem != this)
|
|
SelectedItem.SelectedBackground.gameObject.SetActive(false);
|
|
SelectedItem = this;
|
|
SetSelectStatus(true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
SelectedItem = this;
|
|
SetSelectStatus(true);
|
|
}
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
if (SelectedItem != this)
|
|
SelectedBackground.gameObject.SetActive(true);
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
if (SelectedItem != this)
|
|
SelectedBackground.gameObject.SetActive(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 改变选中状态
|
|
/// </summary>
|
|
/// <param name="selected">是否选中</param>
|
|
/// <param name="force">是否强制执行</param>
|
|
public void SetSelectStatus(bool selected, bool force = false)
|
|
{
|
|
SelectedStatus.gameObject.SetActive(selected);
|
|
if (force)
|
|
{
|
|
SelectedBackground.gameObject.SetActive(selected);
|
|
SelectedItem = selected ? this : null;
|
|
}
|
|
else
|
|
{
|
|
DeviceItem.SelectedItem.DevicesItemData._select_sprite = DeviceIcon.sprite;
|
|
TrainingManager.Instance.UpdateSelectItem(selected ? TrainingDeviceData.id : "");
|
|
}
|
|
}
|
|
}
|