临时提交

This commit is contained in:
PC-20230316NUNE\Administrator
2024-08-17 14:12:46 +08:00
parent 98bead1a7c
commit 3afb2e3b86
279 changed files with 20594 additions and 285 deletions

View File

@@ -0,0 +1,20 @@
using UnityEngine;
using BehaviorTreeSlayer;
namespace BehaviorTreeSlayerEditor
{
internal class ActionView : NodeView
{
public ActionView(BehaviorTreeWindow window) : base(window)
{
}
public override void OnDraw()
{
GUI.BeginGroup(rect);
GUI.Box(new Rect(0, 0, rect.width, rect.height), Name, treeNode.state.ToString());
GUI.Box(new Rect(rect.width / 2 - 20, 8, 40, 6), "", "extra");
GUI.EndGroup();
base.OnDraw();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e456d77139418e4469a1133497b08291
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,106 @@
using BehaviorTreeSlayer;
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace BehaviorTreeSlayerEditor
{
public class ArrayField
{
Type type, itemType;
public int Count;
int lastCount;
Array array;
private readonly TreeNode treeNode;
private readonly FieldInfo fieldInfo;
List<ViewField> viewFields = new List<ViewField>();
private string name;
bool show;
public ArrayField(TreeNode treeNode, FieldInfo fieldInfo, Type tp)
{
this.treeNode = treeNode;
this.fieldInfo = fieldInfo;
this.name = fieldInfo.Name;
type = fieldInfo.FieldType.GetElementType();
itemType = tp == null ? type : tp;
array = fieldInfo.GetValue(treeNode) as Array;
if (array != null)
{
Count = array.Length;
lastCount = Count;
InitViewField();
}
}
public float OnDraw(float y, float width, int arrIdx)
{
float height = show ? viewFields.Count * 30 + 40 : 40;
Rect rect = new Rect(0, y, width, height);
GUI.BeginGroup(rect, "", "ArrayField" + arrIdx);
show = GUI.Toggle(new Rect(5, 10, 20, 20), show, "");
float w = (width - 40) / 3;
GUI.Label(new Rect(rect.x + 25, 10, w, 20), name, "t2");
Count = EditorGUI.IntField(new Rect(w + 40, 10, w * 2 - 10, 20), Count, "box");
if (lastCount != Count)
{
show = true;
if (array == null)
{
array = Array.CreateInstance(type, Count);
object df = type.IsValueType ? 0 : (object)"";
for (int i = 0; i < Count; i++)
{
array.SetValue(df, i);
}
}
else
{
Array arr = Array.CreateInstance(type, Count);
int length = Math.Min(array.Length, Count);
for (int i = 0; i < length; i++)
{
arr.SetValue(array.GetValue(i), i);
}
array = arr;
}
Save();
viewFields.Clear();
InitViewField();
}
if (show)
{
for (int i = 0; i < viewFields.Count; i++)
{
viewFields[i].OnDraw(new Rect(0, i * 30 + 40, rect.width - 10, 20));
}
}
GUI.EndGroup();
lastCount = Count;
return rect.height;
}
private void InitViewField()
{
for (int i = 0; i < array.Length; i++)
{
int idx = i;
viewFields.Add(new ViewField(array.GetValue(idx), idx + ".", itemType, obj =>
{
array.SetValue(obj, idx);
Save();
}));
}
}
void Save()
{
fieldInfo.SetValue(treeNode, array);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 812928c3c48961c41992cf08204baff4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
using UnityEngine;
namespace BehaviorTreeSlayerEditor
{
public class CompositeView : NodeView
{
public CompositeView(BehaviorTreeWindow window) : base(window)
{
}
public override void OnDraw()
{
GUI.BeginGroup(rect);
GUI.Box(new Rect(0, 0, rect.width, rect.height), Name, treeNode.state.ToString());
GUI.Box(new Rect(rect.width / 2 - 20, 8, 40, 6), "", "extra");
GUI.Box(new Rect(rect.width / 2 - 20, rect.height - 14, 40, 6), "", "extra");
GUI.EndGroup();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b0232e5f7addbf64d84e748013c1dd7e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,20 @@
using UnityEngine;
using BehaviorTreeSlayer;
namespace BehaviorTreeSlayerEditor
{
public class EntryView : NodeView
{
public EntryView(BehaviorTreeWindow window) : base(window)
{
}
public override void OnDraw()
{
GUI.BeginGroup(rect);
GUI.Box(new Rect(0, 0, rect.width, rect.height), Name, treeNode.state.ToString());
GUI.Box(new Rect(rect.width / 2 - 20, rect.height - 14, 40, 6), "", "extra");
GUI.EndGroup();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 37d5bb6ac499bd54fb45873665717e07
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,115 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using BehaviorTreeSlayer;
namespace BehaviorTreeSlayerEditor
{
public class NodeView
{
private const BindingFlags BindingAttr = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
public static float SNAP = 20f;
internal NoodleView Connect;
public string Name;
BehaviorTreeWindow window;
protected Rect rect = new Rect(0, 0, 120, 120);
Vector2 bias;
public NodeView(BehaviorTreeWindow wind)
{
window = wind;
}
public NodeView(TreeNode treeNode)
{
this.treeNode = treeNode;
}
public void OnSelect(Vector2 mousePosition)
{
bias = rect.position - mousePosition;
}
bool toggle = true;
public TreeNode treeNode;
public Rect TopRect => new Rect(rect.width / 2 - 20 + rect.position.x, rect.position.y, 40, 14);
public Rect BotRect => new Rect(rect.width / 2 - 20 + rect.position.x, rect.height - 14 + rect.position.y, 40, 14);
public Rect Rect { get => rect; set => rect = value; }
public Vector2 Position
{
get => rect.position; set
{
rect.position = value;
}
}
public bool ShowValueType { get => toggle; }
public void OnUpdate(Event evt)
{
}
public bool Contains(Vector2 point)
{
return rect.Contains(point);
}
internal NodeView Set(TreeNode treeNode)
{
this.treeNode = treeNode;
Position = new Vector2(treeNode.x, treeNode.y);
Name = treeNode.GetType().Name;
InitField();
return this;
}
FieldInfo[] infos;
void InitField()
{
infos = treeNode.GetType().GetFields(BindingAttr)
.Where(f => f.GetCustomAttribute<ShowMe>() != null)
.ToArray();
}
public void OnDestroy()
{
}
public virtual void OnDraw()
{
GUI.BeginGroup(rect);
for (int i = 0; i < infos.Length; i++)
{
object obj = infos[i].GetValue(treeNode);
string text;
if (obj == null)
{
text = "";
}
else if (infos[i].GetCustomAttribute<ShowMe>().ShowMsg != null)
{
text = string.Format(infos[i].GetCustomAttribute<ShowMe>().ShowMsg, obj);
}
else
{
text = obj.ToString();
}
GUI.Label(new Rect(22, 42 + i * 18, rect.width - 44, 18), text, "t1");
}
GUI.EndGroup();
}
public void DragRect()
{
Vector2 vector2 = Event.current.mousePosition + bias;
vector2.x = ((int)(vector2.x / SNAP)) * SNAP;
vector2.y = ((int)(vector2.y / SNAP)) * SNAP;
rect.position = vector2;
treeNode.x = vector2.x;
treeNode.y = vector2.y;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2112d3fd78d5f834bacaad4e7dbbca8b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,66 @@
using UnityEditor;
using UnityEngine;
namespace BehaviorTreeSlayerEditor
{
public class NoodleView
{
public static int Type;
public NodeView Bot, Top;
public void OnDraw()
{
Handles.color = Color.white;
Vector3 point_start = BehaviorTreeWindow.GetVector3(Bot.TopRect.center);
Vector3 point_end = BehaviorTreeWindow.GetVector3(Top.BotRect.center);
switch (Type)
{
case 1:
DrawPolyLine(point_start, point_end);
break;
case 2:
DrawBezier(point_start, point_end);
break;
default:
DrawSimpleLine(point_start, point_end);
break;
}
}
void DrawBezier(Vector3 point_start, Vector3 point_end)
{
Vector3 p1 = new Vector3(point_start.x, (point_start.y + point_end.y) / 2);
Vector3 p2 = new Vector3(point_end.x, (point_start.y + point_end.y) / 2);
float pointDistance = Vector3.Distance(point_start, point_end) / 2f;
Vector3 startTan = new Vector3(point_start.x, point_start.y - pointDistance, 0f);
Vector3 endTan = new Vector3(point_end.x, point_end.y + pointDistance, 0f);
Handles.DrawBezier(point_start, point_end, startTan, endTan, Color.white, null, 3);
}
private void DrawPolyLine(Vector3 point_start, Vector3 point_end)
{
Vector3 p1 = new Vector3(point_start.x, (point_start.y - 20));
Vector3 k1 = new Vector3((point_start.x + point_end.x) / 2, (point_start.y - 20));
Vector3 k2 = new Vector3((point_start.x + point_end.x) / 2, (point_end.y + 20));
Vector3 k3 = new Vector3(point_end.x, (point_end.y + 20));
Vector3 p2 = new Vector3(point_end.x, (point_start.y - 20));
if (p1.x == p2.x)//这两个重合了,线条会变得奇怪,需要把这个情况给排除掉
{
DrawSimpleLine(point_start, point_end);
}
else if (point_start.y <= point_end.y + 20)
{
Handles.DrawAAPolyLine(3, point_start, p1, k1, k2, k3, point_end);
}
else
{
Handles.DrawAAPolyLine(3, point_start, p1, p2, point_end);
}
}
private void DrawSimpleLine(Vector3 point_start, Vector3 point_end)
{
Handles.DrawAAPolyLine(3, point_start, point_end);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9a5483112554883408af49527359d507
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using BehaviorTreeSlayer;
namespace BehaviorTreeSlayerEditor
{
public class ViewDetails
{
List<ViewField> list = new List<ViewField>();
List<ArrayField> array = new List<ArrayField>();
NodeView view;
public void OnSelect(NodeView nodeView)
{
if (view != nodeView)
{
list.Clear();
array.Clear();
view = nodeView;
InitFields();
}
}
Vector2 sd = Vector2.one;
Rect viewRect;
public void OnDraw(Rect rect)
{
float h = 0;
if (viewRect == null)
{
viewRect = rect;
}
sd = GUI.BeginScrollView(rect, sd, viewRect);
for (int i = 0; i < list.Count; i++)
{
list[i].OnDraw(new Rect(0, i * 30 + 20, rect.width-15, 20));
h += 30;
}
h += 20;
for (int i = 0; i < array.Count; i++)
{
h += array[i].OnDraw(h, rect.width-15, i % 7) + 10;
}
viewRect.height = h;
GUI.EndScrollView();
}
void InitFields()
{
TreeNode treeNode = view.treeNode;
Type type = treeNode.GetType();
FieldInfo[] fields = type.GetFields();
for (int i = 0; i < fields.Length; i++)
{
FieldInfo fieldInfo = fields[i];
object[] attr = fieldInfo.GetCustomAttributes(false);
foreach (var item in attr)
{
if (item.GetType() == typeof(OutField))
{
OutField ab = (OutField)item;
if (fieldInfo.FieldType.IsArray)
{
array.Add(new ArrayField(treeNode, fieldInfo, ab.FieldType));
}
else
{
Type tp = ab.FieldType == null ? fieldInfo.FieldType : ab.FieldType;
list.Add(new ViewField(fieldInfo.GetValue(treeNode), fieldInfo.Name, tp, value =>
{
fieldInfo.SetValue(treeNode, value);
}));
}
}
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ee4359fc67a0bed40ac1e0d57e1a2dec
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,258 @@
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using BehaviorTreeSlayer;
namespace BehaviorTreeSlayerEditor
{
public class ViewField
{
string filter;
private const string Style = "box";
public string name;
public string str = "";
string lastStr;
public Type type;
private Action<object> callBack;
int resultInt = 0;
int lastInt = 0;
private float resultFloat;
private float lastFloat;
private double resultDouble;
private double lastDouble;
private Color rsColor;
private Color lastColor;
private object result;
private object lastResult;
private bool rsBool;
private bool lastBool;
private int selectedIdx;
private int lastIdx;
private bool selecting;
public ViewField(object defaultValue, string name, Type tp, Action<object> callBack)
{
this.name = name;
type = tp;
InitValue(defaultValue);
this.callBack = callBack;
}
private void InitValue(object defaultValue)
{
if (defaultValue == null)
{
}
else if (type == typeof(int))
{
resultInt = (int)defaultValue;
lastInt = (int)defaultValue;
}
else if (type == typeof(float))
{
lastFloat = (float)defaultValue;
resultFloat = (float)defaultValue;
}
else if (type == typeof(double))
{
resultDouble = (double)defaultValue;
lastDouble = (double)defaultValue;
}
else if (type == typeof(bool))
{
rsBool = (bool)defaultValue;
lastBool = (bool)defaultValue;
}
else if (type == typeof(string) || SlayerUtils.Dic.ContainsKey(type))
{
str = defaultValue.ToString();
lastStr = defaultValue.ToString();
}
else if (type == typeof(Color))
{
rsColor = (Color)defaultValue;
lastColor = (Color)defaultValue;
}
else if (type.IsEnum)
{
selectedIdx = (int)defaultValue;
lastIdx = (int)defaultValue;
}
else if (type.IsSubclassOf(typeof(UnityEngine.Object)))
{
str = (string)defaultValue;
}
else
{
result = defaultValue;
lastResult = defaultValue;
}
}
public void OnDraw(Rect rect)
{
float width = rect.width / 3;
GUI.Label(new Rect(rect.x, rect.y, width, rect.height), name, "t2r");
Rect fieldRect = new Rect(rect.x + width + 10, rect.y, 2 * width - 10, rect.height);
//
if (type == typeof(int))
{
resultInt = EditorGUI.IntField(fieldRect, resultInt, Style);
if (resultInt != lastInt)
{
callBack?.Invoke(resultInt);
}
lastInt = resultInt;
}
else if (type == typeof(float))
{
resultFloat = EditorGUI.FloatField(fieldRect, resultFloat, Style);
if (resultFloat != lastFloat)
{
callBack?.Invoke(resultFloat);
}
lastFloat = resultFloat;
}
else if (type == typeof(double))
{
resultDouble = EditorGUI.DoubleField(fieldRect, resultDouble, Style);
if (resultDouble != lastDouble)
{
callBack?.Invoke(resultDouble);
}
lastDouble = resultDouble;
}
else if (type.IsSubclassOf(typeof(UnityEngine.Object)))
{
//goddamn ShowObjectPicker
if (selecting == true && Event.current.commandName == "ObjectSelectorClosed")
{
selecting = false;
result = EditorGUIUtility.GetObjectPickerObject();
}
if (GUI.Button(new Rect(fieldRect.x, fieldRect.y, fieldRect.width - 20, fieldRect.height), str, Style))
{
BehaviorTreeWindow.Instance.Msg = str;
}
if (GUI.Button(new Rect(fieldRect.width - 16 + fieldRect.x, fieldRect.y + 4, 16, 16), "Select", "dot2"))
{
filter = "t: " + type.Name;
EditorGUIUtility.ShowObjectPicker<UnityEngine.Object>((UnityEngine.Object)result, false, filter, 0);
selecting = true;
}
if (result != lastResult)
{
string astPath = AssetDatabase.GetAssetPath((UnityEngine.Object)result);
if (string.IsNullOrEmpty(astPath))
{
str = astPath;
}
else
{
int a = astPath.IndexOf("Resources");
if (a < 0)
{
str = astPath;
}
else
{
int b = astPath.LastIndexOf(".");
str = astPath.Substring(a, b - a);
}
BehaviorTreeWindow.Instance.Msg = str;
}
callBack?.Invoke(str);
lastResult = result;
}
}
else if (type == typeof(string))
{
str = GUI.TextField(fieldRect, str, Style);
if (!str.Equals(lastStr))
{
callBack?.Invoke(str);
}
lastStr = str;
}
else if (type == typeof(Color))
{
rsColor = EditorGUI.ColorField(fieldRect, rsColor);
if (rsColor != (lastColor))
{
callBack?.Invoke(rsColor);
}
lastColor = rsColor;
}
else if (type == typeof(bool))
{
rsBool = EditorGUI.Toggle(fieldRect, rsBool);
if (rsBool != lastBool)
{
callBack?.Invoke(rsBool);
}
lastBool = rsBool;
}
else if (type.IsEnum)
{
selectedIdx = EditorGUI.Popup(fieldRect, selectedIdx, Enum.GetNames(type));
if (selectedIdx != lastIdx)
{
callBack?.Invoke(selectedIdx);
}
lastIdx = selectedIdx;
}
else if (SlayerUtils.Dic.ContainsKey(type))
{
str = GUI.TextField(fieldRect, str, Style);
if (!str.Equals(lastStr))
{
object rst = SlayerUtils.Dic[type](str);
callBack?.Invoke(rst);
}
lastStr = str;
}
else if (type == typeof(Vector3))
{
result = EditorGUI.Vector3Field(fieldRect, "", (Vector3)result);
if (result != (lastResult))
{
callBack?.Invoke(result);
}
lastResult = result;
}
else if (type == typeof(Vector3Int))
{
result = EditorGUI.Vector3IntField(fieldRect, "", (Vector3Int)result);
if (result != (lastResult))
{
callBack?.Invoke(result);
}
lastResult = result;
}
else if (type == typeof(Vector2))
{
result = EditorGUI.Vector2Field(fieldRect, "", (Vector2)result);
if (result != (lastResult))
{
callBack?.Invoke(result);
}
lastResult = result;
}
else if (type == typeof(Vector2Int))
{
result = EditorGUI.Vector2IntField(fieldRect, "", (Vector2Int)result);
if (result != (lastResult))
{
callBack?.Invoke(result);
}
lastResult = result;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6a3b5f05d3759ff4bb8a7ce1a57477ff
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: