How do I load using the Addressable method in Editor mode?

btQuestion

New member
There is no issue with loading and using ExternalBehaviorTree via the Adressable method in runtime mode.However, when I load an ExternalBehaviorTree as an Addressable in Editor mode, its BehaviorSource does not load correctly. It seems the BehaviorSource is disabled in Editor mode. How can I enable the ExternalBehaviorTree (BehaviorSource) in Editor mode, just like it is in Runtime mode?
 
What do you mean by the BehaviorSource being disabled? The source contains all of the behavior tree data so there's no enabled state. Can you give more details into what you are doing?
 
BehaviorSource가 비활성화된다는 건 무슨 말씀이신가요? 소스에 모든 동작 트리 데이터가 포함되어 있어서 활성화된 상태가 없습니다. 어떤 작업을 하시는지 더 자세히 설명해 주시겠어요?
What I want is to parse the internal field values of the External Behavior Tree asset into a JSON file.


private static string ConvertBTToJson(string jsonPath, BehaviorSource behaviorSource)
{
var rootTaskField = typeof(BehaviorSource).GetField("mRootTask", BindingFlags.NonPublic | BindingFlags.Instance);
var rootTask = rootTaskField?.GetValue(behaviorSource) as Task;

if (rootTask == null)
{
Debug.LogWarning($"rootTask is null.");
}
// convert bt to json...
}
This method retrieves the BehaviorSource from an ExternalBehaviorTree loaded via AssetDatabase.LoadAssetAtPath() and parses it into a JSON file.


When loading an ExternalBehaviorTree in runtime mode, the behaviorSource can retrieve all field values without issue. However, performing the same operation in editor mode results in all field values within the behaviorSource being null or default.

behaviorSource_notLoaded.png
 
Instead of loading the BehaviorSource with the Assembly system, you should instead load the ExternalBehavior. This is a ScriptableAsset that is made to be loaded in cases like this. You can then assign the ExternalBehavior on the BehaviorTree component and it will deserialize properly.
 
Instead of loading the BehaviorSource with the Assembly system, you should instead load the ExternalBehavior. This is a ScriptableAsset that is made to be loaded in cases like this. You can then assign the ExternalBehavior on the BehaviorTree component and it will deserialize properly.
Hmm... I think my explanation was incorrect. Let me start over from the beginning.


To get straight to the point:

1. When I manually click to activate the ExternalBehaviorTree asset in the Project window and load the ExternalBehaviorTree, the internal field values of the BT load correctly.
2. When I load the ExternalBehaviorTree without manually clicking the ExternalBehaviorTree asset in the Project window, the internal field values of the BT do not load.



I load the ExternalBehaviorTree asset using the following method:
public async UniTask LoadAsync()
{
ExternalBehaviorTree asset = AssetDatabase.LoadAssetAtPath<ExternalBehaviorTree>(assetPath);
}



1. When I select the ExternalBehaviorTree asset by clicking it with the mouse in the Project window and then load it using the LoadAsync() method, the following occurs:

1758119839363.png
1758119700251.png
The internal field values of ExternalBehaviorTree loaded correctly.




2. After refreshing Unity or waiting a certain amount of time, loading the ExternalBehaviorTree asset using the LoadAsync() method without selecting it first results in:

1758119901971.png

1758120445838.png
The internal field values of ExternalBehaviorTree did not load.




Although the same method was used, the result differs depending on whether the asset was directly selected and activated.
What I want is to fully load the internal field values of the ExternalBehaviorTree asset at any time.
 
Ok, good. In this case it sounds like the external tree isn't deserialized. You can do so by calling CheckForSerialization(true) on the ExternalBehavior.
 
1758180022415.png

1758179849386.png
I called CheckForSerialization(true), but a NullReferenceException error occurred.



1758179914430.png
I tried to debug what was happening internally, but failed because I couldn't set a breakpoint.

It would be helpful to know what happens when I directly click the mouse to activate the ExternalBehaviorTree asset...
 

Attachments

  • 1758179684058.png
    1758179684058.png
    32.3 KB · Views: 0
You shouldn't call it on BehaviorSource. Just ExternalBehavior.CheckForSerialization. So in your case:

Code:
bt.CheckForSerialization(true);
 
GameObject go = new();
BehaviorTree b = go.AddComponent<BehaviorTree>();

b.ExternalBehavior = bt;
b.CheckForSerialization();
bt.BehaviorSource.CheckForSerialization(true);

I successfully deserialized the ExternalBehaviorTree in Editor mode!
 
Back
Top