Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoWenYuan committed Mar 1, 2024
1 parent ad584ac commit 4be4c66
Showing 90 changed files with 8,584 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -60,6 +60,7 @@ sysinfo.txt
*.aab
*.unitypackage
*.app
*.meta

# Crashlytics generated file
crashlytics-build.properties
2 changes: 2 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Unity BehaviourTree + Editor Node + Custom Data
Parse the data without using reflection
10 changes: 10 additions & 0 deletions XBehaviour/Editor/Attribute/MustExportAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace XBehaviour.Editor
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class MustExportAttribute : Attribute
{

}
}
131 changes: 131 additions & 0 deletions XBehaviour/Editor/Graph/XBehaviourNodeGraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using XBehaviour.Runtime;
using XNode;
using YamlDotNet.RepresentationModel;

namespace XBehaviour.Editor
{

[CreateAssetMenu(fileName = "NewXBehaviour",menuName = "XBehaviour/NewBehaviourTree")]
public class XBehaviourNodeGraph : NodeGraph {

[ContextMenu("导出行为树")]
void Export()
{
//TODO:执行导出行为树操作
CollectNodes();
}

void CollectNodes()
{
var root = nodes.Find(p => p.GetType() == typeof(RootView));
CollectNodes(root as NodeView);
Debug.LogError(new XBehaviourSerialization().Serializer(root));
}

void CollectNodes(NodeView nodeView)
{
nodeView.className = nodeView.GetType().Name.Replace("View","");
Debug.LogError(nodeView.className);
//只找到子节点的链接对象
var children = nodeView.OutputNodes<NodeView>("child");
if (children == null || children.Count == 0) return;

nodeView.Children.Clear();
children = children.OrderBy(p => p.position.y).ToList();
foreach (var child in children)
{
nodeView.Children.Add(child);
CollectNodes(child);

}
}
[ContextMenu("测试读取")]
void TestReader()
{
string path = "C:/Users/GuoWY/Desktop/BT/Assets/Scripts/Hotfix/XBehaviour/test.yaml";
var input = new StringReader(File.ReadAllText(path));
YamlStream yaml = new YamlStream();
yaml.Load(input);
Debug.LogError(yaml);
YamlMappingNode mapping = (YamlMappingNode)yaml.Documents[0].RootNode;
var rootNode = yaml.Documents[0].RootNode;
var parsedObject = ParseNode(rootNode);
Debug.LogError(parsedObject);
}

private static object ParseNode(YamlNode node)
{
switch (node)
{
case YamlScalarNode scalarNode:
return ParseScalar(scalarNode);
case YamlSequenceNode sequenceNode:
return ParseSequence(sequenceNode);
case YamlMappingNode mappingNode:
return ParseMapping(mappingNode);
default:
return null;
}
}

private static object ParseScalar(YamlScalarNode scalarNode)
{
// 这里可以根据需要处理不同类型的标量值
if (int.TryParse(scalarNode.Value, out int intValue))
{
return intValue;
}
else if (bool.TryParse(scalarNode.Value, out bool boolValue))
{
return boolValue;
}
else
{
return scalarNode.Value;
}
}

private static List<object> ParseSequence(YamlSequenceNode sequenceNode)
{
var list = new List<object>();
foreach (var node in sequenceNode.Children)
{
list.Add(ParseNode(node));
}
return list;
}

private static object ParseMapping(YamlMappingNode mappingNode)
{
/*
var result = new Dictionary<string, object>();
foreach (var entry in mappingNode.Children)
{
/*
result[entry.Key.ToString()] = ParseNode(entry.Value);
if (entry.Value is YamlSequenceNode sequenceNode)
{
foreach (var VARIABLE in sequenceNode.Children)
{
Debug.LogError(VARIABLE.NodeType.ToString() + " =>" + VARIABLE);
}
}
Debug.LogError(entry.Key);
}
// 根据result中的className创建相应的对象
// 注意:这里需要根据实际情况实现对象的创建和属性赋值
return result;
*/

return null;
}
}
}
20 changes: 20 additions & 0 deletions XBehaviour/Editor/Node/DecoratorView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#if UNITY_EDITOR

using XNode;

