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
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 64c4e3fb204f4969917cc3c9292203d8
timeCreated: 1777370342
+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
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9fdd1a937331f41f6bfe17cf230bbd57
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,49 @@
using System;
using AI.FSM;
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: "4485803bf98219ce319c3000d6f8e6bf")]
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 action here
// test for valid variables
if (Agent.Value == null) return Status.Failure;
return Status.Running;
}
protected override Status OnUpdate()
{
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 action here
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 91157a6cd972849c58afadf2f49c5d04
@@ -0,0 +1,35 @@
using System;
using Unity.Behavior;
using UnityEngine;
[Serializable, Unity.Properties.GeneratePropertyBag]
[Condition(name: "Target Null", story: "[Target] [is] null", category: "Conditions", id: "83ee136bdfeb6568a8a2fb0a340e6e69")]
public partial class GameObjectNullCondition : Condition
{
[SerializeReference] public BlackboardVariable<GameObject> Target;
[Comparison(comparisonType: ComparisonType.Boolean)]
[SerializeReference] public BlackboardVariable<ConditionOperator> Is;
public override bool IsTrue()
{
if (Is.Value == ConditionOperator.Equal)
{
return Target.Value == null;
}
else
{
return Target.Value != null;
}
return (Target.Value == null && Is.Value == ConditionOperator.Equal) ||
(Target.Value != null && Is.Value == ConditionOperator.NotEqual);
}
public override void OnStart()
{
}
public override void OnEnd()
{
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 0c4c02578331346c792b21cbd5e227a4