更改文件架构
This commit is contained in:
49
Packages/Common~/Scripts/Sensor/AudioSensor.cs
Normal file
49
Packages/Common~/Scripts/Sensor/AudioSensor.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Pool;
|
||||
using System.Linq;
|
||||
namespace BITKit.Sensors
|
||||
{
|
||||
public interface IAudioObject
|
||||
{
|
||||
float GetVolume();
|
||||
}
|
||||
public class AudioSensor : Sensor
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
public float radius;
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
IAudioObject currentAudioObject;
|
||||
Collider currentCollider;
|
||||
Collider[] colliders = new Collider[32];
|
||||
public override IEnumerable<Transform> Get() => detecteds;
|
||||
public override UniTask Excute()
|
||||
{
|
||||
var cacheList = ListPool<Transform>.Get();
|
||||
for (int i = 0; i < Physics.OverlapSphereNonAlloc(transform.position, radius, colliders, detectLayer); i++)
|
||||
{
|
||||
currentCollider = colliders[i];
|
||||
if (IsValid(currentCollider))
|
||||
{
|
||||
cacheList.Add(currentCollider.transform);
|
||||
}
|
||||
}
|
||||
detecteds = cacheList.ToArray();
|
||||
ListPool<Transform>.Release(cacheList);
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
public override bool IsValid(Collider collider)
|
||||
{
|
||||
if (ignoreColliders.Contains(collider) is false)
|
||||
if (Vector3.Distance(transform.position, collider.transform.position) <= radius)
|
||||
if (collider.TryGetComponent<IAudioObject>(out currentAudioObject))
|
||||
{
|
||||
return currentAudioObject.GetVolume() >= 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public override float GetDistance() => radius;
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Sensor/AudioSensor.cs.meta
Normal file
11
Packages/Common~/Scripts/Sensor/AudioSensor.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d7841c9177d83e54594af3d7a9ec4b08
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
23
Packages/Common~/Scripts/Sensor/BITKit.Sensor.asmdef
Normal file
23
Packages/Common~/Scripts/Sensor/BITKit.Sensor.asmdef
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "BITKit.Sensor",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:a209c53514018594f9f482516f2a6781",
|
||||
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
|
||||
"GUID:d8b63aba1907145bea998dd612889d6b",
|
||||
"GUID:274d4ecae4648e94c8b2cee7218378a0",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23",
|
||||
"GUID:be17a8778dbfe454890ed8279279e153",
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:9400d40641bab5b4a9702f65bf5c6eb5"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 508392158bd966c4d9c21e19661a441d
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
8
Packages/Common~/Scripts/Sensor/Core.meta
Normal file
8
Packages/Common~/Scripts/Sensor/Core.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f5c1c924fb36d8e4e9d5432f50390fe5
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
31
Packages/Common~/Scripts/Sensor/Core/Sensor.cs
Normal file
31
Packages/Common~/Scripts/Sensor/Core/Sensor.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using BITKit;
|
||||
using Cysharp.Threading.Tasks;
|
||||
namespace BITKit.Sensors
|
||||
{
|
||||
public interface ISensor
|
||||
{
|
||||
IEnumerable<Transform> Get();
|
||||
bool IsValid(Collider collider);
|
||||
float GetDistance();
|
||||
UniTask Excute();
|
||||
}
|
||||
public abstract class Sensor : BITBehavior, ISensor
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
public LayerMask detectLayer;
|
||||
[Header(Constant.Header.Gameobjects)]
|
||||
public Collider[] ignoreColliders;
|
||||
[Header(Constant.Header.Components)]
|
||||
public Sensor[] subSensors;
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
[System.NonSerialized]
|
||||
public Transform[] detecteds = new Transform[0];
|
||||
public abstract IEnumerable<Transform> Get();
|
||||
public abstract bool IsValid(Collider collider);
|
||||
public abstract UniTask Excute();
|
||||
public abstract float GetDistance();
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Sensor/Core/Sensor.cs.meta
Normal file
11
Packages/Common~/Scripts/Sensor/Core/Sensor.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 48b73c8bf22a30a4788d2f577064076b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
8
Packages/Common~/Scripts/Sensor/Editor.meta
Normal file
8
Packages/Common~/Scripts/Sensor/Editor.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2391d96dea4c2e844aad567601a85e1c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "BITKit.Sensor.Editor",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:a209c53514018594f9f482516f2a6781",
|
||||
"GUID:508392158bd966c4d9c21e19661a441d",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [
|
||||
"UNITY_EDITOR"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b878544b8c374d44b88df186caa604bb
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
11
Packages/Common~/Scripts/Sensor/Editor/SensorDrawer.cs.meta
Normal file
11
Packages/Common~/Scripts/Sensor/Editor/SensorDrawer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e531e4ce5cb6594f9e754292deb522f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
47
Packages/Common~/Scripts/Sensor/Editor/SensorInspector.cs
Normal file
47
Packages/Common~/Scripts/Sensor/Editor/SensorInspector.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
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);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
} */
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 025bd668d6ca81a41b2ec7d0904c6e46
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
123
Packages/Common~/Scripts/Sensor/RangeSensor.cs
Normal file
123
Packages/Common~/Scripts/Sensor/RangeSensor.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Linq;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.Jobs;
|
||||
using UnityEngine.Pool;
|
||||
namespace BITKit.Sensors
|
||||
{
|
||||
public class RangeSensor : Sensor
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
public float radius;
|
||||
public int fov;
|
||||
public bool requireSight;
|
||||
[Header(Constant.Header.Settings)]
|
||||
public LayerMask blockLayer;
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
FrameUpdate frameUpdater;
|
||||
Collider[] colliders = new Collider[32];
|
||||
RaycastHit[] hits;
|
||||
Collider currentCollider;
|
||||
Location location;
|
||||
int length;
|
||||
Vector3 dir;
|
||||
float maxDistance;
|
||||
public override IEnumerable<Transform> Get() => detecteds;
|
||||
public override UniTask Excute()
|
||||
{
|
||||
if (frameUpdater)
|
||||
{
|
||||
if (maxDistance is 0)
|
||||
{
|
||||
maxDistance = Mathf.Max(subSensors.Select(x => x.GetDistance()).Append(radius).ToArray());
|
||||
}
|
||||
location.position = transform.position;
|
||||
location.rotation = transform.rotation;
|
||||
var list = ListPool<Transform>.Get();
|
||||
for (int i = 0; i < Physics.OverlapSphereNonAlloc(location, maxDistance, colliders, detectLayer); i++)
|
||||
{
|
||||
currentCollider = colliders[i];
|
||||
if (IsValid(currentCollider))
|
||||
list.Add(currentCollider.transform);
|
||||
else
|
||||
{
|
||||
foreach (var sensor in subSensors)
|
||||
{
|
||||
if (sensor.IsValid(currentCollider))
|
||||
{
|
||||
list.Add(currentCollider.transform);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
detecteds = list.ToArray();
|
||||
list.Clear();
|
||||
ListPool<Transform>.Release(list);
|
||||
}
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
public override bool IsValid(Collider collider)
|
||||
{
|
||||
if (ignoreColliders.Contains(collider)
|
||||
|| CheckFov(ref collider) is false
|
||||
|| CheckSight(ref collider) is false
|
||||
|| CheckDistance(ref collider) is false
|
||||
)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else return true;
|
||||
}
|
||||
public override float GetDistance() => radius;
|
||||
bool CheckFov(ref Collider collider)
|
||||
{
|
||||
if (fov is not 0)
|
||||
{
|
||||
var _dir = collider.transform.position - transform.position;
|
||||
if (_dir.sqrMagnitude > 0)
|
||||
{
|
||||
var dir = Quaternion.LookRotation(_dir);
|
||||
if (Vector3.Dot(transform.forward, _dir) > 0 && fov > Quaternion.Angle(transform.rotation, dir))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
else return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
bool CheckSight(ref Collider collider)
|
||||
{
|
||||
if (requireSight)
|
||||
{
|
||||
length = Physics.RaycastNonAlloc(
|
||||
location.position,
|
||||
collider.transform.position - location,
|
||||
hits,
|
||||
Vector3.Distance(location, collider.transform.position),
|
||||
blockLayer
|
||||
);
|
||||
if (length > 0)
|
||||
{
|
||||
if (hits[0].collider == collider)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool CheckDistance(ref Collider collider)
|
||||
{
|
||||
return Vector3.Distance(collider.transform.position, transform.position) <= radius;
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Sensor/RangeSensor.cs.meta
Normal file
11
Packages/Common~/Scripts/Sensor/RangeSensor.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8f305912600022b4c824367828ddc333
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
29
Packages/Common~/Scripts/Sensor/RaySensor.cs
Normal file
29
Packages/Common~/Scripts/Sensor/RaySensor.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
namespace BITKit.Sensors
|
||||
{
|
||||
public class RaySensor : Sensor
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
public float distance;
|
||||
public override IEnumerable<Transform> Get() => detecteds;
|
||||
public override UniTask Excute()
|
||||
{
|
||||
if (Physics.Raycast(transform.position, transform.forward, out var rayhit, distance, detectLayer))
|
||||
{
|
||||
detecteds = new Transform[]{
|
||||
rayhit.transform
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
detecteds = new Transform[0];
|
||||
}
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
public override bool IsValid(Collider collider) => true;
|
||||
public override float GetDistance() => distance;
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Sensor/RaySensor.cs.meta
Normal file
11
Packages/Common~/Scripts/Sensor/RaySensor.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d84842fd060cf0d4f9d37b89de002e94
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
48
Packages/Common~/Scripts/Sensor/TriggerSensor.cs
Normal file
48
Packages/Common~/Scripts/Sensor/TriggerSensor.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
namespace BITKit.Sensors
|
||||
{
|
||||
public class TriggerSensor : MonoBehaviour
|
||||
{
|
||||
[Header(Constant.Header.Events)]
|
||||
public UnityEvent<Collider> onDetected = new();
|
||||
public UnityEvent<Collider> onLost = new();
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
List<Collider> _detected = new();
|
||||
void OnTriggerEnter(Collider collider)
|
||||
{
|
||||
if (IsValid(collider))
|
||||
{
|
||||
_detected.Add(collider);
|
||||
onDetected.Invoke(collider);
|
||||
}
|
||||
}
|
||||
void OnTriggerExit(Collider collider)
|
||||
{
|
||||
if (_detected.TryRemove(collider))
|
||||
{
|
||||
onLost.Invoke(collider);
|
||||
};
|
||||
}
|
||||
void OnCollisionEnter(Collision collision)
|
||||
{
|
||||
var collider = collision.collider;
|
||||
if (IsValid(collider))
|
||||
{
|
||||
_detected.Add(collider);
|
||||
onDetected.Invoke(collider);
|
||||
}
|
||||
}
|
||||
void OnCollisionStay(Collision collision)
|
||||
{
|
||||
var collider = collision.collider;
|
||||
if (_detected.TryRemove(collider))
|
||||
{
|
||||
onLost.Invoke(collider);
|
||||
};
|
||||
}
|
||||
bool IsValid(Component self)=>true;
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Sensor/TriggerSensor.cs.meta
Normal file
11
Packages/Common~/Scripts/Sensor/TriggerSensor.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 099996f054b64cb499431974f59539f2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user