Added Behavior Graph asset for AI simulations

This commit is contained in:
2026-04-28 18:33:07 +02:00
parent 8b1a42ed9d
commit 7e2030155c
34 changed files with 4361 additions and 8 deletions
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4f28c48bd266493dbbd2138cfe7512a9
timeCreated: 1777392372
+7
View File
@@ -0,0 +1,7 @@
namespace DefaultNamespace.BehaviorTree
{
public abstract class Action : Node
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a47380a9d61c417fae2871ebb4af4ca9
timeCreated: 1777392989
@@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace DefaultNamespace.BehaviorTree
{
public abstract class Composite : Node
{
protected List<Node> Children;
public Composite(List<Node> children)
{
Children = children;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 03f624aecf9b4f06907bc2d216d810a7
timeCreated: 1777393016
@@ -0,0 +1,7 @@
namespace DefaultNamespace.BehaviorTree
{
public abstract class Condition : Action
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4a21ed8dbd354365a64b54f680885692
timeCreated: 1777393004
@@ -0,0 +1,12 @@
namespace DefaultNamespace.BehaviorTree
{
public abstract class Decorator : Node
{
protected Node Child;
public Decorator(Node child)
{
Child = child;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 907a63b689ce467d9b544d2cfeb23b50
timeCreated: 1777393029
+15
View File
@@ -0,0 +1,15 @@
namespace DefaultNamespace.BehaviorTree
{
public abstract class Node
{
public virtual Status OnStart() => Status.Running;
public virtual void OnEnd()
{
}
public virtual Status OnUpdate() => Status.Success;
}
public enum Status { Success, Failure, Running }
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 777a49e04b5645c69db83417be0665a5
timeCreated: 1777392427
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
namespace DefaultNamespace.BehaviorTree
{
public class Selector : Composite
{
private int _currentIndex;
private Node _currentNode;
public Selector(List<Node> children) : base(children)
{
}
public override Status OnStart()
{
if (Children.Count == 0) return Status.Failure;
_currentIndex = 0;
_currentNode = Children[_currentIndex];
return _currentNode?.OnStart() ?? Status.Failure;
}
public override Status OnUpdate()
{
Status status = _currentNode?.OnUpdate() ?? Status.Failure;
switch (status)
{
case Status.Failure:
_currentNode?.OnEnd();
_currentIndex++;
if (_currentIndex >= Children.Count) return Status.Failure;
_currentNode = Children[_currentIndex];
return _currentNode?.OnStart() ?? Status.Failure;
case Status.Success:
_currentNode?.OnEnd();
return Status.Success;
case Status.Running:
return Status.Running;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d6125671572a4bdca9faf360d9631007
timeCreated: 1777393713
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
namespace DefaultNamespace.BehaviorTree
{
public class Sequence : Composite
{
private int _currentIndex;
private Node _currentNode;
public Sequence(List<Node> children) : base(children)
{
}
public override Status OnStart()
{
if (Children.Count == 0) return Status.Failure;
_currentIndex = 0;
_currentNode = Children[_currentIndex];
return _currentNode?.OnStart() ?? Status.Failure;
}
public override Status OnUpdate()
{
Status status = _currentNode?.OnUpdate() ?? Status.Failure;
switch (status)
{
case Status.Success:
_currentNode?.OnEnd();
_currentIndex++;
if (_currentIndex >= Children.Count) return Status.Success;
_currentNode = Children[_currentIndex];
return _currentNode?.OnStart() ?? Status.Failure;
case Status.Failure:
_currentNode?.OnEnd();
return Status.Failure;
case Status.Running:
return Status.Running;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d710d34ec35547bda535ffd8af159ab1
timeCreated: 1777393258
+46
View File
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace DefaultNamespace.BehaviorTree
{
public class Tree : MonoBehaviour
{
private Node _rootNode;
// may be you want to run this tree anyways without looking for
// the current state, then throw the ifs away
private void OnEnable()
{
// create first node
_rootNode = new Sequence(new List<Node>());
//if (_rootNode.OnStart() == Status.Failure) enabled = false;
Debug.Log($"Root node status at start: {_rootNode.OnStart()}");
}
private void OnDisable()
{
_rootNode.OnEnd();
}
private void Update()
{
// Status status = _rootNode.OnUpdate();
//
// switch (status)
// {
// case Status.Success:
// break;
// case Status.Failure:
// break;
// case Status.Running:
// break;
// default:
// throw new ArgumentOutOfRangeException();
// }
Debug.Log($"Root node status at update: {_rootNode.OnUpdate()}");
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e3ebc182a7db40659155500d45db62e1
timeCreated: 1777392380
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e5ef39e2e4dfb470ab8296731bacdbfc
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,46 @@
using System;
using DefaultNamespace;
using Unity.Behavior;
using UnityEngine;
using Action = Unity.Behavior.Action;
using Unity.Properties;
[Serializable, GeneratePropertyBag]
[NodeDescription(name: "Find by Raycast", story: "Find [Target] by raycast from [Agent]", category: "Action/Find", id: "cde581f97d96148705ed557e6d9ef0c0")]
public partial class FindByRaycastAction : Action
{
[SerializeReference] public BlackboardVariable<GameObject> Target;
[SerializeReference] public BlackboardVariable<GameObject> Agent;
[SerializeReference] public BlackboardVariable<float> MaxDistance = new(10.0f);
protected override Status OnStart()
{
// initialize the node
// check variables first
if (Agent.Value == null) return Status.Failure;
return Status.Running;
}
protected override Status OnUpdate()
{
// will be called every frame while the node is running
Target.Value = null;
RaycastHit hit;
if (!Physics.Raycast(Agent.Value.transform.position, Agent.Value.transform.forward, out hit, MaxDistance)) return Status.Failure;
PlayerController playerController = hit.collider.GetComponent<PlayerController>();
if (!playerController) return Status.Failure;
Target.Value = playerController.gameObject;
return Status.Success;
}
protected override void OnEnd()
{
// deinitialize the node
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 050a9dc7e793c41e8b67a8485bdc6755
@@ -0,0 +1,27 @@
using System;
using Unity.Behavior;
using UnityEngine;
[Serializable, Unity.Properties.GeneratePropertyBag]
[Condition(name: "GameObject Null", story: "[Target] [is] null", category: "Conditions", id: "6bc7c2e1cf9d2d4a14b5c3ac1b47da1d")]
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: 2de8640b1b5a34a6aa6e88a233a1f2dc