您可以使用反射來你想要做什麼:
var method = typeof(GameObject).GetMethod("GetComponent", new Type[] {});
var specific = method.MakeGenericMethod(typeof(Rifle));
var instance = specific.Invoke(shooter, null) as IWeapon;
如。
using UnityEngine;
using System;
using System.Reflection;
using NUnit.Framework;
public interface IWeapon
{
void ShootAt(GameObject target);
}
public class Rifle : MonoBehaviour, IWeapon
{
public void ShootAt(GameObject target)
{
Debug.Log("Rifle");
}
}
public class Shotgun : MonoBehaviour, IWeapon
{
public void ShootAt(GameObject target)
{
Debug.Log("Shotgun");
}
}
public class WeaponTests
{
private GameObject Shooter()
{
var foo = new GameObject();
foo.AddComponent<Shotgun>();
foo.AddComponent<Rifle>();
return foo;
}
private MethodInfo method = null;
private IWeapon GetWeaponByName(GameObject shooter, string name)
{
if (method == null)
{
// This is slow, cache the result
method = typeof(GameObject).GetMethod("GetComponent", new Type[] {});
}
if (name == "Rifle")
{
MethodInfo specific = method.MakeGenericMethod(typeof(Rifle));
return specific.Invoke(shooter, null) as IWeapon;
}
else if (name == "Shotgun")
{
MethodInfo specific = method.MakeGenericMethod(typeof(Shotgun));
return specific.Invoke(shooter, null) as IWeapon;
}
return null;
}
[Test]
public void TestGetWeaponByName()
{
var target = new GameObject();
var fixture = Shooter();
IWeapon weapon;
weapon = GetWeaponByName(fixture, "Rifle");
Assert.True(weapon != null);
weapon.ShootAt(target);
weapon = GetWeaponByName(fixture, "Shotgun");
Assert.True(weapon != null);
weapon.ShootAt(target);
weapon = GetWeaponByName(fixture, "Laster");
Assert.True(weapon == null);
}
}
通知還有沒有特別需要IWeapon
。您可以輕鬆地直接加載特定實例:
MethodInfo specific = method.MakeGenericMethod(typeof(Rifle));
var rifle = specific.Invoke(shooter, null) as Rifle;
rifle.RifleSpecificThing();
感謝您的支持。由於IWeapon不能從Monobehaviour派生出來,因此我需要使用Monobehaviour方法來如何使用Weapon作爲基類?我會像公共類Weapon:MonoBehaviour,IWeapon和派生類像public class Weapon_Rifle:Weapon? – Absinthe
實際上,在Unity中,一個類不能從其他兩個類派生出來,但是您可以實現儘可能多的接口,以及從MonoBehaviour派生的接口。這兩個武器類仍然繼承MonoBehaviour,以便仍然是組件,所以它們都是Weapon_X:MonoBehaviour,IWeapon。如果你願意的話,你可以在Unity的網站[這裏](https://unity3d.com/learn/tutorials/modules/intermediate/scripting/interfaces)查看關於界面的介紹。 – cjmarsh
謝謝,所以我會設置類似武器類型的狀態機 – Absinthe