简体   繁体   English

父子对象之间的双向通信

[英]Two-way communication between parent and children-objects

I've got a Gameboard containing a lot of dices. 我有一个包含很多骰子的游戏板。

Every can be tilted to either direction (limited by the boundaries of the Gameboard and other dices in their way) by dragging the mouse on top of it in on of the four directions. 通过将鼠标拖到四个方向中的一个方向上,可以将每个方向向任一方向倾斜(受游戏板和其他骰子的边界限制)。 So the dices themself handle the input but before executing a tilt I have to check whether the move is allowed on the gameboard. 因此,骰子自己处理输入,但是在执行倾斜之前,我必须检查游戏板上是否允许移动。

Therefore I'd give every dice a reference to the gameboard but is there a better way to get this two-way behaviour? 因此,我会给每个骰子一个游戏板参考,但是有没有更好的方法来获得这种双向行为?

Instead of handling this logic inside of the dice objects with a reference to the Gameboard, I'd recommend looking at doing the opposite and instead handling this in an an abstracted system (creating a 3rd element that bridges these two components together and handles the required logic), checking whether each move is valid or not. 建议您不要做相反的事情,而是参考抽象的系统(创建第三个元素将这两个组件桥接在一起并处理所需的内容)逻辑),检查每一步是否有效。

To do this you'd need to change the mouse input so that it is also handled in an abstracted system rather than on each Dice, so when you touch a Dice object this system is able to check which object was touched, before then running the code to check if the move is valid on that object. 为此,您需要更改鼠标输入,以便也可以在抽象系统中而不是在每个Dice上处理它,因此,当您触摸Dice对象时,该系统能够检查被触摸的对象,然后再运行代码,以检查移动是否对该对象有效。 This would most likely involve you storing a List/Array/Dictionary of all the Dice objects, so that you're able to check and see if the touched object is contained within and therefore valid. 这很可能涉及您存储所有Dice对象的列表/数组/字典,以便您能够检查并查看触摸的对象是否包含在其中并因此有效。 You can then get any properties you need from the Dice object by using touchedObject.GetComonent<Dice>(); 然后,可以使用touchedObject.GetComonent<Dice>();从Dice对象获取所需的任何属性touchedObject.GetComonent<Dice>(); (or you can store all these properties separately from the actual MonoBehaviour and instead look them up). (或者您可以将所有这些属性与实际的MonoBehaviour分开存储,而是查找它们)。

With this method you can check against the Dice object and the gameboard to see if the next move is valid, without having each object being dependant on the other (which would cause circular dependencies). 使用此方法,您可以检查Dice对象和游戏板上的下一个动作是否有效,而无需使每个对象相互依赖(这会导致循环依赖)。 This also gives the added advantage of giving you more flexibility to change or remove this code going forwards. 这还提供了额外的优势,使您可以更灵活地更改或删除此代码。

This is one of many different approaches you could take, another would be to have your gameboard store a reference of each Dice object and have the Dice object fire an event when interact with (that the gameboard is listening for), so the gameboard code is then able run the check - though from experience I'd go with the former approach and look at decoupling as much as possible. 这是您可以采用的许多不同方法之一,另一种方法是让游戏板存储每个Dice对象的引用,并让Dice对象在与之互动(游戏板正在监听)时触发一个事件,因此游戏板代码为然后可以进行检查-尽管根据经验,我会采用前一种方法,并尽可能地考虑去耦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM