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:
First, we need to create our BarrierObject prefab's holder that will contain the draggable object:
Barrier
.Background
.0
, 250
, 250
, 170
}.0
.200
x 200
.0
, 0
, 0
}.200
, 200
, 1
}.100
, -100
}.Ok, we have our BarrierObject holder's background at the top left-hand corner as shown in the following screenshot:
Let's create the actual BarrierObject prefab, which will be a custom button:
BarrierObject
.0
, 0
, 0
}.160
, 160
, 0
}.125
, 255
, 155
, 130
}.100
, 255
, 60
, 255
}.20
, 255
, 0
, 160
}.115
, 115
, 155
, 255
}.1
.160
x 160
.[99FF99]Barrier
.2
.Ok. We now have our BarrierObject in the Barrier holder. Let's make it draggable by performing the following steps:
1
, 1
, 0
} to avoid Z scrolling.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.
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:
ViewportHolder.cs
C# script to it.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.
18.227.10.45