游戏是游侠破解版Null Refest paul de vencee Exception:Object reference notset to an inste省略

unity3d - Unity 4.6: Button instances added at runtime are not scaled by Reference Resolution? - Stack Overflow
to customize your list.
Announcing Stack Overflow Documentation
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
(Using Unity 4.6.0b20)
I've hit a problem where prefab button size works correctly when added in editor but seems to ignore Reference Resolution when added by script.
This uses a Canvas with Reference Resolution
and MatchWidthOrHeight. Canvas has a Panel Vertical Layout Group for the buttons. The Button has Preferred Width/Height, and it is saved as a Prefab so new instances can be created from assets at runtime.
In the editor I can drag the prefab to scene to add instances to the panel which also has width 102 and they stack and scale nicely:
But when instead I add new instances of the prefab via script to the Panel they appear with the wrong size. Looking at the dimensions my guess is the 102 pixel size is not being scaled by the Reference Resolution:
The script code creates the instance via GameObject.Instantiate() and adds to the Panel by setting transform.parent:
GameObject uiInstance = (GameObject)GameObject.Instantiate(Resources.Load&GameObject&(assetPath));
uiInstance.transform.parent = unitButtonsPanel.
I assume either there is more that needs to be done when adding the Button to the Panel than just setting Parent, or it's a bug with the beta...
Suggestions?
Use the method transform.SetParent passing false in the second parameter:
transform.SetParent(transform, false);
This way you can prevent world positioning and scaling.
You can find more information in the unity manual, searching for "UICreateFromScripting".
I found the cause -- when the prefab is added by script the Scale values in this test Scale was being set to 1.186284.
But if the script sets scale to 1.0 immediately after adding the button:
GameObject uiInstance = (GameObject)GameObject.Instantiate(Resources.Load&GameObject&(assetPath));
uiInstance.transform.parent = unitButtonsPanel.
// HACK force scale to 1.0
uiInstance.transform.localScale = Vector3.
the new button instances are sized correctly.
imho this is a Unity bug because adding the prefab instance in the editor sets scale 1.0, the prefab itself has scale 1.0, so I can't see a reason why adding it from script should act differently. But I'm kind of guessing as there's not much documentation for the new UI yet :)
This is not a bug, its an order of execution issue. When using Unity's new layout components the values are driven by the layout components, which get calculated once at the end of the frame that a layout rebuild call is made. If you're basing anything in a script off of the new rectTransform.rect calculations which are driven by the layout elements, you'll need to wait until this calculation has been performed or call it yourself by using
LayoutRebuilder.MarkLayoutForRebuild (transform as RectTransform);
You'll still have to wait until the end of the frame for those calculations to take place before using anything from the rectTransform like scale, pos, etc. This had me stumped for a while when I first start dynamically creating UI elements and the scale was zero depending upon when I instantiated them.
You can read about that rebuild call and the order of execution of the layout calc stuff here:
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
The week's top questions and answers
Important community announcements
Questions that need answers
By subscribing, you agree to the
Stack Overflow works best with JavaScript enabled游戏七日杀14.6无法启动!_百度知道
游戏七日杀14.6无法启动!
hiphotos.baidu.jpg" target="_blank" title="点击查看大图" class="ikqb_img_alink"><img class="ikqb_img" src="http://h://h.com/zhidao/pic/item/d50735fae6cd7bd8330ef7.jpg" esrc="/zhidao/wh%3D600%2C800/sign=edf096ce02d162d985bb6a1a21ef85d4/d50735fae6cd7bd8330ef7.hiphotos.baidu.hiphotos<a href="/zhidao/wh%3D450%2C600/sign=b8d4fce5a1ec8ac233bdb9/d50735fae6cd7bd8330ef7://h.baidu
游戏是游侠破解版!红字写的是Null Refevence Exception:Object reference notset to an insteancc of an object
而拥有相应的特质,3D高清画面配合宏大的音效,怪物和猎人的对战也变的更加有趣MHOL很不错,每一个怪物会根据栖息地的特质,还可以使用手柄进行游戏,MHOL没有一刀致死的怪物,让游戏的代入感更强
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 relavence 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信