From 5aedb30d3623f9c43982e6523a47e8652b8fcf36 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 29 Jan 2026 11:30:33 +0100 Subject: [PATCH] It runs without errors (not working functionaltiy yet) --- Cluster.cs | 16 ++++- ClusterPrefab.cs | 6 ++ Receptor.cs | 16 ++--- Velocity.asset | 62 +++---------------- Velocity.asset.meta | 2 +- VisualEditor/Editor/BrainPickerWindow.cs | 6 +- VisualEditor/Editor/ClusterInspector.cs | 2 +- .../Editor/NanoBrainComponent_Editor.cs | 2 +- VisualEditor/NanoBrainComponent.cs | 10 +-- 9 files changed, 46 insertions(+), 76 deletions(-) diff --git a/Cluster.cs b/Cluster.cs index 44f68ed..255dc66 100644 --- a/Cluster.cs +++ b/Cluster.cs @@ -7,7 +7,8 @@ using static Unity.Mathematics.math; [System.Serializable] public class Cluster : INucleus { // The ScriptableObject asset from which the runtime object has been created - public readonly ClusterPrefab prefab; + [SerializeField] + public ClusterPrefab prefab; public ClusterPrefab cluster { get; set; } @@ -18,9 +19,18 @@ public class Cluster : INucleus { set => _name = value; } + public Cluster(ClusterPrefab prefab) { + this.prefab = UnityEngine.Object.Instantiate(prefab); + this.name = prefab.name; + this.cluster = null; + if (this.cluster != null) + this.cluster.nuclei.Add(this); + } + // Hmm, a cluster instance can never be part of a scriptable object...(Cluster) public Cluster(ClusterPrefab parent, ClusterPrefab prefab) { - this.prefab = prefab; + this.prefab = prefab.Clone(); //UnityEngine.Object.Instantiate(prefab); + this.name = prefab.name; this.cluster = parent; if (this.cluster != null) this.cluster.nuclei.Add(this); @@ -46,6 +56,8 @@ public class Cluster : INucleus { return clone; } + public INucleus output => prefab.output; + // Not sure if this belongs here... [SerializeReference] private NucleusArray _array; diff --git a/ClusterPrefab.cs b/ClusterPrefab.cs index db65b70..151cba5 100644 --- a/ClusterPrefab.cs +++ b/ClusterPrefab.cs @@ -33,6 +33,12 @@ public class ClusterPrefab : ScriptableObject { } } + public virtual ClusterPrefab Clone() { + ClusterPrefab clone = Instantiate(this); + return clone; + } + + // Call this function to ensure that there is at least one nucleus // This is an invariant and should be ensured before the nucleus is used // because output requires it. diff --git a/Receptor.cs b/Receptor.cs index 72ef7df..74053dd 100644 --- a/Receptor.cs +++ b/Receptor.cs @@ -5,7 +5,7 @@ using static Unity.Mathematics.math; public class Receptor : IReceptor { - private ClusterPrefab cluster; + private Cluster cluster; [SerializeField] protected string _name; @@ -14,25 +14,25 @@ public class Receptor : IReceptor { set => _name = value; } - public Receptor(ClusterPrefab cluster) { + public Receptor(Cluster cluster) { this.cluster = cluster; if (cluster != null) - cluster.nuclei.Add(this); + cluster.prefab.nuclei.Add(this); } - public Receptor(ClusterPrefab cluster, INucleus nucleus) { + public Receptor(Cluster cluster, INucleus nucleus) { this.cluster = cluster; if (cluster != null) - cluster.nuclei.Add(this); + cluster.prefab.nuclei.Add(this); this.AddReceiver(nucleus); } - public static Receptor CreateReceptor(ClusterPrefab cluster, string nucleusName) { + public static Receptor CreateReceptor(Cluster cluster, string nucleusName) { if (cluster == null) return null; Receptor receptor = new(cluster); - foreach (INucleus nucleus in cluster.inputs) { + foreach (INucleus nucleus in cluster.prefab.inputs) { if (nucleus != null && nucleus.name == nucleusName) { // Receptor receptor = new(cluster, nucleus); // return receptor; @@ -45,7 +45,7 @@ public class Receptor : IReceptor { return receptor; } - public virtual IReceptor CloneTo(ClusterPrefab parent) { + public virtual IReceptor CloneTo(Cluster parent) { Receptor clone = new(parent); foreach (INucleus receiver in this.receivers) { diff --git a/Velocity.asset b/Velocity.asset index 521ba9b..1528d43 100644 --- a/Velocity.asset +++ b/Velocity.asset @@ -11,26 +11,20 @@ MonoBehaviour: m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: 60a957541c24c57e78018c202ebb1d9b, type: 3} m_Name: Velocity - m_EditorClassIdentifier: Assembly-CSharp::Cluster + m_EditorClassIdentifier: Assembly-CSharp::ClusterPrefab nuclei: - - rid: 2243601403683012671 - - rid: 2243601403683012676 + - rid: 2243601420268863545 references: version: 2 RefIds: - - rid: -2 - type: {class: , ns: , asm: } - - rid: 2243601403683012671 + - rid: 2243601420268863545 type: {class: Neuron, ns: , asm: Assembly-CSharp} data: _name: Output - _synapses: - - nucleus: - rid: -2 - weight: 1 + _synapses: [] _receivers: [] _array: - rid: 2243601403683012672 + rid: 2243601420268863546 _curvePreset: 0 curve: serializedVersion: 2 @@ -58,51 +52,9 @@ MonoBehaviour: m_RotationOrder: 4 curveMax: 1 average: 0 - - rid: 2243601403683012672 + - rid: 2243601420268863546 type: {class: NucleusArray, ns: , asm: Assembly-CSharp} data: _nuclei: - - rid: 2243601403683012671 + - rid: 2243601420268863545 name: Output - - rid: 2243601403683012676 - type: {class: Neuron, ns: , asm: Assembly-CSharp} - data: - _name: Position - _synapses: [] - _receivers: - - rid: 2243601403683012671 - _array: - rid: 2243601403683012677 - _curvePreset: 0 - curve: - serializedVersion: 2 - m_Curve: - - serializedVersion: 3 - time: 0 - value: 0 - inSlope: 0 - outSlope: 1 - tangentMode: 0 - weightedMode: 0 - inWeight: 0 - outWeight: 0 - - serializedVersion: 3 - time: 1000 - value: 1000 - inSlope: 1 - outSlope: 0 - tangentMode: 0 - weightedMode: 0 - inWeight: 0 - outWeight: 0 - m_PreInfinity: 2 - m_PostInfinity: 2 - m_RotationOrder: 4 - curveMax: 1 - average: 0 - - rid: 2243601403683012677 - type: {class: NucleusArray, ns: , asm: Assembly-CSharp} - data: - _nuclei: - - rid: 2243601403683012676 - name: New neuron diff --git a/Velocity.asset.meta b/Velocity.asset.meta index 07ecb98..38684df 100644 --- a/Velocity.asset.meta +++ b/Velocity.asset.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: dd622ac7ed09e70ea8edac595047ac82 +guid: c61aecac62c26de4aaefb2612bcc9a5d NativeFormatImporter: externalObjects: {} mainObjectFileID: 11400000 diff --git a/VisualEditor/Editor/BrainPickerWindow.cs b/VisualEditor/Editor/BrainPickerWindow.cs index 3a536df..503bd10 100644 --- a/VisualEditor/Editor/BrainPickerWindow.cs +++ b/VisualEditor/Editor/BrainPickerWindow.cs @@ -3,14 +3,14 @@ using UnityEngine; using System; using System.Linq; -public class BrainPickerWindow : EditorWindow { +public class ClusterPickerWindow : EditorWindow { private Vector2 scroll; private ClusterPrefab[] items = new ClusterPrefab[0]; private Action onPicked; private string search = ""; public static void ShowPicker(Action onPicked, string title = "Select Cluster") { - var w = CreateInstance(); + var w = CreateInstance(); w.titleContent = new GUIContent(title); w.minSize = new Vector2(360, 320); w.onPicked = onPicked; @@ -21,7 +21,7 @@ public class BrainPickerWindow : EditorWindow { private void OnEnable() => RefreshList(); private void RefreshList() { - var guids = AssetDatabase.FindAssets("t:Cluster"); + var guids = AssetDatabase.FindAssets("t:ClusterPrefab"); items = guids .Select(g => AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(g))) .Where(b => b != null) diff --git a/VisualEditor/Editor/ClusterInspector.cs b/VisualEditor/Editor/ClusterInspector.cs index 94273dd..413b87f 100644 --- a/VisualEditor/Editor/ClusterInspector.cs +++ b/VisualEditor/Editor/ClusterInspector.cs @@ -617,7 +617,7 @@ public class ClusterInspector : Editor { } protected virtual void AddCluster(INucleus nucleus) { - BrainPickerWindow.ShowPicker(brain => OnClusterPicked(nucleus, brain), "Select Cluster"); + ClusterPickerWindow.ShowPicker(brain => OnClusterPicked(nucleus, brain), "Select Cluster"); } private void OnClusterPicked(INucleus nucleus, ClusterPrefab subCluster) { diff --git a/VisualEditor/Editor/NanoBrainComponent_Editor.cs b/VisualEditor/Editor/NanoBrainComponent_Editor.cs index 253993a..6e60376 100644 --- a/VisualEditor/Editor/NanoBrainComponent_Editor.cs +++ b/VisualEditor/Editor/NanoBrainComponent_Editor.cs @@ -23,7 +23,7 @@ public class NanoBrainComponent_Editor : Editor { public override VisualElement CreateInspectorGUI() { //NanoBrainComponent component = target as NanoBrainComponent; - ClusterPrefab brain = Application.isPlaying ? component.brain : component.defaultBrain; + ClusterPrefab brain = Application.isPlaying ? component.brain.prefab : component.defaultBrain; if (Application.isPlaying == false) serializedObject.Update(); diff --git a/VisualEditor/NanoBrainComponent.cs b/VisualEditor/NanoBrainComponent.cs index 56c9159..d5b39c9 100644 --- a/VisualEditor/NanoBrainComponent.cs +++ b/VisualEditor/NanoBrainComponent.cs @@ -2,13 +2,13 @@ using UnityEngine; public class NanoBrainComponent : MonoBehaviour { public ClusterPrefab defaultBrain; - private ClusterPrefab brainInstance; + private Cluster brainInstance; - public INucleus root => brainInstance.output; - public ClusterPrefab brain { + //public INucleus root => brainInstance.output; + public Cluster brain { get { if (brainInstance == null && defaultBrain != null) { - brainInstance = Instantiate(defaultBrain); + brainInstance = new Cluster(defaultBrain); //Instantiate(defaultBrain); brainInstance.name = defaultBrain.name + " (Instance)"; SwarmControl sc = FindFirstObjectByType(); @@ -23,7 +23,7 @@ public class NanoBrainComponent : MonoBehaviour { } } - public static void UpdateWeight(ClusterPrefab brain, string name, float weight) { + public static void UpdateWeight(Cluster brain, string name, float weight) { INucleus root = brain.output; foreach (Synapse synapse in root.synapses) { if (synapse.nucleus.name == name) {