0
我正在创建一个将成为策略游戏的3D游戏。我想通过代码(C#)创建一个面板(一组文本和按钮的UI组),然后将它们移动到Canvas布局中的GameObject顶部。所以我这样做:实例化后移动UI组件
if (ThisBuilding == null)
ThisBuilding = this.gameObject;
Panel = Canvas.Instantiate(Panel);
Panel.transform.SetParent(canvas.transform, false); //false to prevent stupid scaling issues created by the parent change
Vector3 centerPosition = ThisBuilding.GetComponentInChildren<MeshRenderer>().bounds.center; //the building GameObject contains a MeshRenderer component in its only children.
panelRect.anchoredPosition = Vector3.Lerp(panelRect.anchoredPosition, centerPosition, 1.0f);
的事情是,这个脚本的运行(在开始()),每建筑一次,希望该小组将被实例化了3次,每个小组对应于一个建筑,但对于一些奇怪的原因,这是行不通的。
预期结果:每个面板被实例化时,该位置是相同实例化它建筑物(建筑物游戏物体保持脚本和开启/关闭面板)
实绩:即使面板确实实例化了3次,因为它们都处于相同位置,即使Update()函数改变其位置也不会改变。我究竟做错了什么?
人!这工作完美!非常非常感谢你!它的工作完美无瑕! –
不客气,我很高兴我能帮到你! –