Author Topic: Drag and Drop Prefabs  (Read 3861 times)

phenotype

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Drag and Drop Prefabs
« on: September 12, 2012, 02:04:06 PM »
I am having a problem dragging instantiated prefabs. I have a standard UIRoot with anchor and panel. I've attached the UIDraggablePanel script to the panel as well as a collider. I have a prefab with a root gameobject with a collider and UIDragPanelContents script on it and a SlicedSprite child object. I instantiate a new instance of the prefab like so; 

  1. GameObject obj = NGUITools.AddChild(this.gameObject, MyPrefab);
  2.  

The problem is; when I try to drag one of the instances they all drag. I am guessing that I have set this up wrong. I tried moving the UIdraggablepanel script to the gameobject of the prefab and adding a UIpanel script to it and then moving the uidragpanelcontents to the slicedsprite but the sprite didn't drag at all.

Any insights?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Drag and Drop Prefabs
« Reply #1 on: September 12, 2012, 02:38:38 PM »
"I've attached the UIDraggablePanel script to the panel as well as a collider."

UIDraggablePanel goes on the panel, so the first step is correct. Collider though? Not sure how that factors in.

Object with a collider should have UIDragPanelContents on it instead.

When you try to drag one object, the entire list will move. That's correct, because you have UIDragPanelContents on it, meaning it drags the panel, which affects everything inside that panel. If you want it to be draggable by itself, use UIDragObject instead.

phenotype

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Drag and Drop Prefabs
« Reply #2 on: September 12, 2012, 10:01:40 PM »
That helped a lot. So the Panel of the UI root has no scripts or colliders now. The Prefab has the UIdragobject script. When I instantiate the prefab it is dragable. Nice. But I am able to drag the dialog off the screen. I tried setting the prefabs target to the panel but the dialog stopped being dragable. So without that I guess it doesn't know its bounds.

I also need to resolve layering. Since I am creating a number of dialogs they tend to overlap each other; ie the header of A layering on top of the background of B while the background of B layers on top of the background of A. I guess when I instantiate them I need to set the depth and then when each is given focus I need to bring it forward.

phenotype

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Drag and Drop Prefabs
« Reply #3 on: September 12, 2012, 11:12:34 PM »
Another step further. I added the UIPanel script to the root game object of my prefab. I also added the UIDraggable Panel script. I then added the UIDragPanelContents to the Background of my dialog. Unfortunately, I can still drag the dialogs off the screen, even after checking, restrictwithinpanel. It does a better job with layering but I am still going to need to create a script to handle focus and bringing focused dialogs to the front.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Drag and Drop Prefabs
« Reply #4 on: September 13, 2012, 03:43:38 PM »
"Restrict within panel" option only works when clipping is enabled.

phenotype

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Drag and Drop Prefabs
« Reply #5 on: September 14, 2012, 12:18:05 AM »
That helped. Should the clipping size automatically change to fill the screen or do I need to script that? The way it is the draggable area is very small. I can set a static size but that doesn't seem to fit with the objective of fitting to many screen resolutions so once again I think I'm missing something.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Drag and Drop Prefabs
« Reply #6 on: September 14, 2012, 03:53:21 AM »
"The way it is the draggable area is very small" <-- what do you mean by that? You can set the clipping size to match the screen's size if you want. It just has to be set to something, not be left at zero.

phenotype

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Drag and Drop Prefabs
« Reply #7 on: September 14, 2012, 09:50:40 AM »
That's what I figured. I was hoping it auto-resized to fill the screen or container. I might trying adding that feature to the UIPanel script.

phenotype

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: Drag and Drop Prefabs
« Reply #8 on: September 14, 2012, 10:59:38 PM »
So this is my solution, it may suck but it works;

First I added another value to the enum Clipping called screen;
Then I added the following logic to UIPanel - clipRange - Get

  1. return this.clipping.Equals(UIDrawCall.Clipping.Screen) ? new Vector4(mClipRange.x, mClipRange.y, Screen.width, Screen.height) : mClipRange;
  2.  

I haven't tried it in a screen that changes size at runtime but it works fine to handle multiple static screen resolutions.

Perhaps someone can recommend a more elegant solution.