Creating draggable barriers

It is time to create our draggable barriers. The player will be able to drag-and-drop the BarrierObject prefab in the Viewport GameObject. This BarrierObject prefab will look as shown in the following screenshot:

Creating draggable barriers

The BarrierObject prefab

First, we need to create our BarrierObject prefab's holder that will contain the draggable object:

  1. Select our UI GameObject.
  2. Create a new child with Alt + Shift + N and rename it as Barrier.
  3. Open the Widget Tool wizard by navigating to NGUI | Open and perform the given steps:
    1. Select Sprite for the Template parameter.
    2. Select Dark sprite for the Sprite field.
    3. With our Barrier GameObject selected, click on the Add To button.
  4. Select our new barrier's Sprite (Dark) GameObject and perform the following steps:
    1. Rename it as Background.
    2. Set its Sprite Type to Sliced.
    3. Set its Color Tint to {0, 250, 250, 170}.
    4. Set its Depth value to 0.
    5. Set its Dimensions to 200 x 200.
  5. Select our Barrier GameObject.
  6. Attach a collider to it by navigating to NGUI | Attach a Collider and perform the following steps:
    1. Set its Center coordinates in Box Collider to {0, 0, 0}.
    2. Set the Size parameter of Box Collider to {200, 200, 1}.
  7. Attach Anchor to it by navigating to NGUI | Attach.
    1. Drag our Viewport GameObject in its Container field.
    2. Set its Side parameter to TopLeft.
    3. Set its Pixel Offset to {100, -100}.

Ok, we have our BarrierObject holder's background at the top left-hand corner as shown in the following screenshot:

The BarrierObject prefab

Let's create the actual BarrierObject prefab, which will be a custom button:

  1. Select our Barrier GameObject.
  2. Open the Widget tool wizard by navigating to NGUI | Create a Widget and perform the following steps:
    1. Drag the SciFi Font – Normal prefab into the Widget Tool wizard's Font field by navigating to Assets | NGUI | Examples | Atlases | SciFi.
    2. Select Button for the Template field.
    3. Select the Highlight sprite for the Background field.
    4. With our Barrier GameObject selected, click on the Add To button.
  3. Select our new Button GameObject from Barrier.
    1. Rename it as BarrierObject.
    2. Set its Center coordinates Box Collider to {0, 0, 0}.
    3. Set Size of Box Collider to {160, 160, 0}.
  4. Drag the Background GameObject from BarrierObject into the Target field in UIButton. Then perform the following steps:
    1. Set its Normal Color to {125, 255, 155, 130}.
    2. Set its Hover Color to {100, 255, 60, 255}.
    3. Set its Pressed Color to {20, 255, 0, 160}.
    4. Set its Disabled Color to {115, 115, 155, 255}.
  5. Select the Background GameObject from BarrierObject and perform the following steps:
    1. Set its Depth value to 1.
    2. Set its Dimensions to 160 x 160.
  6. Select the Label GameObject from BarrierObject and then perform the given steps:
    1. Set its text to [99FF99]Barrier.
    2. Set its Depth to 2.

Ok. We now have our BarrierObject in the Barrier holder. Let's make it draggable by performing the following steps:

  1. Select our BarrierObject GameObject.
  2. Attach a Drag Object component to it by navigating to Component | NGUI | Interaction.
    1. Drag our BarrierObject GameObject in its Target field.
    2. Set its Scale parameter to {1, 1, 0} to avoid Z scrolling.
    3. Set its Drag Effect parameter to None. We want it to be precise.
  3. Create and attach a new BarrierObjectController.cs C# script to it.

Click on the play button. The BarrierObject prefab is now draggable. Now, it is time to handle the drop on the Viewport GameObject.

Before we continue, drag our BarrierObject in a folder of your choice in the Project view to make it a prefab.

Dropping a barrier on Viewport

In order to drop a barrier inside the Viewport GameObject, we need to catch the Viewport GameObject's OnDrop() event and check what was dropped:

  1. Select our Viewport GameObject.
  2. Create and attach a new ViewportHolder.cs C# script to it.
  3. Open this new ViewportHolder.cs script.

In this script, we can add a new OnDrop() method that will be called when an object is dropped on it:

void OnDrop(GameObject droppedObj)
{
  //Get the dropped object's BarrierObjectController
  BarrierObjectController barrierObj = droppedObj.GetComponent<BarrierObjectController>();

  //If it actually has one, destroy the droppedObj
  if(barrierObj != null){
    Destroy(droppedObj);
  }
}

Save the script and click on the play button. Surprisingly, when you drop the BarrierObject on the Viewport GameObject, nothing happens!

That's because, like in Chapter 3, Enhancing your UI, the Collider of BarrierObject is enabled when the OnPress(false) event occurs. This obstructs the collision detection of UICamera.

We just have to disable the collider while dragging, and re-enable it when dropping it. Let's also make it reposition itself if it isn't dropped on the Viewport GameObject. Open our BarrierObjectController.cs script, and add following OnPress() method to do so:

void OnPress(bool pressed)
{
  //Invert the Collider's state
  collider.enabled = !pressed;

  //If it has just been dropped
  if(!pressed)
  {
    //Get the target's collider
    Collider col = UICamera.lastHit.collider;
    //If the target has no collider or is not the viewport
    if(col == null || col.GetComponent<ViewportHolder>() == null)
    //Reset its localPosition to {0,0,0}
    transform.localPosition = Vector3.zero;
  }
}

Save the script and click on the play button. This time, the collider is disabled when the BarrierObject prefab is dropped. So, it is indeed dropped on the collider of Viewport and destroyed instantly.

If it is dropped somewhere else (out of screen or on the barrier's container), it is automatically replaced at the center of the barrier's container. Let's make this BarrierObject a prefab by dragging it in the folder of your choice inside the Project view.

We can now create an ActiveBarrier prefab that will be instantiated on the Viewport GameObject.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.10.45