Sunday, April 12, 2015

Handling Drag and Drop with Unity 3D

Drag and Drop is quite useful and we need to implement this quite often in our project. While working on one of my Unity Project, I required to implement the same.

There are some options but I found code described in this post quite simple to use with my project. In this post I will describe how I achieved the same.

So, Let's first create a C# script, I named it Drag.cs and added following code to it. This script will enable touch interaction to added GameObject and will move object along with touch. Also add BoxCollider to GameObject, so we can detect collision.

using UnityEngine;
using System.Collections;
 
public class Drag : MonoBehaviour {
    private Plane plane;
    private Vector3 v3Offset;


    void OnMouseDown() {
 plane.SetNormalAndPosition
             (Camera.main.transform.forward, transform.position);
 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 float dist;
 plane.Raycast (ray, out dist);
 v3Offset = transform.position - ray.GetPoint (dist);         
    }

    void OnMouseUp() {
    }

    void OnMouseDrag() {
 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 float dist;
 plane.Raycast (ray, out dist);
 Vector3 v3Pos = ray.GetPoint (dist);
 transform.position = v3Pos + v3Offset;  
    }
}
Now, suppose we have another GameObject and we want to detect when GameObject we just created with above script, is dropped on it. To detect drop easily let's first add RigidBody to this GameObject and BoxCollider. After adding BoxCollider also check isTrigger is true. After doing this we need to add C# script to it which will handle drop. Let's name it to Drop.cs and add below code to it.

Below script observe the Collision. When collision happens it checks which object is collided with it. If collided object is Drag object then do something.
using UnityEngine;
using System.Collections;

public class Drop : MonoBehaviour {


    void OnCollisionEnter2D (Collision2D other) {
    }
 
    void OnCollisionExit2D(Collision2D other){
    }
 
    void OnTriggerStay2D (Collider2D other) {
        if( other.gameObject.tag == "Drag" ) {
            // Object dropped, handle the drop
        } 
    }
} 
That's pretty much it, hope this helps.

Saturday, April 4, 2015

Sprite with Text in Unity3D

I am working on one game where I wanted to show text over sprite. With new Unity UI System, it's quite easy to create button as sprite and text over it.

But if you want to create a traditional sprite and add text to it, this post describes the same.

Basic idea is to add 3D text component to sprite, but if you have tried it then you might know after adding 3D text component we need to move it to same sorting order as sprite. Also we need to layout text properly to set alignment.

Before we begin, I assume you know how to create Sprite, how to create 3D text component and make it a Prefab object. If not you can refer this article to know how to make Prefab object.

Finally, following is my script which I attached to Sprite. Once added to Sprite, It will create 3D text component from provided Prefab object and add the same to Sprite.
using UnityEngine;
using System.Collections;
 
public class TextScript : MonoBehaviour {
 
 public string text;
 public GameObject textPrefab;
    
 void Start() {
  GameObject text = Instantiate(textPrefab) as GameObject;
  text.transform.parent = gameObject.transform;
  text.transform.rotation = gameObject.transform.rotation;

  TextMesh textInstance = text.GetComponent();
  textInstance.text = text;

  Renderer textRenderer = text.GetComponent ();
  Renderer thisRenderer = GetComponent ();

  textRenderer.sortingLayerID = thisRenderer.sortingLayerID;
  textRenderer.sortingOrder = thisRenderer.sortingOrder+1;
 
  Bounds textBounds = textRenderer.bounds;
  Bounds parentBounds = thisRenderer.bounds;
  
  float x = gameObject.transform.position.x 
                     - (textBounds.size.x/2); 
  float y = gameObject.transform.position.y 
                     + (textBounds.size.y/2);
  textInstance.transform.position = 
                     new Vector3(x, y, parentBounds.center.z + 1);
 }
}
That's it, you can add this script to any component and add text over it.