• Stars
    star
    128
  • Rank 281,044 (Top 6 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 4 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Free GraphView-based Behavior Tree designer for Unity.

UniBT

License: MIT GitHub release Github all releases

Free Behavior Tree designer for Unity.

Motivation

I know some designers like Behavior Designer or Arbor3.
They have many features but not for free. I will provide easily customizable and free designer.

Supported version

  • Unity 2019.4 or later.

Features

  • Supports constructing behavior tree by GraphView.
  • Supports visualizing active node in runtime.
  • Easily add original behaviors(Action,Conditional,Composite).

Get Started

  1. Download and install unity package.
    Also the package can also be imported with Package Manager, by adding the following entry in your Packages/manifest.json:
{
  "dependencies": {
    ...
    "com.github.yoshidan.unibt": "https://github.com/yoshidan/UniBT.git?path=Assets/UniBT/Scripts"
  }
}
  1. Add UniBT.BehaviorTree component for any GameObject.

  2. Open Graph Editor button opens GraphView for Behavior Tree.

  3. Add behaviors and set parameters.

  4. Finally press save button on tool bar of the editor window. (If invalid node found the color of the node become red.)

  5. Run the unity application. you can see node status in the editor window.

    • The red node means that last Update returned Status.Failure`.
    • The green node means that last Update returned Status.Success.
    • The yellow node means that last Update returned Status.Running.
  6. you can save the GameObject with UniBT.BehaviorTree as prefab.

How It Works

  • UnitBT.BehaviorTree updates child nodes in Update timing when the UpdateType is UpdateType.Auto.
  • If you want to update at any time, change UpdateType to UpdateType.Manual and call BehaviorTree.Tick();
  • Only UniBT.BehaviorTree is the MonoBehavior. Each node is just a C# Serializable class.

Core Behavior Node

Name Description
Composite Node It has one or more child nodes and controls which child node to update.
Action Node This is the leaf node. It execute action such as follow player, attack, escape or others you define.
Conditional Node It has one child node and check the condition whether child is updatable. when having no child, Conditional Node is the leaf node like Action Node.

Conditional Node has following parameter.

Name Description
dontReEvaluateOnRunning true: don't re evaluate the condition if the previous status is Status.Running.

Built In Composite Node

I have prepared several built in Composite Node.

Sequence

  • Updates the child nodes in order from the top.
  • Returns failure immediately if the child node returns failure.
  • Returns running immediately and calls the child at the next update timing if the child node returns running.
  • Returns success if all child nodes return success.

Sequence has following parameter.

Name Description
abortOnConditionChanged true: Aborts the running node when a node with a higher priority than the running node becomes infeasible. Specifically, the execution result of Conditional.CanUpdate, which is a descendant of a node with a higher priority than the running node, is used.

Selector

  • Updates the child nodes in order from the top.
  • Returns success immediately if the child node returns success.
  • Returns running immediately and calls the child at the next update timing if the child node returns running.
  • Returns failure if all child nodes return failure.

Selector has following parameter.

Name Description
abortOnConditionChanged true: Aborts the running node when a node with a higher priority than the running node becomes executable. Specifically, the execution result of Conditional.CanUpdate, which is a descendant of a node with a higher priority than the running node, is used.

All

  • Updates all child nodes.
  • Returns running if any child node returns running.
  • Returns failure if any child node returns failure.
  • Otherwise, returns success.

Random

  • The child nodes are elected and executed according to the probability based on the uniform distribution.
  • Select one for each update. However, if the running status is returned during the last update, the node will continue to run.

Rotator

  • Updates the child nodes in order. Unlike Sequencer, one child node is executed by one update instead of executing all child nodes by one update.
  • For example, if there are three child nodes, the first Update will execute the top node, the next Update will execute the second node, and the next Update will execute the third node.
  • The next run will run the top node again.
  • If a child node returns a running state, it exits without executing subsequent child nodes, and the child node continues to run on the next update.

Rotator has following parameter.

Name Description
resetOnAbort It is a flag whether to return the next execution target node from the top when the execution condition of the ancestor Conditional Node changes and the running node is interrupted.

Demo Scene

  • here is the example scene.

Create Behaviors

Create Action

  • Create C# Script and extends UniBT.Action
  • Override OnUpdate and return status(Success/Running/Failure).
  • Override Awake called by UniBT.BehaviorTree.Awake if needed.
  • Override Start called by UniBT.BehaviorTree.Start if needed.
  • Override Abort to reset field or any state when the parent condition changed..
  • Action has Node gameObject field with UniBT.BehaviorTree attached.
  • Private [SerializeField] field and public field can be set on Behavior Tree editor window.
public class Wait : Action
{
    [SerializeField] 
    private float waitTime;

    private float elapsedTime = 0.0f;

    protected override Status OnUpdate()
    {
        elapsedTime += Time.deltaTime;
        if (elapsedTime < waitTime)
        {
            return Status.Running;
        }

        elapsedTime = 0.0f;
        return Status.Success;
    }

    // abort when the parent conditional changed on previous status is running.
    public override void Abort()
    { 
        elapsedTime = 0.0f;
    }
}

Create Conditional

  • Create C# Script and extends UniBT.Conditional
  • Override IsUpdatable and return result(true/false). when IsUpdatable returns update child.
  • Override OnAwake called by UniBT.BehaviorTree.Awake if needed.
  • Override OnStart called by UniBT.BehaviorTree.Start if needed.
  • Conditional Node has gameObject field with UniBT.BehaviorTree attached.
  • Private [SerializeField] field and public field can be set on Behavior Tree editor window.
public class IsHateGt: Conditional
{
    [SerializeField] 
    private int threshold;

    private Enemy enemy;

    protected override void OnAwake()
    {
        enemy = gameObject.GetComponent<Enemy>();
    }

    protected override bool IsUpdatable()
    {
        return enemy.Hate > threshold;
    }
}
  • Conditional Node can be leaf node like Action Node.

  • Conditional Node can be branch node.

Create Composite

  • Create C# Script and extends UniBT.Composite
  • Override OnUpdate and return status(Success/Running/Failure).
  • Override OnAwake called by UniBT.BehaviorTree.Awake if needed.
  • Override OnStart called by UniBT.BehaviorTree.Start if needed.
  • To abort the running node when the condition changed override Abort.
  • Composite Node has gameObject field with UniBT.BehaviorTree attached.
  • Private [SerializeField] field and public field can be set on Behavior Tree editor window.
public class Random : Composite
{
    private NodeBehavior runningNode;

    protected override Status OnUpdate()
    {
        // proceed to update same node when the previous status is running
        if (runningNode != null)
        {
            return HandleStatus(runningNode.Update(), runningNode);
        }

        // update random children
        var result = UnityEngine.Random.Range(0, Children.Count);
        var target = Children[result];
        return HandleStatus(target.Update(), target);
    }

    private Status HandleStatus(Status status, NodeBehavior updated)
    {
        //save running node for next update.
        runningNode = status == Status.Running ? updated : null;
        return status;
    }

    // abort when the parent conditional changed on previous status is running.
    public override void Abort()
    {
        if (runningNode != null)
        {
            runningNode.Abort();
            runningNode = null;
        }
    }
}

More Repositories

1

google-cloud-rust

Google Cloud Client Libraries for Rust.
Rust
163
star
2

EasyVTuberRel

BABINIKU(ใƒ็พŽ่‚‰) meeting support tool for Zoom.us, WebEx, whereby.com or Slack on Mac OS
C#
89
star
3

pytorch-facial-expression-recognition

Lightweight Facial Expression(emotion) Recognition model
Python
53
star
4

pytorch-eyeblink-detection

Simple Eye Blink Detection with CNN
Python
18
star
5

N0vaMac

Desktop Live Wallpaper tool for macOS
C#
12
star
6

spring-boot-redis-metrics

redis metrics for spring boot
Java
7
star
7

solana-auction

The recipe to develop NFT auction program on Solana chain.
Rust
7
star
8

google-cloud-rust-example

Example of google cloud rust
Rust
6
star
9

VR-Locomotion

Example of VR Locomotion implementation like a VRChat
C#
5
star
10

nene

command-line tool to generate Rust code for Google Cloud Spanner
Rust
4
star
11

web_avatar_sample

TypeScript
3
star
12

anchor-liquidity-pool

The example anchor implementation for solana-swap
Rust
3
star
13

reflex

Simple STUN server for WebRTC
C++
3
star
14

anchor-auction

The recipe to develop NFT auction program on Solana chain with Anchor.
Rust
2
star
15

docker-ui-clj

Clojure reagent/re-frame sample app
Clojure
2
star
16

AudienceShader

Compute Shader for audience in IDOL live stage.
C#
2
star
17

go-stun

STUN (Session Traversal Utilities for NATs) Server.
Go
1
star
18

UnityNormalViewer

Normal viewer for Unity Editor
C#
1
star
19

cosmos-sdk-validator-example

How to add the validator to the cosmos-sdk based blockchain.
Dockerfile
1
star
20

NFT

NFTใซ้–ขใ™ใ‚‹่ชฟๆŸป
Solidity
1
star
21

spring-data-redis-support

wrapper for spring-data-redis-wrapper
Java
1
star
22

distributed-data-manager

rebalancer database with consitent hashing
Clojure
1
star