Added: Unity Behavior Graph asset for behaviour demo

This commit is contained in:
2026-11-12 18:54:45 +01:00
parent a9a29ac7c8
commit 7bd50affc9
32 changed files with 4163 additions and 7 deletions
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 299decc0c1c84f348d67c778592f87bf
timeCreated: 1794502155
+10
View File
@@ -0,0 +1,10 @@
namespace AI.BehaviourTree
{
public abstract class Action : Node
{
protected Action()
{
children = null;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f74fc08720e6434d99bf774b6a6f3b09
timeCreated: 1794503810
@@ -0,0 +1,30 @@
using UnityEngine;
namespace AI.BehaviourTree
{
public class BehaviourTree : MonoBehaviour
{
private Node rootNode;
private void OnEnable()
{
NodeStatus status = rootNode.Start();
Debug.Log("Behaviour tree started");
Debug.Log($"Behaviour tree start root node status: {status}");
if (status != NodeStatus.Running) enabled = false;
}
private void OnDisable()
{
Debug.Log("Behaviour tree stopped");
rootNode.Stop();
}
private void Update()
{
NodeStatus status = rootNode.Execute();
Debug.Log($"Behaviour tree execute root node status: {status}");
if (status != NodeStatus.Running) enabled = false;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b2377a84a8744e51ad63470db235b306
timeCreated: 1794502220
@@ -0,0 +1,10 @@
namespace AI.BehaviourTree
{
public abstract class Composite : Node
{
protected Composite(params Node[] children)
{
this.children.AddRange(children);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f3fc40a957b743bbb38e5fc6ac76431a
timeCreated: 1794503869
@@ -0,0 +1,7 @@
namespace AI.BehaviourTree
{
public abstract class Condition : Action
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bb50593562d3477d9c9b37ff6c40c654
timeCreated: 1794503879
@@ -0,0 +1,10 @@
namespace AI.BehaviourTree
{
public abstract class Decorator : Node
{
protected Decorator(Node child)
{
this.children.Add(child);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0a8feb2ab491450fa977c32f0262f949
timeCreated: 1794503911
+23
View File
@@ -0,0 +1,23 @@
using System.Collections.Generic;
namespace AI.BehaviourTree
{
public abstract class Node
{
protected List<Node> children = new List<Node>();
public virtual NodeStatus Start() => NodeStatus.Running;
public virtual NodeStatus Execute() => NodeStatus.Success;
public virtual void Stop()
{
}
}
public enum NodeStatus
{
Running,
Success,
Failure
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 53b501c551ff46848aaadb57d7118a3e
timeCreated: 1794502273
@@ -0,0 +1,7 @@
namespace AI.BehaviourTree
{
public class Selector : Composite
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 025f7db0aae240698cce07fb6743c2d9
timeCreated: 1794503925
@@ -0,0 +1,67 @@
namespace AI.BehaviourTree
{
public class Sequence : Composite
{
Node _currentChild;
int _currentChildIndex = 0;
public Sequence(params Node[] children) : base(children)
{
}
public override NodeStatus Start()
{
if (children.Count == 0) return NodeStatus.Failure;
_currentChildIndex = 0;
_currentChild = children[_currentChildIndex];
// if status == NodeStatus.Failure => whole sequence fails => return NodeStatus.Failure;
// if status == NodeStatus.Success => run the next child
do
{
NodeStatus status = _currentChild?.Start() ?? NodeStatus.Failure;
if (status == NodeStatus.Failure) return NodeStatus.Failure;
if (status == NodeStatus.Running) return NodeStatus.Running;
} while (RunNextChild());
return NodeStatus.Success;
}
public override NodeStatus Execute()
{
NodeStatus status = _currentChild?.Execute() ?? NodeStatus.Failure;
if (status == NodeStatus.Failure) return NodeStatus.Failure;
if (status == NodeStatus.Running) return NodeStatus.Running;
while (RunNextChild())
{
status = _currentChild?.Start() ?? NodeStatus.Failure;
if (status == NodeStatus.Failure) return NodeStatus.Failure;
if (status == NodeStatus.Running) return NodeStatus.Running;
}
return NodeStatus.Success;
}
public override void Stop()
{
_currentChild?.Stop();
}
private bool RunNextChild()
{
_currentChild?.Stop();
_currentChild = null;
_currentChildIndex++;
if (_currentChildIndex < children.Count)
{
_currentChild = children[_currentChildIndex];
return true;
}
return false;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 71354184e3e64c428a329b5c493b8cc9
timeCreated: 1794503979
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 61b9621051732437db91df5e92e80098
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,56 @@
using System;
using Core;
using Unity.Behavior;
using UnityEngine;
using Action = Unity.Behavior.Action;
using Unity.Properties;
[Serializable, GeneratePropertyBag]
[NodeDescription(name: "FindByRaycastAction", story: "Find Player [Target] by raycast from [Self]", category: "Action",
id: "0211c365022a481bc2c40a1c93ef9f2b")]
public partial class FindByRaycastAction : Action
{
[SerializeReference] public BlackboardVariable<GameObject> Target;
[SerializeReference] public BlackboardVariable<GameObject> Self;
[SerializeReference] public BlackboardVariable<float> MaxDistance = new(10.0f);
[SerializeReference] public BlackboardVariable<Boolean> IsFound = new(false);
protected override Status OnStart()
{
// initialize the node
return Status.Running;
}
protected override Status OnUpdate()
{
// will be called every frame until the node returns a status not running
Target.Value = null;
IsFound.Value = false;
RaycastHit hit;
if (!Physics.Raycast(Self.Value.transform.position, Self.Value.transform.forward, out hit,
MaxDistance.Value))
{
return Status.Failure;
}
// return hit.collider.gameObject == Target.Value ? Status.Success : Status.Failure;
PlayerController playerController = hit.collider.gameObject.GetComponent<PlayerController>();
if (playerController == null)
{
return Status.Failure;
}
Target.Value = playerController.gameObject;
IsFound.Value = true;
return Status.Success;
}
protected override void OnEnd()
{
// deinitialize the node
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 1561f6f216f4a46f9b1cccf66f577ed8
@@ -0,0 +1,27 @@
using System;
using Unity.Behavior;
using UnityEngine;
[Serializable, Unity.Properties.GeneratePropertyBag]
[Condition(name: "GameObjectNullCondition", story: "[Target] [is] null", category: "Conditions", id: "6766cb3cdf20628407006d946fe46a8b")]
public partial class GameObjectNullCondition : Condition
{
[SerializeReference] public BlackboardVariable<GameObject> Target;
[Comparison(comparisonType: ComparisonType.Boolean)]
[SerializeReference] public BlackboardVariable<ConditionOperator> Is;
public override bool IsTrue()
{
if (Target.Value == null && Is.Value == ConditionOperator.Equal) return true;
if (Target.Value != null && Is.Value == ConditionOperator.NotEqual) return true;
return false;
}
public override void OnStart()
{
}
public override void OnEnd()
{
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5c7760d815f8b4823b6cae55d87fc5b8