Added Behavior Graph asset for Unity

This commit is contained in:
2026-04-28 12:35:51 +02:00
parent a3c7859b18
commit 2b213f359b
34 changed files with 5079 additions and 9 deletions
+7
View File
@@ -0,0 +1,7 @@
namespace BehaviorTree
{
public abstract class Action : Node
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f3ab3ddc36e243b2be90aa9b5509e227
timeCreated: 1777370370
@@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace BehaviorTree
{
public abstract class Composite : Node
{
protected List<Node> nodes = new List<Node>();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 14d4eb6b8f8d457a8337c28efd869803
timeCreated: 1777370399
@@ -0,0 +1,7 @@
namespace BehaviorTree
{
public abstract class Condition : Node
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 802a61204be646c99182eba809e9aaf6
timeCreated: 1777370380
@@ -0,0 +1,8 @@
namespace BehaviorTree
{
public abstract class Decorator : Node
{
protected Node node = null;
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 14a7012970af4403878ef7499c89a656
timeCreated: 1777370408
+29
View File
@@ -0,0 +1,29 @@
using System.Collections.Generic;
namespace BehaviorTree
{
public abstract class Node
{
public virtual Status Start()
{
return Status.Running;
}
public virtual Status End()
{
return Status.Success;
}
public virtual Status Update()
{
return Status.Running;
}
}
public enum Status
{
Success,
Failure,
Running
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d82e920d170a41e7b3c88051227b2b87
timeCreated: 1777370362
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
namespace BehaviorTree
{
public class Selector : Composite
{
private int _currentIndex;
private Node _currentNode;
public Selector(List<Node> _nodes)
{
nodes = _nodes;
}
public override Status Start()
{
if (nodes == null || nodes.Count == 0)
{
return Status.Failure;
}
_currentIndex = 0;
_currentNode = nodes[_currentIndex];
_currentNode.Start();
return Status.Running;
}
public override Status Update()
{
Status status = _currentNode?.Update() ?? Status.Failure;
switch (status)
{
case Status.Failure:
if (_currentNode?.End() == Status.Failure) return Status.Failure;
_currentIndex++;
if (_currentIndex >= nodes.Count) return Status.Failure;
_currentNode = nodes[_currentIndex];
if (_currentNode?.Start() == Status.Failure) return Status.Failure;
return Status.Running;
case Status.Success:
_currentNode?.End();
return Status.Success;
case Status.Running:
return Status.Running;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6cdc8db4acb84507990e59ccd03bf1a3
timeCreated: 1777370428
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
namespace BehaviorTree
{
public class Sequence : Composite
{
private int _currentIndex;
private Node _currentNode;
public Sequence(List<Node> _nodes)
{
nodes = _nodes;
}
public override Status Start()
{
if (nodes == null || nodes.Count == 0)
{
return Status.Failure;
}
_currentIndex = 0;
_currentNode = nodes[_currentIndex];
_currentNode.Start();
return Status.Running;
}
public override Status Update()
{
Status status = _currentNode?.Update() ?? Status.Failure;
switch (status)
{
case Status.Success:
if (_currentNode?.End() == Status.Failure) return Status.Failure;
_currentIndex++;
if (_currentIndex >= nodes.Count) return Status.Success;
_currentNode = nodes[_currentIndex];
if (_currentNode?.Start() == Status.Failure) return Status.Failure;
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: a7965f6ffc804719afe03c1ea8ed1f1d
timeCreated: 1777370418
+66
View File
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace BehaviorTree
{
public class Tree : MonoBehaviour
{
private Node _rootNode;
private void OnEnable()
{
_rootNode = new Selector(new List<Node>{});
Status status = _rootNode.Start();
Debug.Log(status);
switch (status)
{
case Status.Success:
break;
case Status.Failure:
break;
case Status.Running:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private void OnDisable()
{
Status status = _rootNode?.End() ?? Status.Failure;
switch (status)
{
case Status.Success:
break;
case Status.Failure:
break;
case Status.Running:
break;
default:
throw new ArgumentOutOfRangeException();
}
_rootNode = null;
}
private void Update()
{
Status status = _rootNode?.Update() ?? Status.Failure;
switch (status)
{
case Status.Success:
break;
case Status.Failure:
break;
case Status.Running:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 48d4d2eb93f743c0821902732350a1e5
timeCreated: 1777370351