Unity-odin-npbehave结合烟雨大佬的示例-续
本篇主要记录如何使用从ODIN导出的数据给Npbehave使用
注:烟雨大佬的MOBA Demo 是基于ET引擎的
开始
准备工作:1.下载好烟雨大佬的源码即可,也可以自行导入ODIN,自行导入NPBEHAVE
1.上文说到已经成功从ODIN可视化编辑器导出各种NP_NodeDataBase,所以我们需要加载导出数据,然后进行反序列化成我们想要的类实例。由于序列化使用的Mongo的BSON库,然后很多子类父类都需要导出,所以要将所有子类进行注册。
public Dictionary<long, NP_DataSupportor> NpRuntimeTreesDatas = new Dictionary<long, NP_DataSupportor>();
public void Awake()
{
Type[] types = typeof (NodeType).Assembly.GetTypes();
foreach (Type type in types)
{
if (!type.IsSubclassOf(typeof (NP_NodeDataBase)) && !type.IsSubclassOf(typeof (NP_ClassForStoreAction)) &&
!type.IsSubclassOf(typeof (SkillBaseNodeData)) && !type.IsSubclassOf(typeof (BuffDataBase)) &&
!type.IsSubclassOf(typeof (ListenBuffEventBase)))
{
continue;
}
BsonClassMap.LookupClassMap(type);
}
同时,将导出的文件进行反序列化生成我们需要得行为树节点数据:
DirectoryInfo directory = new DirectoryInfo(NPDataPath);
FileInfo[] fileInfos = directory.GetFiles();
foreach (var VARIABLE in fileInfos)
{
byte[] mfile = File.ReadAllBytes(VARIABLE.FullName);
if (mfile.Length == 0) Log.Info("没有读取到文件");
try
{
NP_DataSupportor MnNpDataSupportor = BsonSerializer.Deserialize<NP_DataSupportor>(mfile);
Log.Info($"反序列化行为树:{VARIABLE.FullName}完成");
NpRuntimeTreesDatas.Add(MnNpDataSupportor.RootId, MnNpDataSupportor);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
2.上面将所有行为树及其节点数据都准备好了,接下来就要用这些行为树节点数据,与NPBehave内的各种节点对应起来,生成真正的NPBehave节点。于是在NP_RuntimeTreeFactory类创建一个NPBehave树实例时,调用节点数据类的不同方法,生成对应的类,并将节点之间的关系通过linkedID进行连接。
NP_DataSupportor npDataSupportor = Game.Scene.GetComponent<NP_TreeDataRepository>().GetNP_TreeData_DeepCopy(NPDataId);
long theRuntimeTreeID = IdGenerater.GenerateId();
//Log.Info($"运行时id为{theRuntimeTreeID}");
foreach (var VARIABLE in npDataSupportor.mNP_DataSupportorDic)
{
switch (VARIABLE.Value.NodeType)
{
case NodeType.Task:
VARIABLE.Value.CreateTask(unit.Id, theRuntimeTreeID);
break;
case NodeType.Decorator:
VARIABLE.Value.CreateDecoratorNode(unit.Id, theRuntimeTreeID,
npDataSupportor.mNP_DataSupportorDic[VARIABLE.Value.linkedID[0]].NP_GetNode());
break;
case NodeType.Composite:
List<Node> temp = new List<Node>();
foreach (var VARIABLE1 in VARIABLE.Value.linkedID)
{
temp.Add(npDataSupportor.mNP_DataSupportorDic[VARIABLE1].NP_GetNode());
}
VARIABLE.Value.CreateComposite(temp.ToArray());
break;
}
}
3.创建NP_RuntimeTree,填入所有相关行为树数据
public Root m_NPRuntimeTreeRootNode;
/// <summary>
/// 所归属的数据块
/// </summary>
public NP_DataSupportor m_BelongNP_DataSupportor;
public void Awake(Root mRoot,NP_DataSupportor m_BelongNP_DataSupportor)
{
this.m_NPRuntimeTreeRootNode = mRoot;
this.m_BelongNP_DataSupportor = m_BelongNP_DataSupportor;
}
4.将生成好的行为树加入到行为树管理类中,同时把行为树数放入黑板中
NP_RuntimeTree tempTree = ComponentFactory.CreateWithId<NP_RuntimeTree, Root, NP_DataSupportor>(theRuntimeTreeID,
(Root) npDataSupportor.mNP_DataSupportorDic[npDataSupportor.RootId].NP_GetNode(), npDataSupportor);
unit.GetComponent<NP_RuntimeTreeManager>().AddTree(tempTree.Id, npDataSupportor.RootId, tempTree);
//把提前缓存的数据注入黑板
unit.GetComponent<NP_InitCacheComponent>().AddCacheDatas2RuntimeTree(tempTree);
自此行为树已经建立好,将root节点start,整个行为树就托管给NPBehave开始跑了