Files
plugin-library/Assets/11.PointCloudTools/Editor/Script/CreatPointModelWindow.cs

214 lines
7.0 KiB
C#
Raw Normal View History

2025-09-29 18:24:13 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Stary.Evo;
using Stary.Evo.Editor;
using Stary.Evo.Editor.Entity;
using UnityEditor;
using UnityEngine;
public class CreatPointModelWindow : OdinEditorWindow
{
private static List<string> _modelTypes;
public static List<string> ScenarioTypes;
[MenuItem("Evo/创建ModelLibrary")]
private static async void Init()
{
_modelTypes = await GetModelTypes();
ScenarioTypes = await GetScenarioTypes();
// Get existing open window or if none, make a new one:
CreatPointModelWindow window = (CreatPointModelWindow)EditorWindow.GetWindow(typeof(CreatPointModelWindow));
window.Show();
}
protected override void OnEnable()
{
base.OnEnable();
documentFileid = EditorPrefs.GetString("documentFileid");
modelType = EditorPrefs.GetString("modelType");
scenarioType = EditorPrefs.GetString("scenarioType");
}
[TitleGroup("创建")] [InfoBox("Model名称")]
public string modelName;
[TitleGroup("创建")] [InfoBox("Model描述")] [ValueDropdown("GetModelType")][OnValueChanged("SetModelType")]
public string modelType;
[TitleGroup("创建")] [InfoBox("Scenario描述")] [ValueDropdown("GetScenarioType")][OnValueChanged("SetScenarioType")]
public string scenarioType;
[TitleGroup("创建")] [ReadOnly] [InfoBox("文件所对应数据库id")]
public string documentFileid;
private List<string> GetModelType() => _modelTypes;
private List<string> GetScenarioType() => ScenarioTypes;
private void SetModelType() => EditorPrefs.SetString("modelType", modelType);
private void SetScenarioType() => EditorPrefs.SetString("scenarioType", scenarioType);
private static async UniTask<List<string>> GetModelTypes()
{
string ip = EditorPrefs.GetString("ip");
if (string.IsNullOrEmpty(ip))
{
EditorUtility.DisplayDialog("提示", "ip不能为空", "确定");
return null;
}
// var entity = new PointModelEntity
// {
// modelName = modelName,
// modelType = modelType,
// scenarioType = scenarioType,
// };
//string postData = JsonConvert.SerializeObject(entity);
ResultMessageEntity entity = await WebRequestSystem.Get(ip, "/ModelLibrary/FindModelType");
if (entity.code == 200)
{
var modelTypes = JsonConvert.DeserializeObject<List<string>>(entity.data.ToString());
return modelTypes;
}
else
{
return null;
}
}
private static async UniTask<List<string>> GetScenarioTypes()
{
string ip = EditorPrefs.GetString("ip");
if (string.IsNullOrEmpty(ip))
{
EditorUtility.DisplayDialog("提示", "ip不能为空", "确定");
return null;
}
ResultMessageEntity entity = await WebRequestSystem.Get(ip, "/SceneWork/FindScenarioType");
if (entity.code == 200)
{
var scenarioTypes = JsonConvert.DeserializeObject<List<string>>(entity.data.ToString());
return scenarioTypes;
}
else
{
return null;
}
}
[TitleGroup("创建")]
[Button("创建Model", ButtonSizes.Large)]
public async void CreatModel()
{
if (string.IsNullOrEmpty(modelName) || string.IsNullOrEmpty(modelType) || string.IsNullOrEmpty(scenarioType) ||
string.IsNullOrEmpty(documentFileid))
{
EditorUtility.DisplayDialog("提示", "modelName和modelType和scenarioType和documentFileid不能为空", "确定");
return;
}
string ip = EditorPrefs.GetString("ip");
if (string.IsNullOrEmpty(ip))
{
EditorUtility.DisplayDialog("提示", "ip不能为空", "确定");
return;
}
var pointModelEntity = new PointModelEntity
{
Name = modelName,
ModelType = modelType,
ScenarioType = scenarioType,
DocumentFileid = documentFileid,
};
string postData = JsonConvert.SerializeObject(pointModelEntity);
ResultMessageEntity entity = await WebRequestSystem.Post($"{ip}/ModelLibrary/AddModel", postData);
if (entity.code == 200)
{
EditorUtility.DisplayDialog("提示", "创建成功", "确定");
modelName = "";
documentFileid = "";
EditorPrefs.SetString("documentFileid","");
EditorPrefs.SetString("modelType","");
EditorPrefs.SetString("scenarioType","");
}
else if (entity.code == 4009)
{
EditorUtility.DisplayDialog("提示", $"创建失败,存在相同名称元素", "确定");
}
}
[TitleGroup("上传")] [InfoBox("Model路径")] [Sirenix.OdinInspector.FilePath(Extensions = "$modelType")]
public string modelPath;
[TitleGroup("上传")] [ReadOnly][HideLabel, ProgressBar(0, 100f)]
public float modelUpdateInterval = 0f;
[TitleGroup("上传")]
[Button("上传Model", ButtonSizes.Large)]
public async void UpdateModel()
{
if (string.IsNullOrEmpty(modelPath))
{
EditorUtility.DisplayDialog("提示", "modelPath不能为空", "确定");
return;
}
string ip = EditorPrefs.GetString("ip");
if (string.IsNullOrEmpty(ip))
{
EditorUtility.DisplayDialog("提示", "ip不能为空", "确定");
return;
}
var messageEntity = await WebRequestSystem.PostFile(ip + "/FileLoad/UpLoadFile", new[] { modelPath }, (progress) =>
{
modelUpdateInterval = progress * 100f;
});
if (messageEntity.code == 200)
{
List<ResultMessageEntity> resultMessageEntities =
JsonConvert.DeserializeObject<List<ResultMessageEntity>>(messageEntity.data.ToString());
if (resultMessageEntities.Count > 0)
{
foreach (var resultMessageEntity in resultMessageEntities)
{
var data = JsonConvert.DeserializeObject(resultMessageEntity.data.ToString()) as JObject;
documentFileid = data["id"].ToString();
EditorPrefs.SetString("documentFileid", documentFileid);
}
}
}
}
[Serializable]
public class PointModelEntity
{
/// <summary>
/// 模型名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 模型标签
/// </summary>
public string ModelType { get; set; }
/// <summary>
/// 场景标签
/// </summary>
public string ScenarioType { get; set; }
/// <summary>
/// 文件id
/// </summary>
public string DocumentFileid { get; set; }
}
}