add unity behavior tree package & examples

add start of own implementation of a behavior tree
This commit is contained in:
2025-04-23 12:33:28 +02:00
parent 8a01366c35
commit d9fb5ee7bc
19 changed files with 232 additions and 17 deletions
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2c6bdc0b9db0433e939c2e77844106f1
timeCreated: 1745402938
+9
View File
@@ -0,0 +1,9 @@
namespace DefaultNamespace.BehaviorTree
{
public abstract class Action : Node
{
public Action()
{
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d658db48a0944051aa2a4a2fa02e089c
timeCreated: 1745403146
@@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace DefaultNamespace.BehaviorTree
{
public class Composite : Node
{
public Composite(List<Node> nodes)
{
_nodes.AddRange(nodes);
}
public Composite(params Node[] nodes)
{
_nodes.AddRange(nodes);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 975b1d6c26eb459d9a8cdd3e042b6674
timeCreated: 1745403206
@@ -0,0 +1,7 @@
namespace DefaultNamespace.BehaviorTree
{
public abstract class Condition : Action
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a2d064fe5d964df7aa50ae36e8551561
timeCreated: 1745403415
@@ -0,0 +1,19 @@
namespace DefaultNamespace.BehaviorTree
{
public class Decorator : Node
{
public Node Node
{
get
{
if (_nodes != null && _nodes.Count > 0) return _nodes[0];
return null;
}
}
public Decorator(Node node)
{
_nodes.Add(node);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f064dd30dc4940009b32f253d2f6ec34
timeCreated: 1745403277
+31
View File
@@ -0,0 +1,31 @@
using System.Collections.Generic;
namespace DefaultNamespace.BehaviorTree
{
public abstract class Node
{
protected List<Node> _nodes = new();
public virtual void Start()
{
}
public virtual Status Update()
{
return Status.Success;
}
public virtual void End()
{
}
}
public enum Status
{
Success,
Failure,
Running,
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bb191187e8a246b29358c9eca60e18ea
timeCreated: 1745403053
@@ -0,0 +1,42 @@
using System;
namespace DefaultNamespace.BehaviorTree
{
public class Sequence : Composite
{
protected int _currentIndex;
protected Node _currentNode;
public override void Start()
{
_currentIndex = 0;
_currentNode = _currentIndex >= _nodes.Count ? null : _nodes[_currentIndex];
_currentNode?.Start();
}
public override Status Update()
{
if (_currentNode == null) return Status.Failure;
Status status = _currentNode.Update();
switch (status)
{
case Status.Success:
_currentNode.End();
_currentIndex++;
if (_currentIndex >= _nodes.Count) return Status.Success;
_currentNode = _nodes[_currentIndex];
_currentNode.Start();
return Status.Running;
case Status.Failure:
_currentNode.End();
return Status.Failure;
case Status.Running:
return Status.Running;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 39476da0e0a54e429221ed45a8c2cf0f
timeCreated: 1745403452
+28
View File
@@ -0,0 +1,28 @@
using System;
using UnityEngine;
namespace DefaultNamespace.BehaviorTree
{
public class Tree : MonoBehaviour
{
private Node _rootNode;
private void OnEnable()
{
if (_rootNode == null) return;
_rootNode.Start();
}
private void Update()
{
if (_rootNode == null) return;
_rootNode.Update();
}
private void OnDisable()
{
if (_rootNode == null) return;
_rootNode.End();
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1de9a2c205bb40fb9770d0639fb89abe
timeCreated: 1745402959
+5 -4
View File
@@ -1,10 +1,11 @@
{
"dependencies": {
"com.unity.ai.navigation": "2.0.6",
"com.unity.ai.navigation": "2.0.7",
"com.unity.behavior": "1.0.9",
"com.unity.collab-proxy": "2.7.1",
"com.unity.ide.rider": "3.0.31",
"com.unity.ide.visualstudio": "2.0.22",
"com.unity.inputsystem": "1.13.1",
"com.unity.ide.rider": "3.0.35",
"com.unity.ide.visualstudio": "2.0.23",
"com.unity.inputsystem": "1.14.0",
"com.unity.multiplayer.center": "1.0.0",
"com.unity.render-pipelines.universal": "17.0.4",
"com.unity.test-framework": "1.5.1",
+47 -11
View File
@@ -1,7 +1,7 @@
{
"dependencies": {
"com.unity.ai.navigation": {
"version": "2.0.6",
"version": "2.0.7",
"depth": 0,
"source": "registry",
"dependencies": {
@@ -9,9 +9,26 @@
},
"url": "https://packages.unity.com"
},
"com.unity.behavior": {
"version": "1.0.9",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.burst": "1.7.2",
"com.unity.dt.app-ui": "2.0.0-pre.16",
"com.unity.modules.ai": "1.0.0",
"com.unity.collections": "2.1.4",
"com.unity.modules.audio": "1.0.0",
"com.unity.modules.animation": "1.0.0",
"com.unity.nuget.newtonsoft-json": "3.2.1",
"com.unity.modules.particlesystem": "1.0.0",
"com.unity.modules.unitywebrequest": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.burst": {
"version": "1.8.19",
"depth": 2,
"version": "1.8.21",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.mathematics": "1.2.1",
@@ -28,7 +45,7 @@
},
"com.unity.collections": {
"version": "2.5.1",
"depth": 2,
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.burst": "1.8.17",
@@ -38,6 +55,18 @@
},
"url": "https://packages.unity.com"
},
"com.unity.dt.app-ui": {
"version": "2.0.0-pre.16",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.modules.physics": "1.0.0",
"com.unity.modules.androidjni": "1.0.0",
"com.unity.modules.uielements": "1.0.0",
"com.unity.modules.screencapture": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.ext.nunit": {
"version": "2.0.5",
"depth": 1,
@@ -45,7 +74,7 @@
"dependencies": {}
},
"com.unity.ide.rider": {
"version": "3.0.31",
"version": "3.0.35",
"depth": 0,
"source": "registry",
"dependencies": {
@@ -54,7 +83,7 @@
"url": "https://packages.unity.com"
},
"com.unity.ide.visualstudio": {
"version": "2.0.22",
"version": "2.0.23",
"depth": 0,
"source": "registry",
"dependencies": {
@@ -63,7 +92,7 @@
"url": "https://packages.unity.com"
},
"com.unity.inputsystem": {
"version": "1.13.1",
"version": "1.14.0",
"depth": 0,
"source": "registry",
"dependencies": {
@@ -88,7 +117,14 @@
},
"com.unity.nuget.mono-cecil": {
"version": "1.11.4",
"depth": 3,
"depth": 2,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.nuget.newtonsoft-json": {
"version": "3.2.1",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
@@ -179,11 +215,11 @@
}
},
"com.unity.test-framework.performance": {
"version": "3.0.3",
"depth": 3,
"version": "3.1.0",
"depth": 2,
"source": "registry",
"dependencies": {
"com.unity.test-framework": "1.1.31",
"com.unity.test-framework": "1.1.33",
"com.unity.modules.jsonserialize": "1.0.0"
},
"url": "https://packages.unity.com"
@@ -9,5 +9,6 @@ EditorBuildSettings:
path: Assets/Scenes/SampleScene.unity
guid: 99c9720ab356a0642a771bea13969a05
m_configObjects:
com.unity.dt.app-ui: {fileID: 11400000, guid: 99f9c9493070a9d4c979a8fec7c5a8d3, type: 2}
com.unity.input.settings.actions: {fileID: -944628639613478452, guid: 052faaac586de48259a63d0c4782560b, type: 3}
m_UseUCBPForAssetBundles: 0
+2 -2
View File
@@ -1,2 +1,2 @@
m_EditorVersion: 6000.0.44f1
m_EditorVersionWithRevision: 6000.0.44f1 (101c91f3a8fb)
m_EditorVersion: 6000.0.47f1
m_EditorVersionWithRevision: 6000.0.47f1 (2ad1ed33fd3b)