Add neuron property drawer

This commit is contained in:
Pascal Serrarens 2026-05-06 09:48:21 +02:00
parent 7fb23e9b89
commit 67beb5e486
6 changed files with 108 additions and 12 deletions

View File

@ -19,16 +19,21 @@ namespace CreatureControl {
HomePheromonePrefabInspector();
FoodPheromonePrefabInspector();
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(Ant.targetDirection)));
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(Ant.foodReceptor)));
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(Ant.homeReceptor)));
EditorGUILayout.PropertyField(serializedObject.FindProperty(nameof(Ant.hasFood)));
}
private void HomePheromonePrefabInspector() {
SerializedProperty homePheromonePrefabProp = serializedObject.FindProperty(nameof(Ant.homePheromonePrefab));
homePheromonePrefabProp.objectReferenceValue = (GameObject) EditorGUILayout.ObjectField("Home Pheromone Prefab", homePheromonePrefabProp.objectReferenceValue, typeof(GameObject), true);
homePheromonePrefabProp.objectReferenceValue = (GameObject)EditorGUILayout.ObjectField("Home Pheromone Prefab", homePheromonePrefabProp.objectReferenceValue, typeof(GameObject), true);
}
private void FoodPheromonePrefabInspector() {
SerializedProperty foodPheromonePrefabProp = serializedObject.FindProperty(nameof(Ant.foodPheromonePrefab));
foodPheromonePrefabProp.objectReferenceValue = (GameObject) EditorGUILayout.ObjectField("Food Pheromone Prefab", foodPheromonePrefabProp.objectReferenceValue, typeof(GameObject), true);
foodPheromonePrefabProp.objectReferenceValue = (GameObject)EditorGUILayout.ObjectField("Food Pheromone Prefab", foodPheromonePrefabProp.objectReferenceValue, typeof(GameObject), true);
}
}

View File

@ -0,0 +1,79 @@
using UnityEngine;
using UnityEditor;
using Unity.Mathematics;
using System;
using System.Reflection;
using System.Collections;
namespace NanoBrain {
[CustomPropertyDrawer(typeof(Neuron))]
class NeuronDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
// Draw foldout + properties
label = EditorGUI.BeginProperty(position, label, property);
// Begin indent block
int indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
object instance = GetTargetObjectOfProperty(property);
float lineHeight = EditorGUIUtility.singleLineHeight;
Rect r = new(position.x, position.y, position.width, lineHeight);
if (instance != null) {
FieldInfo field = typeof(Neuron).GetField("_outputValue", BindingFlags.NonPublic | BindingFlags.Instance);
if (field != null) {
float3 val = (float3)field.GetValue(instance);
EditorGUI.Vector3Field(r, $"Neuron: {label}", val);
}
}
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
// height for 1 line
return (EditorGUIUtility.singleLineHeight * 1) + (EditorGUIUtility.standardVerticalSpacing * 0);
}
public static object GetTargetObjectOfProperty(SerializedProperty prop) {
var path = prop.propertyPath.Replace(".Array.data[", "[");
object obj = prop.serializedObject.targetObject;
var elements = path.Split('.');
foreach (var element in elements) {
if (element.Contains("[")) {
var elementName = element.Substring(0, element.IndexOf("["));
var index = Convert.ToInt32(element.Substring(element.IndexOf("[")).Replace("[", "").Replace("]", ""));
obj = GetValue_Imp(obj, elementName, index);
}
else {
obj = GetValue_Imp(obj, element);
}
}
return obj;
}
static object GetValue_Imp(object source, string name) {
if (source == null)
return null;
Type t = source.GetType();
FieldInfo f = t.GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (f != null)
return f.GetValue(source);
PropertyInfo p = t.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
return p?.GetValue(source, null);
}
static object GetValue_Imp(object source, string name, int index) {
if (GetValue_Imp(source, name) is not IEnumerable enumerable)
return null;
IEnumerator en = enumerable.GetEnumerator();
for (int i = 0; i <= index; i++) {
if (!en.MoveNext())
return null;
}
return en.Current;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: aa0e340763ca6299e93d514b271ae38d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -556,14 +556,15 @@ namespace NanoBrain {
return this;
// Find a sleeping cluster
foreach (Cluster cluster in this.siblingClusters) {
if (cluster.defaultOutput.isSleeping) {
RemoveThingCluster(cluster);
return cluster;
}
}
// foreach (Cluster cluster in this.siblingClusters) {
// if (cluster.defaultOutput.isSleeping) {
// RemoveThingCluster(cluster);
// return cluster;
// }
// }
// Otherwise find longest unused cluster
// Find longest unused cluster
// Note this uses the default output...
Cluster unusedCluster = this.siblingClusters[0];
for (int ix = 1; ix < this.siblingClusters.Length; ix++) {
if (this.siblingClusters[ix].defaultOutput.lastUpdate < unusedCluster.defaultOutput.lastUpdate)

View File

@ -285,7 +285,7 @@ namespace NanoBrain {
public Action WhenFiring;
public virtual bool isSleeping => Time.time - this.lastUpdate > this.timeToSleep; //this.outputMagnitude == 0;
public virtual bool isSleeping => false;// Time.time - this.lastUpdate > this.timeToSleep; //this.outputMagnitude == 0;
public void SleepCheck() {
if (this.isSleeping) {
#if UNITY_MATHEMATICS

View File

@ -225,7 +225,7 @@ namespace CreatureControl {
Vector3 smellDirection = this.transform.InverseTransformPoint(nest.transform.position);
float distance = smellDirection.magnitude;
float angle = Vector3.Angle(Vector3.forward, smellDirection);
float angle = Vector3.Angle(Vector3.back, smellDirection);
if (angle < smellAngle && distance > 0.01) {
float intensity = nest.StrengthAt(distance);
Vector3 value = smellDirection.normalized * intensity;