namespace XBehaviour.Editor
{
[Node.CreateNodeMenu("根节点/基础装饰器")]
[Node.NodeWidth(400)]
[Node.NodeTint(75, 20, 75)]
public class DecoratorView : NodeView
{
protected override void Init()
{
base.Init();
name = "装饰节点";
}
}
}

#endif
53 changes: 53 additions & 0 deletions XBehaviour/Editor/Node/NodeView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine;

using XNode;
using YamlDotNet.Serialization;

namespace XBehaviour.Editor
{
[System.Flags]
public enum TestEnum
{
[InspectorName("测试A")]
A = 1 << 0,
[InspectorName("BBBBB")]
B = 1 << 1,
[InspectorName("CCCCCCC")]
C = 1 << 2,
}


public class NodeView : XNode.Node
{
[HideInInspector]
public string className; // 类名



[Input(connectionType = ConnectionType.Override,backingValue = ShowBackingValue.Never),YamlIgnore]
public int parent;

[Output(connectionType = ConnectionType.Multiple,backingValue = ShowBackingValue.Never),YamlIgnore]
public int child;

[HideInInspector]
public List<NodeView> Children { get; set; } = new List<NodeView>();

protected override void Init()
{

}

public override object GetValue(NodePort port)
{
return this;
}
}

}
#endif
23 changes: 23 additions & 0 deletions XBehaviour/Editor/Node/ParallelView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#if UNITY_EDITOR
using XBehaviour.Runtime;

namespace XBehaviour.Editor
{
[CreateNodeMenu("组合节点/并行节点")]
[NodeWidth(400)]
[NodeTint(100, 140, 0)]
public class ParallelView : NodeView
{

public Parallel.ParallelState state;

public Parallel.ChildReturnStatue returnStatue;

protected override void Init()
{
base.Init();
name = "并行节点";
}
}
}
#endif
21 changes: 21 additions & 0 deletions XBehaviour/Editor/Node/RootView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#if UNITY_EDITOR
using UnityEngine;
using XBehaviour.Runtime;

namespace XBehaviour.Editor
{
[CreateNodeMenu("根节点/基础根节点")]
[NodeWidth(400)]
[NodeTint(70, 130, 180)]
public class RootView : NodeView
{
[InspectorName("根节点运行类型")] public RunningType RunningType;

protected override void Init()
{
base.Init();
name = "基础根节点";
}
}
}
#endif
21 changes: 21 additions & 0 deletions XBehaviour/Editor/Node/SelectorView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#if UNITY_EDITOR
using UnityEngine;
using XBehaviour.Runtime;

namespace XBehaviour.Editor
{
[CreateNodeMenu("组合节点/选择节点")]
[NodeWidth(400)]
[NodeTint(65, 105, 100)]
public class SelectorView : NodeView
{
[InspectorName("选择节点状态")]
public Selector.SelectionState selectionState;
protected override void Init()
{
base.Init();
name = "选择节点";
}
}
}
#endif
18 changes: 18 additions & 0 deletions XBehaviour/Editor/Node/SequenceView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#if UNITY_EDITOR

namespace XBehaviour.Editor
{
[CreateNodeMenu("组合节点/顺序节点")]
[NodeWidth(400)]
[NodeTint(46, 139, 87)]
public class SequenceView : NodeView
{
protected override void Init()
{
base.Init();
name = "顺序节点";
}
}
}

#endif
28 changes: 28 additions & 0 deletions XBehaviour/Editor/Node/TaskView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if UNITY_EDITOR

using System;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using UnityEngine;
using XNode;

namespace XBehaviour.Editor
{
[Node.CreateNodeMenu("根节点/基础任务节点")]
[Node.NodeWidth(400)]
[Node.NodeTint(30, 30, 30)]
public class TaskView : NodeView
{
public string value;

[InspectorName("测试用向量")]
public VectorView vector;
protected override void Init()
{
base.Init();
name = "基础任务节点";
}
}
}

#endif
28 changes: 28 additions & 0 deletions XBehaviour/Editor/Node/VectorView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if UNITY_EDITOR
using System;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using UnityEngine;
using XNode;
using YamlDotNet.Serialization;

namespace XBehaviour.Editor
{

public class VectorView : XNode.Node
{

[HideInInspector]
public string className;

public float x;
public float y;
public float z;
protected override void Init()
{
base.Init();
className = "Vector3";
}
}
}
#endif
Loading

0 comments on commit 4be4c66

Please sign in to comment.