GQ_Communicate/GQ_URP/GQ/Assets/script/设备拖拽/OnChlickDrag1.cs

141 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Vector3 = UnityEngine.Vector3;
[RequireComponent(typeof(UnityEngine.EventSystems.EventTrigger))]
public class OnChlickDrag1 : MonoBehaviour, IPointerDownHandler
{
public GameObject prefab;
private GameObject dragGO;
/// <summary>
/// 货架父物体
/// </summary>
public List<GameObject> framesList = new List<GameObject>();
//private List<GameObject> frames = new List<GameObject>();
/// <summary>
/// 选项Btn;
/// </summary>
public Button SelectBtn;
void OnEnable()
{
//Debug.Log("版本号 1.1.1");
AddCollider(prefab.transform);
//Debug.Log("版本号 1.1.1");
}
private void Start()
{
}
public Transform TT;
//[ContextMenu("TT")]
//public void SetUWei()
//{
// for (int i = 0; i < TT.childCount; i++)
// {
// if (TT.GetChild(i).transform.Find("U位"))
// {
// GameObject o = TT.GetChild(i).transform.Find("U位").gameObject;
// framesList.Add(o);
// }
// }
//}
public void OnPointerDown(PointerEventData eventData)
{
if (dragGO == null)
{
dragGO = Instantiate(prefab);
dragGO.GetComponent<DragTest>().frames = framesList;
//dragGO.GetComponent<DragTest>().GetUList();
dragGO.SetActive(false);
}
}
public void MouseExitBtn()
{
if (dragGO != null)
{
Vector3 mouse = Input.mousePosition;
//TODO通过调整Z调整拖拽物体显示大小
mouse.z = 1;
Vector3 WorldPos = Camera.main.ScreenToWorldPoint(mouse);
dragGO.transform.position = WorldPos;
dragGO.SetActive(true);
dragGO = null;
}
}
void AddCollider(Transform game)
{
BoxCollider boxCollider = game.GetComponent<BoxCollider>();
Renderer renderer = null;
if (!boxCollider)
{
try
{
////复杂设备
//if (game.childCount != 0)
// renderer = game.Find(game.name).GetComponent<Renderer>();
////简单设备
//else
// renderer = game.GetComponent<Renderer>();
try
{
renderer = game.Find(game.name).GetComponent<Renderer>();
}
catch (Exception)
{
renderer = game.GetComponent<Renderer>();
}
if (renderer != null)
{
var initrot = game.rotation;
game.rotation = UnityEngine.Quaternion.identity;
var bounds = renderer.bounds;
var a = game.gameObject.AddComponent<BoxCollider>();
a.isTrigger = false;
a.center = bounds.center - game.transform.position;
//a.center = new Vector3(
// a.center.x * AdjustColliderSize(10, 10, 10).x,
// a.center.y * AdjustColliderSize(10, 10, 10).y,
// a.center.z * AdjustColliderSize(10, 10, 10).z);
a.size = bounds.size;
//a.size = new Vector3(
// a.size.x * AdjustColliderSize(10, 10, 10).x,
// a.size.y * AdjustColliderSize(10, 10, 10).y,
// a.size.z * AdjustColliderSize(10, 10, 10).z);
//a.transform.rotation = game.rotation; //重置其旋转为默认值
game.rotation = initrot;
}
}
catch (Exception e)
{
Debug.Log(game);
}
}
}
}