Fixing serialization problems

This commit is contained in:
Pascal Serrarens 2026-07-08 12:52:19 +02:00
parent d72f15eb66
commit 8afd5e006f
4 changed files with 87 additions and 2 deletions

View File

@ -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();

View File

@ -7,6 +7,7 @@ using static Unity.Mathematics.math;
#endif
using NanoBrain.Unity;
namespace NanoBrain {
/// <summary>
@ -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<NeuronData> neurons = new();
public List<ExternalClusterData> 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;
}
}
}

View File

@ -734,4 +734,26 @@ namespace NanoBrain {
}
}
[Serializable]
public class NeuronData {
public string name;
public Vector3 bias = Vector3.zero;
public Neuron.CombinatorType combinatorType;
public List<SynapseData> 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);
}
}
}
}

View File

@ -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;
}
}
}