forked from zxl/LaboratoryProtection
142 lines
3.7 KiB
C#
142 lines
3.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace UnityTest.ZXL
|
|
{
|
|
[RequireComponent(typeof(BoxCollider), typeof(Rigidbody))]
|
|
public class DragItem : MonoBehaviour, IDragHandler, IPointerClickHandler, IEndDragHandler
|
|
{
|
|
[FormerlySerializedAs("img")] public RectTransform icon;
|
|
|
|
[SerializeField] [ReadOnly] private Vector3 startPosition;
|
|
[SerializeField] [ReadOnly] private bool isCanPut;
|
|
[SerializeField] [ReadOnly] private bool isPutIn;
|
|
|
|
[SerializeField] private DragData _dragData;
|
|
|
|
private void Start()
|
|
{
|
|
var rect = GetComponent<RectTransform>();
|
|
startPosition = rect.anchoredPosition3D;
|
|
// startPosition = icon.position;
|
|
|
|
isPutIn = false;
|
|
GetComponent<Collider>().enabled = true;
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
if (isPutIn)
|
|
return;
|
|
|
|
Debug.Log("OnDrag");
|
|
icon.position = eventData.position;
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
if (isPutIn)
|
|
return;
|
|
|
|
Debug.Log("OnEndDrag");
|
|
TryPut();
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
if (isPutIn)
|
|
return;
|
|
|
|
Debug.Log("OnPointerClick");
|
|
}
|
|
|
|
void TryPut()
|
|
{
|
|
if (target != null && isCanPut)
|
|
{
|
|
// 到达目标点的固定位置
|
|
// icon.position = target.itemPosition;
|
|
// startPosition = target.itemPosition;
|
|
|
|
// 回到原位
|
|
icon.anchoredPosition3D = startPosition;
|
|
|
|
_dragData.PutOrOut(true);
|
|
isPutIn = true;
|
|
}
|
|
else
|
|
{
|
|
icon.anchoredPosition3D = startPosition;
|
|
_dragData.PutOrOut(false);
|
|
}
|
|
}
|
|
|
|
private DragTarget target;
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
Debug.Log($"{transform.parent.name} OnTriggerEnter {other.transform.parent.name}");
|
|
target = other.GetComponent<DragTarget>();
|
|
if (target == null)
|
|
return;
|
|
|
|
isCanPut = true;
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
Debug.Log($"{transform.parent.name} OnTriggerExit {other.transform.parent.name}");
|
|
if (isPutIn)
|
|
{
|
|
Debug.Log($"已经放入");
|
|
return;
|
|
}
|
|
|
|
isCanPut = false;
|
|
target = null;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
[Button]
|
|
void Add()
|
|
{
|
|
var rectTransform = gameObject.GetComponent<RectTransform>();
|
|
var rect = rectTransform.rect;
|
|
var boxCollider = GetComponent<BoxCollider>();
|
|
boxCollider.isTrigger = true;
|
|
boxCollider.size = new Vector3(rect.width, rect.height, 5);
|
|
var rigidbody = GetComponent<Rigidbody>();
|
|
rigidbody.useGravity = true;
|
|
rigidbody.isKinematic = true;
|
|
icon = rectTransform;
|
|
UnityEditor.EditorUtility.SetDirty(this);
|
|
}
|
|
|
|
[Button]
|
|
void SetDirty()
|
|
{
|
|
UnityEditor.EditorUtility.SetDirty(this);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
[System.Serializable]
|
|
public struct DragData
|
|
{
|
|
public string name;
|
|
public CharacterInfo info;
|
|
|
|
public void PutOrOut(bool isPut)
|
|
{
|
|
if (info == null || !isPut)
|
|
return;
|
|
|
|
info.SetCloth(name);
|
|
}
|
|
}
|
|
} |