Files
Simulation/Assets/Scripts/Manager/DataManager.cs
2025-07-14 09:35:27 +09:00

61 lines
1.9 KiB
C#

using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using Octopus.Simulator;
public class DataManager : UnitySingleton<DataManager>
{
public bool isPlaying = false;
List<SimulationModel> models = new List<SimulationModel>();
public event Action<List<SimulationModel>> onModelsUpdated;
public void AddModel(SimulationModel model)
{
if (models.Any(x => x == model))
return;
string baseName = model.modelName;
if (string.IsNullOrEmpty(model.objectName))
{
var existingIndices = models
.Where(x => x.objectName.StartsWith(baseName + "_"))
.Select(x =>
{
var parts = x.objectName.Split('_');
int idx;
return (parts.Length > 1 && int.TryParse(parts.Last(), out idx)) ? idx : 0;
});
int nextIndex = existingIndices.Any() ? existingIndices.Max() + 1 : 0;
model.objectName = $"{baseName}_{nextIndex}";
}
model.gameObject.name = model.objectName;
model.onModelClicked += FindAnyObjectByType<Panel_ConnectedObject>(FindObjectsInactive.Include).SetConnectedDataItem;
models.Add(model);
models=models.OrderBy(m => m.objectName).ToList();
onModelsUpdated?.Invoke(models);
}
public void RemoveModel(SimulationModel model)
{
/*
if (models.Any(x => x == model))
return;
*/
models.Remove(model);
models = models.OrderBy(m => m.objectName).ToList();
onModelsUpdated?.Invoke(models);
}
public List<SimulationModel> GetModels()
{
return models;
}
public SimulationModel GetModel(string nodeID)
{
if (nodeID == null) return null;
SimulationModel model = models.Find(x => x.nodeID.Equals(nodeID));
return model;
}
}