172 lines
6.4 KiB
C#
172 lines
6.4 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using MotionFramework;
|
||
using MotionFramework.Scripts.Runtime.Engine.Engine.Network.WebRequest;
|
||
using Newtonsoft.Json;
|
||
using UnityEngine;
|
||
|
||
namespace DefaultNamespace
|
||
{
|
||
/// <summary>
|
||
/// 货架箱子管理器
|
||
/// </summary>
|
||
public class ShelfBoxManager : MonoBehaviour
|
||
{
|
||
public GameObject box;
|
||
public int maxRows = 11;
|
||
public int maxColumns = 34;
|
||
public int maxLayers = 2;
|
||
|
||
private List<GameObject> instantiatedBoxes = new List<GameObject>();
|
||
private void Start()
|
||
{
|
||
string json = @"{
|
||
""code"": 200,
|
||
""type"": ""success"",
|
||
""message"": """",
|
||
""result"": [
|
||
{
|
||
""id"": ""XSCS_01_010100101600100"",
|
||
""type"": ""Buff"",
|
||
""locationId"": ""010100101600100"",
|
||
""description"": ""01%区1层1行16列"",
|
||
""locationState"": ""Normal"",
|
||
""storageState"": ""Load"",
|
||
""layer"": 1,
|
||
""row"": 11,
|
||
""column"": 21,
|
||
""specialFlag"": null,
|
||
""palletNum"": null,
|
||
""itemType"": ""EmptyBox1"",
|
||
""isSpecial"": 0
|
||
},
|
||
{
|
||
""id"": ""XSCS_01_010100101600200"",
|
||
""type"": ""Buff"",
|
||
""locationId"": ""010100101600200"",
|
||
""description"": ""01%区2层1行16列"",
|
||
""locationState"": ""Normal"",
|
||
""storageState"": ""Load"",
|
||
""layer"": 1,
|
||
""row"": 12,
|
||
""column"": 22,
|
||
""specialFlag"": null,
|
||
""palletNum"": null,
|
||
""itemType"": ""EmptyBox1"",
|
||
""isSpecial"": 1
|
||
}
|
||
],
|
||
""extras"": null,
|
||
""time"": ""2024-06-26 19:20:09""
|
||
}";
|
||
|
||
ShelfBoxModel parsedData = JsonConvert.DeserializeObject<ShelfBoxModel>(json);
|
||
|
||
Debug.Log($"Code: {parsedData.code}");
|
||
Debug.Log($"Type: {parsedData.type}");
|
||
Debug.Log($"Time: {parsedData.time}");
|
||
|
||
foreach (var item in parsedData.result)
|
||
{
|
||
string str = $"{item.row}-{item.column}-{item.layer}";
|
||
Debug.Log(str);
|
||
Instantiate(box).transform.SetParent(GameObject.Find(str).transform, false);
|
||
|
||
Debug.Log($"ID: {item.id}, Description: {item.description}, ItemType: {item.itemType}");
|
||
}
|
||
|
||
|
||
GenerateRandomLocations();
|
||
|
||
}
|
||
private void GenerateRandomLocations()
|
||
{
|
||
// 删除之前生成的所有箱子
|
||
foreach (GameObject box in instantiatedBoxes)
|
||
{
|
||
Destroy(box);
|
||
}
|
||
instantiatedBoxes.Clear();
|
||
|
||
// 生成新的位置
|
||
HashSet<(int row, int column, int layer)> positions = new HashSet<(int, int, int)>();
|
||
System.Random rand = new System.Random();
|
||
|
||
// 确保生成唯一位置
|
||
while (positions.Count < 100)
|
||
{
|
||
int row = rand.Next(1, maxRows + 1);
|
||
int column = rand.Next(1, maxColumns + 1);
|
||
int layer = rand.Next(1, maxLayers + 1);
|
||
positions.Add((row, column, layer));
|
||
}
|
||
|
||
foreach (var position in positions)
|
||
{
|
||
GameObject instance = Instantiate(box);
|
||
BoxInfo boxInfo = instance.AddComponent<BoxInfo>(); // Ensure your Box prefab does not already have this component.
|
||
string parentName = $"{position.row}-{position.column}-{position.layer}";
|
||
GameObject parentObject = GameObject.Find(parentName);
|
||
|
||
if (parentObject != null)
|
||
{
|
||
instance.transform.SetParent(parentObject.transform, false);
|
||
}
|
||
else
|
||
{
|
||
Destroy(instance);
|
||
Debug.LogWarning($"未找到名为 {parentName} 的父对象。物体将被放置在原点。");
|
||
}
|
||
// Example data setup for each box
|
||
ShelfBoxeItem data = new ShelfBoxeItem
|
||
{
|
||
id = $"XSCS_01_{position.layer:D2}{position.row:D2}{position.column:D4}00",
|
||
type = "Buff",
|
||
locationId = $"01{position.layer:D2}{position.row:D2}{position.column:D4}00",
|
||
description = $"01%区{position.layer}层{position.row}行{position.column}列",
|
||
locationState = "Normal",
|
||
storageState = "Load",
|
||
layer = position.layer,
|
||
row = position.row,
|
||
column = position.column,
|
||
itemType = "EmptyBox1",
|
||
isSpecial = UnityEngine.Random.Range(0, 2) // Randomly 0 or 1
|
||
};
|
||
boxInfo.SetData(data);
|
||
|
||
instantiatedBoxes.Add(instance);
|
||
}
|
||
}
|
||
|
||
private async void GetBoxData()
|
||
{
|
||
string json = await MotionEngine.GetModule<WebRequestManager>().GetTextAsync(APIs.ShelfBox, null);
|
||
ShelfBoxModel parsedData = JsonConvert.DeserializeObject<ShelfBoxModel>(json);
|
||
|
||
Debug.Log($"代码:{parsedData.code}");
|
||
Debug.Log($"类型:{parsedData.type}");
|
||
Debug.Log($"时间:{parsedData.time}");
|
||
|
||
foreach (var item in parsedData.result)
|
||
{
|
||
Debug.Log($"ID:{item.id},描述:{item.description},物品类型:{item.itemType}");
|
||
}
|
||
}void Update()
|
||
{
|
||
//if (Input.GetMouseButtonDown(0))
|
||
//{
|
||
// Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
// RaycastHit hit;
|
||
|
||
// if (Physics.Raycast(ray, out hit))
|
||
// {
|
||
// BoxInfo hitBox = hit.collider.GetComponent<BoxInfo>();
|
||
// if (hitBox != null)
|
||
// {
|
||
// Debug.Log($"点击的箱子: BoxID--->{hitBox.data.id}, 信息--->{hitBox.data.description}");
|
||
// }
|
||
// }
|
||
//}
|
||
}
|
||
}
|
||
} |