- Home /
GameObject Initialize or Replace with prefab
Hi All,
I have a questions about Initializing of GameObject with prefab.
There are two prefabs, almost same but have only a few differences. and load to prefabs and haven't been instantiated. It may have various children.
instantiate a gameobject with A prefab, and run... After a while, I'd like to replace only the data which are different between whole A and B prefab.
The instance may have dynamic values, I want it to remain.
I'm trying to manage UI with prefab. and making two prefabs for vertical and horizontal screen mode. So I need to reset recttransforms of all gameobjects(maybe.. but better to replace all the differences of prefabs, suppose UI is fixed and have one root gameobject) when screen orientation changes.
Is there any nice method to do it?.. PrefabUtility is in UnityEditor so I can not use it while running on mobile..
Answer by PAHeartBeat · Sep 11, 2014 at 10:18 AM
HI JYKeith,
If you are just making two prefeb for two diffrent screeen mode like Verticle / Horizontal. I thought it's will take much time if you need to change lt later.'
I thought you are want to support both orientation in your game like Candy Crush saga. so your game can playable in both orientation, and user can change orientation run time.
I suggest a tweek. why you instantiate? try repostion insted-of instantiate. repostion left and right according your aspect change. As Unity camera, it's never change size of camera in Y "height" (means you have camera orthographic size 10, Unity would make screen size 20, 10 up side from camera cetner and 10 botam side) after that it's would calculate camera width using aspect ratio of screen. so it's follow this equation to crate render viewport for orthographic camera.
OS = Orthographic Size = 10 WPH = View port Height = OS x 2 = 20 WPW = View port Width = WPH * screen aspect
Landsacpe orientation WPW = 20 * 1.33 (screen aspect of iPad or iPad Ratina 4:3 landscape), so Now WPW = 26.6. according it your view port for landscape orientation is 26.6 x 20 unity units.
Protrait orientation WPW = 20 * 0.75 (screen aspect of iPad or iPad Ratina 3:4 Protrait), so Now WPW = 15. according it your view port for landscape orientation is 15 x 20 unity units.
I use this simple math to repostion HUD / UI object as per differant Devices of Apple or Android. and it's work good in our games. may it's usefull to you.
So I suggest don't used diffrent prefebs for different orientation. just repostion your game object based on virtual center of screen.
Hi PAHeartBeat! thank you for your answer.:-) I'm working under unity 4.6 new UI system. So basically the UI elements change their position according to screen resolution. but sometimes I'd like to have different layouts for each screen orientation which seem difficult to make it with something like "LayoutGroup" of new UI system. It's complicated....
Your answer
Follow this Question
Related Questions
Replacing empty gameobjects with prefabs 3 Answers
Change the colour of a prefab with a script. 1 Answer
How can I tell what prefab a GameObject belongs to? 2 Answers
Swap tree prefabs at runtime 1 Answer
Spawn Prefabs 1 Answer