236 lines
7.8 KiB
C#
236 lines
7.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using static TMPro.SpriteAssetUtilities.TexturePacker_JsonArray;
|
|
|
|
public class DragTest : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// 锚点位置,放预制体最下面
|
|
/// </summary>
|
|
private GameObject anchorPos;
|
|
public List<GameObject> listTargets = new List<GameObject>();
|
|
//[HideInInspector]
|
|
public List<GameObject> frames = new List<GameObject>();
|
|
[Header("该设备层数")]
|
|
public int PerTier = 42;
|
|
[Header("该区域设备数")]
|
|
public int PerRow = 5;
|
|
[Header("该物体容积层数")]
|
|
public int volume;
|
|
|
|
public bool isExit = false;
|
|
public bool isClicked = false;
|
|
|
|
public bool isTarget;
|
|
/// <summary>
|
|
/// 是否已经放置
|
|
/// </summary>
|
|
public bool isplace;
|
|
|
|
|
|
public bool canPut;
|
|
bool isray = false;
|
|
bool finishPut;
|
|
private void Start()
|
|
{
|
|
//Debug.Log("版本号 1.1.1");
|
|
GetUList();
|
|
canPut = true;
|
|
anchorPos = transform.Find("锚点").gameObject;
|
|
//finishPut = gameObject.GetComponent<Device>().isPut;
|
|
//Debug.Log("版本号 1.1.1");
|
|
}
|
|
private void Update()
|
|
{
|
|
|
|
RaycastHit hit;
|
|
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 999, 1 << 8 | 1 << 10))
|
|
{
|
|
isray = true;
|
|
}
|
|
else
|
|
{
|
|
isray = false;
|
|
}
|
|
if (finishPut == false)
|
|
{
|
|
if (Input.GetMouseButtonUp(0))
|
|
{
|
|
MouseUp();
|
|
}
|
|
}
|
|
}
|
|
IEnumerator OnMouseOver()
|
|
{
|
|
if (isray)
|
|
{
|
|
//三维物体坐标转屏幕坐标
|
|
Vector3 screenSpace = Camera.main.WorldToScreenPoint(anchorPos.transform.position);
|
|
//将鼠标屏幕坐标转为三维坐标,再计算物体位置与鼠标之间的距离
|
|
var offset = anchorPos.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
|
|
while (Input.GetMouseButton(0) && !finishPut)
|
|
{
|
|
finishPut = false;
|
|
Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
|
|
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
|
|
transform.position = curPosition;
|
|
yield return null;
|
|
}
|
|
}
|
|
|
|
}
|
|
public void MouseUp()
|
|
{
|
|
//if (isray)
|
|
{
|
|
isTarget = false;
|
|
foreach (GameObject item in listTargets)
|
|
{
|
|
if (GetOverGameObject(Camera.main.gameObject).Contains(item))
|
|
{
|
|
//判断当前为第几个子物体
|
|
int rank = listTargets.IndexOf(item) + 1;
|
|
|
|
//移除设备
|
|
//if (item.transform.childCount >= 1)
|
|
//{
|
|
// int v = item.transform.GetComponentInChildren<DragTest>().volume;
|
|
// for (int i = 0; i < v; i++)
|
|
// {
|
|
// listTargets[rank - 1 + i].tag = "Untagged";
|
|
// }
|
|
// Destroy(item.transform.GetChild(0).gameObject);
|
|
// break;
|
|
//}
|
|
|
|
int isarea = 1; int isrow = 1; int istier = 0;
|
|
bool allowPut = true;
|
|
|
|
//计算层、列、区域
|
|
|
|
for (int i = 1; i <= rank; i++)
|
|
{
|
|
if (istier < PerTier)
|
|
{
|
|
istier++;
|
|
}
|
|
else
|
|
{
|
|
if (isrow < PerRow)
|
|
{
|
|
isrow++;
|
|
}
|
|
else
|
|
{
|
|
isarea++;
|
|
isrow = 1;
|
|
}
|
|
istier = 1;
|
|
}
|
|
}
|
|
|
|
//剩下预留层数
|
|
int lower = 0;
|
|
lower = PerTier - istier + 1;
|
|
if (lower >= volume)
|
|
{
|
|
for (int i = 0; i < volume; i++)
|
|
{
|
|
if (listTargets[rank - 1 + i].CompareTag("Isput"))
|
|
{
|
|
allowPut = false;
|
|
Debug.Log("预留空间不足,不能放置该设备");
|
|
break;
|
|
}
|
|
}
|
|
if (allowPut)
|
|
{
|
|
////设备第几个区域
|
|
//gameObject.GetComponent<Device>().mydata.area = isarea.ToString();
|
|
////设备第几列
|
|
//gameObject.GetComponent<Device>().mydata.row = isrow.ToString();
|
|
////设备第几层
|
|
//gameObject.GetComponent<Device>().mydata.tier = istier.ToString();
|
|
Debug.Log("该设备容积为:" + volume);
|
|
Debug.Log("第" + isarea + "区,第" + isrow + "台,第" + istier + "层");
|
|
|
|
transform.position = item.transform.position;
|
|
Collider collider = item.GetComponent<Collider>();
|
|
|
|
//放到锚点位置
|
|
transform.position = new Vector3(anchorPos.transform.position.x, anchorPos.transform.position.y + collider.bounds.size.y / 2, anchorPos.transform.position.z);
|
|
|
|
//设置父物体
|
|
transform.SetParent(item.transform, true);
|
|
|
|
//给使用的格子打上标签
|
|
for (int i = 0; i < volume; i++)
|
|
{
|
|
listTargets[rank - 1 + i].tag = "Isput";
|
|
}
|
|
|
|
|
|
finishPut = true;
|
|
isTarget = true;
|
|
isplace = true;
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("预留空间不足,不能放置该设备");
|
|
}
|
|
|
|
}
|
|
}
|
|
if (isplace == false)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
else if (!isTarget && !isplace)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|
|
//获取光标停留的3D物体
|
|
public List<GameObject> GetOverGameObject(GameObject raycaster)
|
|
{
|
|
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
|
|
pointerEventData.position = Input.mousePosition;
|
|
PhysicsRaycaster pr = raycaster.GetComponent<PhysicsRaycaster>();
|
|
List<RaycastResult> results = new List<RaycastResult>();
|
|
pr.Raycast(pointerEventData, results);
|
|
List<GameObject> listObjs = new List<GameObject>();
|
|
if (results.Count != 0)
|
|
{
|
|
foreach (RaycastResult item in results)
|
|
{
|
|
//if (item.gameObject.layer != 7)
|
|
//位子物体layer层要为6
|
|
//if (item.gameObject.layer != 8) break;
|
|
listObjs.Add(item.gameObject);
|
|
}
|
|
}
|
|
return listObjs;
|
|
}
|
|
/// <summary>
|
|
/// 得到U位数量
|
|
/// </summary>
|
|
public void GetUList()
|
|
{
|
|
for (int i = 0; i < frames.Count; i++)
|
|
{
|
|
for (int j = 0; j < PerTier; j++)
|
|
{
|
|
listTargets.Add(frames[i].transform.GetChild(j).gameObject);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
} |