简体   繁体   English

Unity 2D - 在NGUI的“UI Root”(作为Parent)中动态实例化新预制件

[英]Unity 2D - Dynamically instantiate new prefab inside NGUI's “UI Root”(as Parent)

Im new to unity. 我是新的团结。 When I instantiate a new prefab GameObject from inside script as follows: 当我从内部脚本中实例化一个新的预制GameObject时,如下所示:

GameObject newArrow = (GameObject)Instantiate(arrowPrefab);  
newArrow.transform.position = arrowSpawnTransform.position;

But this is creating the object in the root hierarchy(and not inside "UI Root" of NGUI). 但这是在根层次结构中创建对象(而不是在NGUI的“UI根”内)。 When I add any object outside of the UI-Root (of NGUI) it adds it to some location far away from the camera, also with a huge dimension. 当我在UI-Root(NGUI)之外添加任何对象时,它会将其添加到远离相机的某个位置,同时具有巨大的尺寸。 Can someone help me with how to add the newly created prefab under "UI Root" ? 有人可以帮我解决如何在“UI Root”下添加新创建的预制件吗?

It would be great of someone also lets me know about the positioning and scaling associated with native unity and NGUI. 如果有人也让我了解与原生团结和NGUI相关的定位和缩放,那将是很棒的。 I try hard but am not understanding where to keep what object and in what size so that it comes out as expected. 我努力但不知道在哪里保留什么对象和大小,以便它按预期出现。 I'll appreciate any help that can be provided. 我将感谢任何可以提供的帮助。 Thanks ! 谢谢 !

EDIT: I have found a way to place the new prefab inside "UI Root" thru: newArrow.transform.parent = gameObject.transform.parent; 编辑:我找到了一种方法将新预制件置于“UI Root”中:newArrow.transform.parent = gameObject.transform.parent; after instantiating. 实例化后。

But still the scaling is huge. 但仍然是规模巨大。 It's like multiple times bigger than the screen size. 这比屏幕尺寸大几倍。 Please help me with this. 请帮我解决一下这个。 What should I be doing ? 我该怎么办?

When working with UI elements in NGUI, don't use Instantiate. 在NGUI中使用UI元素时,请不要使用Instantiate。 Use NGUITools.AddChild(parent, prefab). 使用NGUITools.AddChild(parent,prefab)。

NGUI's scale with respect to the rest of the objects in the scene is rather microscopic. NGUI相对于场景中其他对象的比例相当微观。 If you look at the UIRoot object, you will notice that its scale is measured in thousandths of a meter (default object resolution of 1 typically represents a meter). 如果您查看UIRoot对象,您会注意到它的刻度是以千分之一米为单位测量的(默认对象分辨率为1通常表示一米)。 That is why when you instantiate a prefab and attach it to any object under the UIRoot it appears gigantic relative to the scale of the UIPanel. 这就是为什么当您实例化预制件并将其附加到UIRoot下的任何对象时,它相对于UIPanel的比例显得巨大。 You could manually scale down the object to the appropriate size, or let NGUI do it for you (as indicated by Dover8) by utilizing NGUITools.AddChild(parent, prefab). 你可以手动将对象缩小到适当的大小,或者让NGUI通过使用NGUITools.AddChild(parent,prefab)为你做(如Dover8所示)。 In fact, this is the proper way to do so. 事实上,这是正确的方法。 Don't try to do it manually. 不要尝试手动操作。 The results will be unpredictable and can lead to inappropriate behavior of components. 结果将无法预测,并可能导致组件的不当行为。 Here's a link to Tasharen's forum on this topic: http://www.tasharen.com/forum/index.php?topic=779.0 以下是Tasharen关于此主题的论坛的链接: http ://www.tasharen.com/forum/index.php?topic = 779.0

Positioning is a bit more complicated. 定位有点复杂。 Everything is relative to anchors. 一切都与锚相关。 Anchor positions are a relationship between a parent object (usually a panel) and a target (usually some form of widget such as a sprite or label). 锚点位置是父对象(通常是面板)和目标(通常是某种形式的小部件,例如精灵或标签)之间的关系。 They are controlled by four values relative to the edges of the panel (or parent object), right, left, bottom, and top with respect to the edges of the target (varies by position). 它们由相对于面板边缘(或父对象)的四个值控制,相对于目标边缘的右,左,底和顶部(根据位置而变化)。 Each of these are modified by an integer value (in pixels) that modify the dimensions of the target relative to the parent. 这些中的每一个都由整数值(以像素为单位)修改,该整数值修改目标相对于父级的维度。 There are many examples included with NGUI. NGUI中包含许多示例。 I suggest that you look over them. 我建议你看看他们。 In particular, pay attention to Example 1 - Anchors. 特别要注意例1 - 锚点。 I learned a lot from studying these examples, but they don't really cover the full power of NGUI, but they are an excellent starting point. 我从研究这些例子中学到了很多东西,但它们并没有真正涵盖NGUI的全部功能,但它们是一个很好的起点。

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

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