英文:
Information propagation through child object
问题
I'm fairly new to unity and was wondering: What is the correct method to propagate information through child objects. For example if I'm making an RPG and I want to create a window that will display all the information of a given character with 2 child objects: A skin display of the character, and a stat display of the character. All my objects need a reference to the character object to work. We end up with something like that:
- CharacterWindow
- SkinDisplay
- StatDisplay
How I see things (I can be wrong) is that CharacterWindow
holds the source of truth and should propagate the information through its child when the reference is updated, but what is the good practice to do that ?
PS: I know that I could make for each child a reference to the parent object and fetch the data from it but idk it seems tedious to me to reference my parent in all my children.
Thank you for your time and have a good day!
英文:
I'm fairly new to unity and was wondering: What is the correct method to propagate information through child objects. For example if I'm making a RPG and I want to create a window that will display all the information of a given character with 2 child object: A skin display of the character, and a stat display of the character. All my object need a reference to the character object to work. We end up with something like that:
- CharacterWindow
- SkinDisplay
- StatDisplay
How I see thing (I can be wrong) is that CharacterWindow
holds the source of truth and should propagate the information through its child when the reference is updated, but what is the good practice to do that ?
PS: I know that I could make for each child a reference to the parent object and fetch the data from it but idk it seems tedious to me to reference my parent in all my children
Thank for your time and have a good day !
答案1
得分: 1
在Unity中,你有GameObject(游戏对象)和MonoBehavior(单脚本)。游戏对象是你场景中的物体,在层次视图中可见。你可以给每个游戏对象附加一个或多个MonoBehavior(用C#编写)。现在我假设你有3个游戏对象,并且每个游戏对象都附加了一个相应的MonoBehavior,它在层次视图中看起来像这样:
(父级)游戏对象 - 附加了CharacterWindow MonoBehavior
- (子级)游戏对象 - 附加了SkinDisplay MonoBehavior
- (子级)游戏对象 - 附加了StatDisplay MonoBehavior
现在,你可以随时使用this.gameObject.transform.parent.gameObject
来访问父级的游戏对象,但我认为你实际上想要获取父级游戏对象中的CharacterWindow。你可以通过调用GetComponentInParent
来获取它(这里是文档链接):
public class SkinDisplay : MonoBehaviour
{
// 这将来自父级游戏对象
private CharacterWindow characterWindow;
private void Start()
{
characterWindow = GetComponentInParent<CharacterWindow>();
}
}
请注意,你还可以调用GetComponentInChildren
来获取子级中的MonoBehavior。还有这些方法的复数版本,用于获取父级或子级中所有匹配的MonoBehavior。
英文:
In Unity, you have GameObjects and MonoBehaviors. GameObjects are physical objects in your scene and in the Hierarchy window. You can have one or more MonoBehaviors (written in C#) attached to each GameObject. Now I'll assume you have 3 gameobjects and a corresponding monobehavior attached to each one, which would look like this in the Hierarchy window:
(parent) gameobject - has CharacterWindow monobehavior
- (child) gameobject - has SkinDisplay monobehavior
- (child) gameobject - has StatDisplay monobehavior
Now you can access the parent's gameobject anytime using this.gameObject.transform.parent.gameObject
but I think you'd actually want the CharacterWindow from the parent gameobject instead. You can get it by calling GetComponentInParent
(here's the documentation):
public class SkinDisplay : MonoBehaviour
{
// this will come from the parent gameobject
private CharacterWindow characterWindow;
private void Start()
{
characterWindow = GetComponentInParent<CharacterWindow>();
}
}
Note you can also call GetComponentInChildren
to get monobehaviors from children. And there are plural versions of these methods to get all matching monobehaviors in parents or children.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论