【发布时间】:2019-06-30 14:58:02
【问题描述】:
让我先介绍一下我在做什么。
在 Unity 中,我想让一些 GameObjects(比如玩家)能够拾取物品(其他 GameObjects)。
为了做到这一点,我设计了这个基本代码:
一个拉取物品的组件:
public class PickupMagnet : MonoBehaviour
{
// [...]
private void Update()
{
Transform item = FindClosestItemInRange(); // Well, this line doesn't exist, it's just a simplification.
if (ítem != null)
Pickup(item);
}
private void Pickup(Transform item)
{
IPickup pickup = item.GetComponent<IPickup>();
if (pickup != null)
{
pickup.Pickup();
Destroy(item);
}
}
}
那些(那个目前)项目的界面:
public interface IPickup
{
void Pickup();
// [...]
}
还有我当时做的单品:
public class Coin : MonoBehaviour, IPickup
{
private int price;
// [...]
void IPickup.Pickup()
{
Global.money += price; // Increase player money
}
// [...]
}
在我想添加一个新项目之前,一切都很好:一个健康包。该物品会增加拾取它的生物的生命值。但为了做到这一点,我需要生物脚本的实例:LivingObject。
public class HealthPack: MonoBehaviour, IPickup
{
private int healthRestored;
// [...]
void IPickup.Pickup(LivingObject livingObject)
{
livingObject.TakeHealing(healthRestored);
}
// [...]
}
问题是IPickup.Pickup() 上面没有任何参数。显然,我可以将其更改为IPickup.Pickup(LivingObject livingObject) 并忽略Coin.Pickup 上的参数,但是如果将来我想添加更多种类的项目,需要不同的参数怎么办?
其他选择是向接口添加一个新方法,但这迫使我实现 Coin.Pickup(LivingObject livingObject) 并实现它。
经过考虑,我删除了 IPickup 并将其替换为:
public abstract class Pickupable : MonoBehaviour
{
// [...]
public abstract bool ShouldBeDestroyedOnPickup { get; }
public virtual void Pickup() => throw new NotImplementedException();
public virtual void Pickup(LivingObject livingObject) => throw new NotImplementedException();
}
然后覆盖Coin和HealthPack中的必要方法。另外,我将PickupMagnet.Pickup(Transform item) 更改为:
public class PickupMagnet : MonoBehaviour
{
// [...]
private LivingObject livingObject;
private void Start()
{
livingObject = gameObject.GetComponent<LivingObject>();
}
// [...]
private void Pickup(Transform item)
{
Pickupable pickup = item.GetComponent<Pickupable>();
if (pickup != null)
{
Action[] actions = new Action[] { pickup.Pickup, () => pickup.Pickup(livingObject) };
bool hasFoundImplementedMethod = false;
foreach (Action action in actions)
{
try
{
action();
hasFoundImplementedMethod = true;
break;
}
catch (NotImplementedException) { }
}
if (!hasFoundImplementedMethod)
throw new NotImplementedException($"The {item.gameObject}'s {nameof(Pickup)} class lack of any Pickup method implementation.");
else if (pickup.ShouldBeDestroyedOnPickup)
Destroy(item.gameObject);
}
}
}
基本上,这会遍历actions 中定义的所有方法并执行它们。如果他们提出NotImplementedException,它会继续尝试使用数组中的其他方法。
这段代码运行良好,但就个人而言,我不喜欢用Pickable.Pickup 的每个重载来定义该数组。
所以,我开始做一些研究,我发现了一种叫做“反思”的东西。我仍然不确定它是如何深入工作的,但我设法制作了这个工作代码。
private void Pickup(Transform item)
{
Pickupable pickup = item.GetComponent<Pickupable>();
if (pickup != null)
{
bool hasFoundImplementedMethod = false;
foreach (MethodInfo method in typeof(Pickupable).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
if (method.Name == "Pickup")
{
ParameterInfo[] parametersGetted = method.GetParameters();
int parametersAmount = parametersGetted.Length;
object[] parametersObjects = new object[parametersAmount ];
for (int i = 0; i < parametersAmount; i++)
{
Type parameterType = parametersGetted[i].ParameterType;
if (parameters.TryGetValue(parameterType, out object parameterObject))
parametersObjects[i] = parameterObject;
else
throw new KeyNotFoundException($"The key Type {parameterType} was not found in the {nameof(parameters)} dictionary.");
}
bool succed = TryCatchInvoke(pickup, method, parametersObjects);
if (succed) hasFoundImplementedMethod = true;
}
}
if (!hasFoundImplementedMethod)
throw new NotImplementedException($"The {item.gameObject}'s {nameof(Pickup)} class lack of any Pickup method implementation.");
else if (pickup.ShouldBeDestroyedOnPickup)
Destroy(item.gameObject);
}
}
private bool TryCatchInvoke(Pickupable instance, MethodInfo method, object[] args)
{
try
{
method.Invoke(instance, args);
return true;
}
catch (Exception) // NotImplementedException doesn't work...
{
return false;
}
}
并添加到MagnetPickup:
private LivingObject livingObject;
private Dictionary<Type, object> parameters;
private void Start()
{
livingObject = gameObject.GetComponent<LivingObject>();
parameters = new Dictionary<Type, object> { { typeof(LivingObject), livingObject } };
}
...并且有效。
我对 Unity 分析器不是很熟悉,但我认为最后一个代码的运行速度要快一点(不到 1%)。
问题是我不确定该代码将来是否会给我带来问题,所以这是我的问题:反射是解决此问题的正确方法还是应该使用我的 try/catch 尝试或者其他代码?
仅 1% 我不确定是否应该冒险使用它。我不是在寻找最好的性能,只是在寻找解决这个问题的正确方法。
【问题讨论】:
-
只有
pickup.Pickup(LivingObject);有什么问题?对于您不会使用参数的硬币,它们只会给玩家钱。或者就此而言,为什么健康包需要一个活物:只有玩家在捡硬币,所以除了玩家之外没有人是相关的活物。 -
@Draco18s 我想我试图概括太多,就像 Mockarutan 回答说的那样。
标签: c# oop unity3d reflection system.reflection