using UnityEngine; using System; using System.Collections.Generic; using System.Linq; using Octopus.Simulator; public class DataManager : UnitySingleton { public bool isPlaying = false; List models = new List(); public event Action> 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(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 GetModels() { return models; } public SimulationModel GetModel(string nodeID) { if (nodeID == null) return null; SimulationModel model = models.Find(x => x.nodeID.Equals(nodeID)); return model; } }