Skip to content

Commit

Permalink
Enableable 重命名为 Enable
Browse files Browse the repository at this point in the history
  • Loading branch information
KumoKyaku committed Sep 12, 2023
1 parent 0ad244d commit 09857f9
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public sealed override bool Match<T>(T input)
}

[Serializable]
public class EnableableConditionSO : Enableable
public class EnableableConditionSO : Enable
{
[SupportTypes(typeof(ConditionSO),
AllowAbstract = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SceneSwitcher : MonoBehaviour
[Header("Editor")]
public SceneAsset TargetScene;
public OpenSceneMode OpenSceneMode = OpenSceneMode.Single;
public Megumin.Enableable<KeyCode> Key = new Megumin.Enableable<KeyCode>(true, KeyCode.F4);
public Megumin.Enable<KeyCode> Key = new Megumin.Enable<KeyCode>(true, KeyCode.F4);
#endif

[Space]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,38 @@

namespace Megumin
{
[Serializable]
public class EnableableFrame : Enableable
{
[FrameAndTime]
public int Value;

//public bool CheckAlive(long startFID, long frameID)
//{
// if (Enabled)
// {
// return element.Config.CacheVaildFrame.Value + element.WaitStartFID
// >= frameID
// }
// return true;
//}
}

[Serializable]
public class EnableableHDRColor : Enableable
{
[ColorUsage(true, true)]
public Color Value;
}

/// <summary>
/// 可启用的,继承后字段值名字一定要是Value.
/// 解决Value不能设置特性的问题.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <remarks><see cref="Nullable{T}"/>无法序列化,不能写成特性.</remarks>
[Serializable]
[Obsolete("use Enable instead", false)]
public abstract class Enableable
{
[SerializeField]
Expand All @@ -31,28 +56,78 @@ public static implicit operator bool(Enableable activeable)
}
}

/// <summary>
/// 可启用的
/// </summary>
/// <typeparam name="T"></typeparam>
/// <see cref="Nullable{T}"/>无法序列化,不能写成特性.
[Serializable]
public class EnableableFrame : Enableable
[Obsolete("use Enable<T> instead", false)]
public class Enableable<T> : Enableable
{
[FrameAndTime]
public int Value;
[SerializeField]
public T Value;

//public bool CheckAlive(long startFID, long frameID)
public Enableable() : this(true, default)
{

}

public Enableable(bool enabled = true, T def = default)
{
Enabled = enabled;
Value = def;
}

public static implicit operator T(Enableable<T> activeable)
{
return activeable.Value;
}

public override bool HasValue
{
get
{
if (!Enabled)
{
return false;
}
else
{
return Value != null;
}
}
}

//public static implicit operator Nullable<T>(Enableable<T> activeable)
// where T:struct
//{
// if (Enabled)
// {
// return element.Config.CacheVaildFrame.Value + element.WaitStartFID
// >= frameID
// }
// return true;
// return default;
//}
}

/// <summary>
/// 可启用的,继承后字段值名字一定要是Value.
/// 解决Value不能设置特性的问题.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <remarks><see cref="Nullable{T}"/>无法序列化,不能写成特性.</remarks>
[Serializable]
public class EnableableHDRColor : Enableable
public abstract class Enable
{
[ColorUsage(true, true)]
public Color Value;
[SerializeField]
[UnityEngine.Serialization.FormerlySerializedAs("Active")]
public bool Enabled = true;

/// <summary>
/// <inheritdoc cref="Nullable{T}.HasValue"/>
/// </summary>
public virtual bool HasValue => Enabled;

public static implicit operator bool(Enable activeable)
{
return activeable?.Enabled ?? false;
}
}

/// <summary>
Expand All @@ -61,18 +136,23 @@ public class EnableableHDRColor : Enableable
/// <typeparam name="T"></typeparam>
/// <see cref="Nullable{T}"/>无法序列化,不能写成特性.
[Serializable]
public class Enableable<T> : Enableable
public class Enable<T> : Enable
{
[SerializeField]
public T Value;

public Enableable(bool enabled = true, T def = default)
public Enable() : this(true, default)
{

}

public Enable(bool enabled = true, T def = default)
{
Enabled = enabled;
Value = def;
}

public static implicit operator T(Enableable<T> activeable)
public static implicit operator T(Enable<T> activeable)
{
return activeable.Value;
}
Expand All @@ -92,7 +172,7 @@ public override bool HasValue
}
}

//public static implicit operator Nullable<T>(Enableable<T> activeable)
//public static implicit operator Nullable<T>(Enable<T> activeable)
// where T:struct
//{
// return default;
Expand All @@ -108,6 +188,8 @@ namespace UnityEditor.Megumin
using UnityEditor;

#if !DISABLE_MEGUMIN_PROPERTYDRWAER
[CustomPropertyDrawer(typeof(Enable), true)]
[CustomPropertyDrawer(typeof(Enable<>), true)]
[CustomPropertyDrawer(typeof(Enableable), true)]
[CustomPropertyDrawer(typeof(Enableable<>), true)]
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if !MEGUMIN_Common
#if !MEGUMIN_Common

using System.Collections;
using System.Collections.Generic;
Expand All @@ -10,8 +10,9 @@ namespace Megumin
[Serializable]
public class GameObjectFilter
{
public Enableable<LayerMask> LayerMask = new Enableable<LayerMask>(false, 0);
public Enableable<TagMask> TagMask = new Enableable<TagMask>(false, new TagMask());
public Enable<LayerMask> LayerMask = new(false, 0);
public Enable<TagMask> TagMask = new(false, new TagMask());
public Enable<List<GameObject>> Exclude = new(false, null);

public bool Check(Component component)
{
Expand All @@ -25,17 +26,28 @@ public bool Check(Component component)
}
}

/// <summary>
///
/// </summary>
/// <param name="gameObject"></param>
/// <returns>
/// 同时满足 LayerMask含有,TagMask含有,Exclude不含有,三个条件返回true。
/// </returns>
public bool Check(GameObject gameObject)
{
if (gameObject)
{
if (LayerMask.Enabled)
if (Exclude.HasValue)
{
if ((1 << gameObject.layer & LayerMask.Value) != 0)
if (Exclude.Value.Contains(gameObject))
{
return true;
return false;
}
else
}

if (LayerMask.Enabled)
{
if ((1 << gameObject.layer & LayerMask.Value) == 0)
{
return false;
}
Expand All @@ -54,6 +66,13 @@ public bool Check(GameObject gameObject)
}
}

/// <summary>
///
/// </summary>
/// <param name="gameObject"></param>
/// <returns>
/// TagMask含有目标tag返回true。
/// </returns>
public bool CheckTag(GameObject gameObject)
{
if (gameObject)
Expand All @@ -71,6 +90,36 @@ public bool CheckTag(GameObject gameObject)
}
}

/// <summary>
/// </summary>
/// <param name="gameObject"></param>
/// <returns>
/// Exclude不包含目标,返回true。
/// Exclude包含目标 或 gameObject为空,返回false
/// </returns>
public bool CheckExclude(GameObject gameObject)
{
if (gameObject)
{
if (Exclude.HasValue)
{
return Exclude.Value.Contains(gameObject) == false;
}
return true;
}
else
{
return false;
}
}

/// <summary>
///
/// </summary>
/// <param name="gameObject"></param>
/// <returns>
/// LayerMask含有目标layer返回true。
/// </returns>
public bool CheckLayer(GameObject gameObject)
{
if (gameObject)
Expand All @@ -94,6 +143,55 @@ public bool CheckLayer(GameObject gameObject)
return false;
}
}

/// <summary>
/// 物理测试仅能在主线程调用,这里不用考虑hitColliders多线程访问问题。
/// </summary>
static Collider[] hitColliders = null;
public virtual bool TryPhysicsTest(Vector3 position,
float radius,
ICollection<Collider> results,
Func<Collider, bool> checkCollider = null,
int maxColliders = 20)
{

if (hitColliders == null || hitColliders.Length < maxColliders)
{
hitColliders = new Collider[maxColliders];
}

var layerMask = -1;
if (this.LayerMask.Enabled)
{
layerMask = this.LayerMask.Value;
}

int numColliders = Physics.OverlapSphereNonAlloc(position, radius, hitColliders, layerMask);

for (int i = 0; i < numColliders; i++)
{
var collider = hitColliders[i];
var go = collider.gameObject;
if (this.CheckTag(go)
&& this.CheckExclude(go))
{
if (checkCollider == null)
{
results.Add(collider);
}
else
{
if (checkCollider(collider))
{
results.Add(collider);
}
}
}
}
Array.Clear(hitColliders, 0, hitColliders.Length);

return true;
}
}

}
Expand Down

0 comments on commit 09857f9

Please sign in to comment.