让我先介绍一下我在做什么。
在Unity中,我想制作一些GameObject
(例如播放器)来拾取物品(其他GameObject
s)。
为此,我设计了以下基本代码:
一个用来拉起和捡起物品的组件:
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%的不确定性,我不确定是否应该承担使用它的风险。我不是在寻找最佳性能,而只是解决此问题的正确方法。
答案 0 :(得分:1)
我确实认为最好的方法是在Pickup中发送对您的Player对象的引用,然后对每种Pickup对象类型进行自定义逻辑。我可能会跳过该接口,而只拥有一个名为“ PickupObject”的基础对象,然后让FindClosestItemInRange返回这些对象。
最后,您应该销毁GameObject,而不要传递给Pickup函数。您可能可以销毁Transform并获得相同的结果(我没有尝试过),但是实际上销毁GameObject而不是GameObject的任何组件只是一种好习惯
public class PickupObject : MonoBehaviour
{
virtual void Pickup(Player playerObject) { }
}
public class Coin : PickupObject
{
public int price;
override void Pickup(Player playerObject)
{
playerObject.money += price; // Move money over to the player as it probably makes more sense
}
}
public class HealthPack : PickupObject
{
public int healthRestored;
override void Pickup(Player playerObject)
{
playerObject.health += healthRestored;
}
}
public class PickupMagnet : MonoBehaviour
{
public Player PlayerObject;
private void Update()
{
PickupObject item = FindClosestItemInRange();
Pickup(item);
}
private void Pickup(PickupObject pickup)
{
pickup.Pickup(PlayerObject);
Destroy(pickup.gameObject);
}
}
编辑,对您的代码有一些一般想法:
如果您有通用的“ LivingObject”,可以按照代码的建议同时获取生命值和硬币,那么您可能会泛化得太多。听起来您只有一个需要拿起东西的球员。根据我的经验,让“任何东西”能够拾取“任何东西”只是一种概括。不要尝试在第一行代码中解决所有未来的问题。如果您不确定要去哪里,或者在这个早期阶段结构不妙。编写执行所需功能的代码,并在出现模式和重复时重构它。