Author Topic: Prefabs and anchoring  (Read 2915 times)

vallcrist

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 49
    • View Profile
Prefabs and anchoring
« on: January 15, 2014, 10:22:52 PM »
Hello guys. =)

So, I'm trying a new approach in our game UI, where each "feature" in the UI is a separate prefab which gets instantiated at runtime, this way it's easier to edit them individually and enable/disable features in the UI.

However i stumbled into a problem when something needs to be anchored at the screen somehow.

since our prefabs doesn't include the Panel itself for perfomance reasons, it seems like when the prefab gets instantiated it loses all the anchor information.

I've found a way to kind of solve this, by calling on Start the function UIWidget.SetAnchor(transform.parent) and praying that the auto-anchor is good enough, this seems kinda hackish though, any better ideas on how to solve this?

ShinyMark

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Prefabs and anchoring
« Reply #1 on: January 15, 2014, 10:41:00 PM »
We do essentially the same thing in our game when it comes to dynamically spawning UI prefabs. My advice - use more than one panel. In fact, depending on how your UI is setup and how many of your prefabs are visible at one time, consider using a separate panel at the root of EVERY prefab.

Adding a few draw calls is NOT going to kill your game's performance on modern hardware, including mobile. We're not on the iPhone 3G anymore.

vallcrist

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 49
    • View Profile
Re: Prefabs and anchoring
« Reply #2 on: January 15, 2014, 11:04:07 PM »
I don't know.. we're already using dynamic fonts in all text in the game, so if each prefab is a separate panel, each one of those would be 2-4 draw calls depending on the complexity, if we modulate our UI we will probably end with about 12~15 widgets.. kinda scares me. haha

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Prefabs and anchoring
« Reply #3 on: January 17, 2014, 12:28:54 AM »
SetAnchor simply sets the anchor reference to the one of your choice. It will keep all the relative and absolute values -- so yes, it's the right thing to do in your case.

Holy Manfred

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 8
  • Posts: 71
    • View Profile
Re: Prefabs and anchoring
« Reply #4 on: January 20, 2014, 01:18:39 PM »
I'll just post in here, since my question is slightly related:
I can add UI prefabs alright with SetAnchor(transform.parent) to make them fit into whatever they are being added to. This stretches the prefab in all directions to fit.

I would like to know if I can somehow anchor a spawned prefab so that only certain sides are anchored? In my case I would like to add a prefab to a UITable and make it anchor left and right but leave its original height untouched.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Prefabs and anchoring
« Reply #5 on: January 21, 2014, 01:37:35 AM »
You could answer your own question by navigating to UIRect.SetAnchor function. It's a few lines of code.
  1. rect.leftAnchor.target = someLeftTransform;
  2. rect.rightAnchor.target = someRightTransform;
  3. rect.ResetAnchors();
  4. rect.UpdateAnchors();