是否可以从接口获取对游戏对象的引用?

Ada*_*tox 3 c# unity-game-engine

我已经IVehicle为我的游戏中的所有车辆实现了一个接口,并且希望能够gameObject在不使用gameObject属性的情况下获得对其脚本附加的引用。这可能吗?

就像是:

IVehicle vehicle = GameObject.FindObjectOfType(typeof(IVehicle));
GameObject vehicleGO = vehicle.gameObject;
Run Code Online (Sandbox Code Playgroud)

zam*_*ari 7

是的,有一个非常简单的解决方案。只是地方

GameObject gameObject { get ; } 
Run Code Online (Sandbox Code Playgroud)

作为界面的一部分 - 所有 MonoBehaviours 都实现它,不需要额外的步骤。我将它添加到 Unity 中几乎所有的界面中,它不花钱而且有很大帮助