From 8afd5e006fa3e8509fa5bcf7b8e97507e41a5e16 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 8 Jul 2026 12:52:19 +0200 Subject: [PATCH] Fixing serialization problems --- Editor/ClusterEditor.cs | 8 ++++++ Runtime/Scripts/Core/Cluster.cs | 43 +++++++++++++++++++++++++++++++++ Runtime/Scripts/Core/Neuron.cs | 26 ++++++++++++++++++-- Runtime/Scripts/Core/Synapse.cs | 12 +++++++++ 4 files changed, 87 insertions(+), 2 deletions(-) diff --git a/Editor/ClusterEditor.cs b/Editor/ClusterEditor.cs index 7cc4f44..e42dc95 100644 --- a/Editor/ClusterEditor.cs +++ b/Editor/ClusterEditor.cs @@ -24,6 +24,14 @@ namespace NanoBrain.Unity { clusterPrefab.cluster.Cleanup(); } + void OnDisable() { + string assetPath = AssetDatabase.GetAssetPath(clusterPrefab); + string folder = System.IO.Path.GetDirectoryName(assetPath); + string jsonPath = System.IO.Path.Combine(folder, clusterPrefab.name + ".json"); + + clusterPrefab.cluster.Export(jsonPath); + } + public override void OnInspectorGUI() { EditorGUI.BeginChangeCheck(); diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index e706e83..e577fbf 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -7,6 +7,7 @@ using static Unity.Mathematics.math; #endif using NanoBrain.Unity; + namespace NanoBrain { /// @@ -773,6 +774,48 @@ namespace NanoBrain { this.nuclei.Remove(nucleus); } } + + #region Serialization + + public void Export(string path) { + ClusterData data = new(this); //this.ToJSON(); + string json = JsonUtility.ToJson(data, prettyPrint: true); + + Debug.Log($"Exporting json to {path}"); + System.IO.File.WriteAllText(path, json); + } + + #endregion } + [Serializable] + public class ClusterData { + public string name; + public List neurons = new(); + public List clusters = new(); + + public ClusterData(Cluster cluster) { + this.name = cluster.name; + foreach (Nucleus nucleus in cluster.nuclei) { + if (nucleus is Neuron neuron) { + NeuronData neuronData = new(neuron); + this.neurons.Add(neuronData); + } else if (nucleus is Cluster extCluster) { + ExternalClusterData clusterData = new (extCluster); + this.clusters.Add(clusterData); + } + } + } + } + + [Serializable] + public class ExternalClusterData { + public string name; + public uint instanceCount; + + public ExternalClusterData(Cluster cluster) { + this.name = cluster.name; + this.instanceCount = (uint)cluster.instanceCount; + } + } } \ No newline at end of file diff --git a/Runtime/Scripts/Core/Neuron.cs b/Runtime/Scripts/Core/Neuron.cs index d9fc41b..921de93 100644 --- a/Runtime/Scripts/Core/Neuron.cs +++ b/Runtime/Scripts/Core/Neuron.cs @@ -92,7 +92,7 @@ namespace NanoBrain { Neuron sender = this.parent.GetNeuron(senderNeuronName); if (sender == null) return null; - + return this.GetSynapse(sender); } @@ -734,4 +734,26 @@ namespace NanoBrain { } } -} \ No newline at end of file + [Serializable] + public class NeuronData { + public string name; + public Vector3 bias = Vector3.zero; + public Neuron.CombinatorType combinatorType; + public List synapses = new(); + public Neuron.ActivationType activationType; + + public NeuronData(Neuron neuron) { + this.name = neuron.name; + this.bias = neuron.bias; + this.combinatorType = neuron.combinator; + this.activationType = neuron.activator; + + foreach (Synapse synapse in neuron.synapses) { + SynapseData synapseData = new(synapse); + this.synapses.Add(synapseData); + } + } + + } + +} diff --git a/Runtime/Scripts/Core/Synapse.cs b/Runtime/Scripts/Core/Synapse.cs index 9116e12..0cddc88 100644 --- a/Runtime/Scripts/Core/Synapse.cs +++ b/Runtime/Scripts/Core/Synapse.cs @@ -103,4 +103,16 @@ namespace NanoBrain { } } + [Serializable] + public class SynapseData { + public string clusterName; + public string neuronName; + public float weight; + + public SynapseData(Synapse synapse) { + this.clusterName = synapse.neuron.parent.name; + this.neuronName = synapse.neuron.name; + this.weight = synapse.weight; + } + } } \ No newline at end of file