Fixing serialization problems
This commit is contained in:
parent
d72f15eb66
commit
8afd5e006f
@ -24,6 +24,14 @@ namespace NanoBrain.Unity {
|
|||||||
clusterPrefab.cluster.Cleanup();
|
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() {
|
public override void OnInspectorGUI() {
|
||||||
EditorGUI.BeginChangeCheck();
|
EditorGUI.BeginChangeCheck();
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,7 @@ using static Unity.Mathematics.math;
|
|||||||
#endif
|
#endif
|
||||||
using NanoBrain.Unity;
|
using NanoBrain.Unity;
|
||||||
|
|
||||||
|
|
||||||
namespace NanoBrain {
|
namespace NanoBrain {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -773,6 +774,48 @@ namespace NanoBrain {
|
|||||||
this.nuclei.Remove(nucleus);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user