forked from zxl/LaboratoryProtection
add 添加拖拽功能和测试场景,模型待修改
parent
db7c6dd005
commit
3b647e7a5b
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: bde4aeef11512d048ac4632ac788d161
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a41fdb0a846171942b25ba3bfae6f4c0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,142 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 94e1c20b7a14db147b642f9f8ee4ca48
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue