Author Topic: Resizing a prefab anchored to a 3d world object  (Read 4756 times)

jessebfox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Resizing a prefab anchored to a 3d world object
« on: February 19, 2015, 11:59:39 AM »
There may be a simple solution to this I am unaware of. The situaion:

I have a prefab window that in script is brought in to existence using AddChild, parenting it to a panel for proper display/management. I think anchor it to a 3d object using .SetAnchor

I have tried a couple methods to resize this prefab window, but none seem to work. Could someone give me a suggestion?

Thank you in advance,

Jesse

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Resizing a prefab anchored to a 3d world object
« Reply #1 on: February 19, 2015, 05:17:57 PM »
Did you try SetRect()?

jessebfox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Resizing a prefab anchored to a 3d world object
« Reply #2 on: February 19, 2015, 06:00:02 PM »
Yes I did, but I may be missing something. When I SetRect before SetAnchor, the dimensions aren't changed, and I assume the SetAnchor to worldspace object changes it's dimensions with the anchors. When I SetRect after SetAnchor it moves it, and I assume that is because SetRect is removing it's anchor target. Correct me if I am wrong.

I didn't explore much in SetRect after that as I was unsure of the recommended way of doing this so I tried other things

jessebfox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Resizing a prefab anchored to a 3d world object
« Reply #3 on: February 19, 2015, 06:05:08 PM »
Now that I am pointed at SetRect, I am looking at the declaration and it looks to me at a cursory look that it should not lose the anchor and maybe I did something else wrong. I am going to look in to using this after the setanchor and see if I can get it to appear correctly

jessebfox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Resizing a prefab anchored to a 3d world object
« Reply #4 on: February 19, 2015, 06:50:43 PM »
Hmm, I will post my code here. From looking over the methods, it seems to me I should anchor the object before using SetRect. Only when I do that, it appears that the object disappears entirely. If I eliminate SetRect then the UI I want to spawn appears correctly and behaves perfectly. Just much larger than I desire. Forgive the superfelous code I wanted to include from addChild to Active in case I made some rookie mistake (I am a NGUI rookie after all). Where is my mistake? I have tried several things to troubleshoot such as adjusting the arguments being passed to SetRect so I could try to find where the widget ended up at. I have double and triple checked panels and depths to try to make sure it isn't behind everything.... and as I said it appears sans the SetRect() so something in that I am doing wrong I suspect

  1.                                 var win = NGUITools.AddChild(GameObject.Find("CardPanel"), attackedPanel);
  2.                                 win.GetComponent<UIWidget>().SetAnchor(attacker.gameObject);
  3.                                 win.GetComponent<UIWidget>().SetRect(0, 0, 200, 100);
  4.                                 win.GetComponent<UIDragDropContainer>().reparentTarget= GameObject.Find("UsedCards").transform;//This is a panel with -1 depth for hiding widgets from view
  5.                                 win.GetComponent<DefensePanel>().attack = evnt.attack;
  6.                                 win.GetComponent<DefensePanel>().damage = evnt.damage;
  7.                                 win.GetComponent<DefensePanel>().attacker = attacker;
  8.                                 NGUITools.SetActive(win, true);
  9.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Resizing a prefab anchored to a 3d world object
« Reply #5 on: February 21, 2015, 08:08:08 AM »
Anchoring is not necessary, unless you actually need anchors, in which case you should probably be calling SetAnchor(target, left, bottom, right, top) instead of SetRect.

When the object disappears, where does it go exactly?

Why is your game object disabled from the start? It should be enabled.

On an unrelated note, GameObject.Find(name) is terrible practice, it's very slow, and should never be used in production code.

jessebfox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Resizing a prefab anchored to a 3d world object
« Reply #6 on: February 21, 2015, 11:52:49 AM »
Thank you for your response, I think it helps me out tremendously. I am about to leave on a day trip and I hope to jump in code before I depart to see if it corrects my issues.

When the object disappears, I am not sure currently where it goes. Unfortunately this is part of a bit of code that is utilizing networking and only fires when I test and connect with clients to server, so I have been building a couple scenes and running the .exe on clients and server to test. I can plop this code somewhere else for testing, though. I may not need to if my suspicions are correct.

I had not confirmed my gameobject was disabled at the start, I will eliminate that bit of code to confirm it is enabled. I had that in there because of code I found in the sticky for AddChild (remember, I am very new to NGUI so scouring the forums for answers before I post).

GameObject.Find is very slow, I agree. It really is just placeholder code until I get things working, after that it will be a variable stored.

And in final, this is a window that is popping up over a game object in world space, which is why I was anchoring it. I was ignorant to the fact I could declare the left,bottom, right, top anchors inside of setanchor. I bet my problem will be solved if I simply delete setRect and just call dimensions in the anchor. I will post as soon as I can verify.

jessebfox

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Resizing a prefab anchored to a 3d world object
« Reply #7 on: February 21, 2015, 12:05:04 PM »
Beautiful, works like a charm.

SetAnchor(object, left, bottom, right, top) got it working as I wanted, and indeed the object was enabled to begin with so I axed the enable at the end. Thank you very much for your help.