upgrade
This commit is contained in:
@@ -39,7 +39,7 @@ namespace BITKit.Sensors
|
||||
return _sensorImplementation.Execute();
|
||||
}
|
||||
}
|
||||
public abstract class Sensor : BITBehavior, ISensor
|
||||
public abstract class Sensor : MonoBehaviour, ISensor
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
public LayerMask detectLayer;
|
||||
|
@@ -4,7 +4,8 @@
|
||||
"references": [
|
||||
"GUID:a209c53514018594f9f482516f2a6781",
|
||||
"GUID:508392158bd966c4d9c21e19661a441d",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23"
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23",
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
|
@@ -1,47 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UnityEngine.UIElements;
|
||||
using UnityEditor.UIElements;
|
||||
namespace BITKit.Sensors.Editors
|
||||
{
|
||||
/* [CustomEditor(typeof(Sensor), true)]
|
||||
public class SensorInspecer : Editor
|
||||
{
|
||||
public override VisualElement CreateInspectorGUI()
|
||||
{
|
||||
VisualElement container = new();
|
||||
InspectorElement.FillDefaultInspector(container, serializedObject, this);
|
||||
|
||||
var sensor = serializedObject.targetObject as Sensor;
|
||||
|
||||
VisualElement listElement = new();
|
||||
Refresh();
|
||||
Button button = new(() =>
|
||||
{
|
||||
sensor.Excute();
|
||||
Refresh();
|
||||
});
|
||||
button.text = "Test";
|
||||
|
||||
container.Add(listElement);
|
||||
container.Add(button);
|
||||
return container;
|
||||
|
||||
void Refresh()
|
||||
{
|
||||
listElement.Clear();
|
||||
listElement.Add(new Label("Detected:"));
|
||||
sensor.detecteds.ForEach(x =>
|
||||
{
|
||||
var objectFiled = new ObjectField();
|
||||
objectFiled.objectType = typeof(Transform);
|
||||
objectFiled.value = x;
|
||||
listElement.Add(objectFiled);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
} */
|
||||
}
|
||||
|
@@ -1,10 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
using UnityEngine.UIElements;
|
||||
#if UNITY_EDITOR
|
||||
using System.CodeDom;
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
#endif
|
||||
namespace BITKit.Sensors
|
||||
{
|
||||
public class TriggerSensor : MonoBehaviour, ISensor
|
||||
@@ -12,28 +17,35 @@ namespace BITKit.Sensors
|
||||
[Header(Constant.Header.Settings)] [SerializeField]
|
||||
private List<GameObject> ignores = new();
|
||||
|
||||
[SerializeField] private Optional<LayerMask> detectedLayer;
|
||||
[SerializeField] private bool allowStatic;
|
||||
|
||||
[Header(Constant.Header.Events)] public UnityEvent<Collider> onDetected = new();
|
||||
public UnityEvent<Collider> onLost = new();
|
||||
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
// ReSharper disable once FieldCanBeMadeReadOnly.Local
|
||||
private List<Collider> detected = new();
|
||||
|
||||
private Queue<Collider> triggerEnterQueue=new();
|
||||
private Queue<Collider> triggerExitQueue=new();
|
||||
|
||||
|
||||
private void OnTriggerEnter(Collider _collider)
|
||||
{
|
||||
if (_collider.gameObject.isStatic) return;
|
||||
if (IsValid(_collider) is false) return;
|
||||
if (detected.Contains(_collider)) return;
|
||||
detected.Add(_collider);
|
||||
onDetected.Invoke(_collider);
|
||||
triggerEnterQueue.Enqueue(_collider);
|
||||
|
||||
// if (IsValid(_collider) is false) return;
|
||||
// if (detected.Contains(_collider)) return;
|
||||
// detected.Add(_collider);
|
||||
// onDetected.Invoke(_collider);
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider _collider)
|
||||
{
|
||||
if (_collider.gameObject.isStatic) return;
|
||||
if (IsValid(_collider) is false) return;
|
||||
if (!detected.Remove(_collider)) return;
|
||||
onLost.Invoke(_collider);
|
||||
triggerExitQueue.Enqueue(_collider);
|
||||
// if (IsValid(_collider) is false) return;
|
||||
// if (!detected.Remove(_collider)) return;
|
||||
// onLost.Invoke(_collider);
|
||||
}
|
||||
|
||||
private void OnCollisionEnter(Collision collision)
|
||||
@@ -55,7 +67,13 @@ namespace BITKit.Sensors
|
||||
|
||||
public IEnumerable<Transform> Get() => detected.Select(x => x.transform).ToArray();
|
||||
|
||||
public bool IsValid(Collider _collider) => ignores.Contains(_collider.gameObject) is false;
|
||||
//public bool IsValid(Collider _collider) => ignores.Contains(_collider.gameObject) is false;
|
||||
public bool IsValid(Collider _collider)
|
||||
{
|
||||
if(allowStatic is false && _collider.gameObject.isStatic) return false;
|
||||
if (ignores.Contains(_collider.gameObject)) return false;
|
||||
return !detectedLayer.Allow || detectedLayer.Value.Includes(_collider.gameObject.layer);
|
||||
}
|
||||
|
||||
public float GetDistance()
|
||||
{
|
||||
@@ -66,5 +84,51 @@ namespace BITKit.Sensors
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
while (triggerEnterQueue.TryDequeue(out var _collider))
|
||||
{
|
||||
if (IsValid(_collider) is false) return;
|
||||
if (triggerExitQueue.Contains(_collider) || detected.Contains(_collider))continue;
|
||||
detected.Add(_collider);
|
||||
}
|
||||
while (triggerExitQueue.TryDequeue(out var _collider))
|
||||
{
|
||||
if (IsValid(_collider) is false) return;
|
||||
detected.Remove(_collider);
|
||||
}
|
||||
}
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[CustomEditor(typeof(TriggerSensor))]
|
||||
public class TriggerSensorInspector:BITInspector<TriggerSensor>
|
||||
{
|
||||
private VisualElement detectedContainer;
|
||||
public override VisualElement CreateInspectorGUI()
|
||||
{
|
||||
FillDefaultInspector();
|
||||
|
||||
detectedContainer = root.Create<VisualElement>();
|
||||
|
||||
return root;
|
||||
}
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
if (detectedContainer is null) return;
|
||||
detectedContainer.Clear();
|
||||
foreach (var x in agent.Get())
|
||||
{
|
||||
|
||||
ObjectField objectField = new()
|
||||
{
|
||||
objectType = x.GetType(),
|
||||
value = x,
|
||||
};
|
||||
objectField.SetEnabled(false);
|
||||
detectedContainer.Add(objectField);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
@@ -20,4 +20,6 @@ namespace BITKit.UX
|
||||
public const string ContextListView = "context-listview";
|
||||
public const string Inspector = "inspector-container";
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -17,6 +17,14 @@ namespace BITKit
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class BITEditorUtils
|
||||
{
|
||||
public const string InspectorPath = "Assets/BITKit/Unity/Common/UX/BITInspector.uss";
|
||||
#if UNITY_EDITOR
|
||||
public static StyleSheet InspectorStyleSheet => AssetDatabase.LoadAssetAtPath<StyleSheet>(InspectorPath);
|
||||
#endif
|
||||
}
|
||||
public class ServerRpcAttribute : System.Attribute
|
||||
{
|
||||
|
||||
@@ -79,7 +87,7 @@ namespace BITKit
|
||||
void OnEnable()
|
||||
{
|
||||
//StyleSheet css = Addressables.LoadAssetAsync<StyleSheet>(ussName).WaitForCompletion();
|
||||
StyleSheet css = AssetDatabase.LoadAssetAtPath<StyleSheet>($"Assets/BITKit/Unity/Common/UX/BITInspector.uss");
|
||||
StyleSheet css = AssetDatabase.LoadAssetAtPath<StyleSheet>(BITEditorUtils.InspectorPath);
|
||||
|
||||
root.root.styleSheets.Add(css);
|
||||
|
||||
|
Reference in New Issue
Block a user