1
This commit is contained in:
20
Src/Unity/Scripts/WorldNode/BITKit.WorldNode.Runtime.asmdef
Normal file
20
Src/Unity/Scripts/WorldNode/BITKit.WorldNode.Runtime.asmdef
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "BITKit.WorldNode.Runtime",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:508392158bd966c4d9c21e19661a441d",
|
||||
"GUID:d525ad6bd40672747bde77962f1c401e",
|
||||
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
|
||||
"GUID:d8b63aba1907145bea998dd612889d6b"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 01cf845b40a364e4cbaee058936fa4a3
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
8
Src/Unity/Scripts/WorldNode/Editor.meta
Normal file
8
Src/Unity/Scripts/WorldNode/Editor.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dad0d8732f120a848a84b5b41d23c141
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "BITKit.WorldNode.Editor",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:508392158bd966c4d9c21e19661a441d",
|
||||
"GUID:d525ad6bd40672747bde77962f1c401e",
|
||||
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
|
||||
"GUID:d8b63aba1907145bea998dd612889d6b",
|
||||
"GUID:01cf845b40a364e4cbaee058936fa4a3"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [
|
||||
"UNITY_EDITOR"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9f93c330bf016d047a49b0c29307ef42
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.WorldNodes
|
||||
{
|
||||
public class WorldNodeServiceEditor : EditorWindow
|
||||
{
|
||||
[MenuItem("Tools/Scenes/WorldNode Window")]
|
||||
public static void Open()
|
||||
{
|
||||
GetWindow<WorldNodeServiceEditor>().Show();
|
||||
}
|
||||
private List<ValueTuple<IWorldNode,IWorldNode>> _connections = new();
|
||||
private List<ValueTuple<IWorldNode, IWorldNode>> _blockConnections = new();
|
||||
|
||||
private Label reportLabel;
|
||||
private Toggle drawBlocked;
|
||||
private void OnEnable()
|
||||
{
|
||||
var checkButton = rootVisualElement.Create<Button>();
|
||||
checkButton.text = "Check";
|
||||
checkButton.clicked += Rebuild;
|
||||
|
||||
drawBlocked = rootVisualElement.Create<Toggle>();
|
||||
|
||||
reportLabel = rootVisualElement.Create<Label>();
|
||||
|
||||
drawBlocked.label = "Draw Blocked Connections";
|
||||
|
||||
WorldNodeService.OnDrawGizmo += DrawGizmo;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
WorldNodeService.OnDrawGizmo -= DrawGizmo;
|
||||
}
|
||||
|
||||
private void Rebuild()
|
||||
{
|
||||
var watch = System.Diagnostics.Stopwatch.StartNew();
|
||||
_connections.Clear();
|
||||
_blockConnections.Clear();
|
||||
//这里会在WorldNodeService中获取节点的数组,然后获取所有两个节点之间的组合
|
||||
var nodes = WorldNodeService.WorldNodes;
|
||||
foreach (var t in nodes)
|
||||
{
|
||||
foreach (var t1 in nodes)
|
||||
{
|
||||
if(t1==t)continue;
|
||||
if(Physics.Linecast(t.Position, t1.Position, out var hit))
|
||||
{
|
||||
_blockConnections.Add((t, t1));
|
||||
}
|
||||
else
|
||||
{
|
||||
_connections.Add((t, t1));
|
||||
}
|
||||
}
|
||||
}
|
||||
watch.Stop();
|
||||
reportLabel.text = $"Connections: {_connections.Count}";
|
||||
reportLabel.text += $"\nBlockConnections: {_blockConnections.Count}";
|
||||
reportLabel.text += $"\nTime: {watch.ElapsedMilliseconds}ms\tcompleted at {DateTime.Now:HH:mm:ss}";
|
||||
}
|
||||
private void DrawGizmo()
|
||||
{
|
||||
foreach (var x in WorldNodeService.WorldNodes)
|
||||
{
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawSphere(x.Position, 0.1f);
|
||||
}
|
||||
|
||||
foreach (var (x, y) in _connections)
|
||||
{
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawLine(x.Position, y.Position);
|
||||
}
|
||||
|
||||
|
||||
if (drawBlocked.value)
|
||||
{
|
||||
foreach (var (x, y) in _blockConnections)
|
||||
{
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawLine(x.Position, y.Position);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7b8483d77837cd64ba3bf1212b7fd8fe
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
62
Src/Unity/Scripts/WorldNode/WorldNode.cs
Normal file
62
Src/Unity/Scripts/WorldNode/WorldNode.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.WorldNodes
|
||||
{
|
||||
public interface IWorldNode
|
||||
{
|
||||
Vector3 Position { get; }
|
||||
}
|
||||
[ExecuteAlways]
|
||||
public class WorldNode : MonoBehaviour,IWorldNode
|
||||
{
|
||||
[SerializeField, ReadOnly] private string report;
|
||||
[SerializeField,ReadOnly] private bool isRegistered;
|
||||
private void OnEnable()
|
||||
{
|
||||
var reportBuilder = new StringBuilder();
|
||||
|
||||
reportBuilder.AppendLine(nameof(OnEnable));
|
||||
|
||||
var instanceId = GetInstanceID();
|
||||
reportBuilder.AppendLine($"InstanceId{instanceId}");
|
||||
|
||||
switch (instanceId)
|
||||
{
|
||||
case <0:
|
||||
isRegistered = true;
|
||||
WorldNodeService.WorldNodeList.Add(this);
|
||||
reportBuilder.AppendLine(report);
|
||||
break;
|
||||
case >0:
|
||||
reportBuilder.AppendLine($"WorldNode {name} will not registered");
|
||||
break;
|
||||
}
|
||||
report = reportBuilder.ToString();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
var reportBuilder = new StringBuilder();
|
||||
|
||||
reportBuilder.AppendLine(nameof(OnDisable));
|
||||
|
||||
if (isRegistered)
|
||||
{
|
||||
WorldNodeService.WorldNodeList.Remove(this);
|
||||
reportBuilder.AppendLine( $"WorldNode {name} unregistered");
|
||||
isRegistered = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
reportBuilder.AppendLine($"WorldNode {name} is not registered");
|
||||
}
|
||||
report = reportBuilder.ToString();
|
||||
}
|
||||
|
||||
public Vector3 Position => transform.position;
|
||||
}
|
||||
}
|
11
Src/Unity/Scripts/WorldNode/WorldNode.cs.meta
Normal file
11
Src/Unity/Scripts/WorldNode/WorldNode.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 28ecadca8974e094c8c8bc672a760ad6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
82
Src/Unity/Scripts/WorldNode/WorldNodeService.cs
Normal file
82
Src/Unity/Scripts/WorldNode/WorldNodeService.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.WorldNodes
|
||||
{
|
||||
public interface IWorldNodeService
|
||||
{
|
||||
IWorldNode[] WorldNodes { get; }
|
||||
IWorldNode[] GetNodes(Vector3 position, float radius);
|
||||
IWorldNode[] GetConnectedNodes(IWorldNode node);
|
||||
}
|
||||
public abstract class WorldNodeServiceImplementation : IWorldNodeService
|
||||
{
|
||||
protected virtual IWorldNodeService Service { get; }
|
||||
|
||||
public IWorldNode[] WorldNodes => Service.WorldNodes;
|
||||
|
||||
public IWorldNode[] GetNodes(Vector3 position, float radius)
|
||||
{
|
||||
return Service.GetNodes(position, radius);
|
||||
}
|
||||
|
||||
public IWorldNode[] GetConnectedNodes(IWorldNode node)
|
||||
{
|
||||
return Service.GetConnectedNodes(node);
|
||||
}
|
||||
}
|
||||
[ExecuteInEditMode]
|
||||
public class WorldNodeService : MonoBehaviour,IWorldNodeService
|
||||
{
|
||||
public static event Action OnDrawGizmo;
|
||||
public static readonly CacheList<IWorldNode> WorldNodeList = new();
|
||||
public static IWorldNode[] WorldNodes => WorldNodeList.ValueArray;
|
||||
public static IWorldNode[] GetNodes(Vector3 position, float radius)
|
||||
{
|
||||
return WorldNodes.Where(node => InRange(position, node, radius))
|
||||
.Where(node => InSight(position, node))
|
||||
.ToArray();
|
||||
}
|
||||
public static IWorldNode[] GetConnectedNodes(IWorldNode node)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
private static bool InRange(Vector3 position, IWorldNode node, float radius)
|
||||
{
|
||||
return Vector3.Distance(position, node.Position) <= radius;
|
||||
}
|
||||
private static bool InSight(Vector3 position, IWorldNode node)
|
||||
{
|
||||
if (Physics.Linecast(position, node.Position, out var hit))
|
||||
{
|
||||
return hit.transform == node.As<MonoBehaviour>().transform;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
IWorldNode[] IWorldNodeService.WorldNodes => WorldNodes;
|
||||
IWorldNode[] IWorldNodeService.GetNodes(Vector3 position, float radius)
|
||||
{
|
||||
return GetNodes(position, radius);
|
||||
}
|
||||
IWorldNode[] IWorldNodeService.GetConnectedNodes(IWorldNode node)
|
||||
{
|
||||
return GetConnectedNodes(node);
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
OnDrawGizmo?.Invoke();
|
||||
}
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
WorldNodeList.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
11
Src/Unity/Scripts/WorldNode/WorldNodeService.cs.meta
Normal file
11
Src/Unity/Scripts/WorldNode/WorldNodeService.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1edc46d4a7d336f41bc9f8c4a1bdecc5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user