我正在努力完成一些事情,但我不确定它是否可能.
快速的想法是,我正在写一个游戏,并希望有一个不同怪物的单个数组(或矢量).继承主类Monster的每个类都会简单地覆盖它的函数(但不添加任何新函数).
然后,当我浏览怪物列表时,我可以调用所有它们具有的相同功能.
这里有一些代码来展示我想要实现的目标:
class Monster
{
public:
int hp; //hit points
int atp; //attack power
int def; //defense
bool attacking;
bool defending;
virtual void attack();
virtual void defend();
};
void Monster::attack()
{}
void Monster::defend()
{}
class Goblin: public Monster
{
public:
virtual void attack() override;
virtual void defend() override;
};
void Goblin::attack()
{
//Goblin's attacking patterns
}
void Goblin::defend()
{
//Goblin's defending patterns
}
class Orc: public Monster
{
public:
virtual void attack() override;
virtual void defend() override;
}; …Run Code Online (Sandbox Code Playgroud) 我有自己的自定义 UnityEvent,并尝试添加一个侦听器。
我已经使用过AddListener许多其他 UI 对象,例如按钮、下拉菜单、切换开关等,所以我了解这个过程。但是,当我Invoke使用 UnityEvent 时,它根本不触发。我没有收到任何错误消息,经过阅读和研究后,一切看起来都是正确的。所以,不知道下一步该做什么。
这是一个旋转时会发光的物体。
这是我的代码的基础知识:
using UnityEngine.Events;
public class Rotator: MonoBehaviour
{
public UnityEvent OnRotate;
int angle = 0;
int newAngle = 0;
void Start()
{
OnRotate = new UnityEvent();
}
void Update()
{
newAngle = (int)transform.rotation.eulersAngles.z;
if (newAngle != angle)
{
print ("Actual Instance ID: " + GetInstanceID());
print ("Invoking!");
OnRotate.Invoke();
angle = newAngle;
}
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class Owner: MonoBehaviour
{
public Rotator rotator;
void Start()
{
print ("Rotator …Run Code Online (Sandbox Code Playgroud)