0

I have a question about the way how to show simple 2d image on top of detected marker. I have followed some tutorial to show 3d model and it works fine. there is no problem with 3d. The problem starts when I want to add normal 2d object->sprite . When I add simple sprite I can't add texture and when I insert UI image it's added together with canvas and it is not showed when target is detected. The original image on editor is placed then so far that it's difficult to find it. I would be grateful if somebody can highlight me the right direction.

I need to make this image touch sensitive like a button. Clicking into it must show new scene ( I have it but under GUI.Button). The best way is to replace original marker but I can also make new sprite bigger to hide marker under it.

Jan
  • 29
  • 1
  • 4
  • 9

1 Answers1

5

To help understand the answer, here's a quick rundown on how Vuforia handles marker detection. If you take a look at the DefaultTrackableEventHandler script that's attached to the ImageTarget prefab, you'll see that there are events that fire when the when the tracking system finds or loses an Image.

These are OnTrackingFound (line 67) & OnTrackingLost (line 88) in DefaultTrackableEventHandler.cs

If you want to show a Sprite when tracking, all you need to do is put the Image Target prefab (or any other) and make the Sprite a child of the prefab. The enabling and disabling should happen automatically.

However, in case you want to do something more, here's some edited code.

DefaultTrackableEventHandler.cs

//Assign this in the inspector. This is the GameObject that 
//has a SpriteRenderer and Collider2D component attached to it
public GameObject spriteGameObject ;

Add the below lines to OnTrackingFound

    //Enable both the Sprite Renderer, and the Collider for the sprite
    //upon Tracking Found. Note that you can change the type of 
    //collider to be more specific (such as BoxCollider2D)
    spriteGameObject.GetComponent<SpriteRenderer>().enabled = true;
    spriteGameObject.GetComponent<Collider2D>().enabled = true;

    //EDIT 1
    //Have the sprite inherit the position and rotation of the Image
    spriteGameObject.transform.position = transform.position;
    spriteGameObject.transform.rotation = transform.rotation;

And the below to OnTrackingLost

    //Disable both the Sprite Renderer, and the Collider for the sprite
    //upon Tracking Lost. 
    spriteGameObject.GetComponent<SpriteRenderer>().enabled = false;
    spriteGameObject.GetComponent<Collider2D>().enabled = false;



Next, your question about detecting clicks on this Sprite. Unity's Monobehaviour fires events for a lot of mouse events, such as OnMouseUp, OnMouseDown etc.

Link to Monobehaviour on Unity's API docs
What you will need is an event called OnMouseUpAsButton

Create a new script called HandleClicks.cs and add the below code to it. Attach this script as a component to the spriteGameObject that you assigned for the above.

public class HandleClicks : MonoBehaviour {

    //Event fired when a collider receives a mouse down
    //and mouse up event, like the interaction with a button
    void OnMouseUpAsButton () {
        //Do whatever you want to
        Application.LoadLevel("myOtherLevel");
    }

}
Venkat at Axiom Studios
  • 2,456
  • 1
  • 15
  • 25
  • Wow, thank you for the great answer, I will try it today :). I have one more question if you don't mind. Let's say that I'm showing my image based on your example - do i have to move/rotate it when I'm changing the angle between phone and marker - what I wan't to achieve is to always have my image to be printed under the target and with this same position. – Jan Jan 15 '15 at 08:34
  • I'm not entirely sure if I understood that question. Are you saying that you want the Sprite to look EXACTLY the same regardless of camera angle and Image Target rotation? Or are you saying that the Sprite should have the orientation of the Image Target? – Venkat at Axiom Studios Jan 15 '15 at 08:56
  • The sprite should have position and orientation of the image target. Also, please let me know what you mean by saying "all you need to do is put the Image Target " - do you mean that I should add the sprite in graphic editor under image target in object tree ? – Jan Jan 15 '15 at 09:03
  • Edited the code to do what you asked for. Look for "EDIT 1" in the response above. And yes, the Sprite GameObject should be "under" the image target in the hierarchy. Basically, the Image Target GameObject should have a little triangle to the left, and if you click it, you should be able to see the Sprite. – Venkat at Axiom Studios Jan 15 '15 at 09:12
  • Venkat at Axiom Studios, thank you one more time for your help. I have to say that you pointed me the right direction. What I had to do it was to set the texture type of my .png file to 'Sprite (2d and UI)'. After that I was able to set it as texture of my newly added sprite. When connected it into the Image target it started to work whenever the marker was detected. Second case was to make it touch sensitive - the method you described helped me a lot and the only thing I had to add extra it was Box Collider on the sprite. Without that the sprite wasn't touch responsible. Regards Jan – Jan Jan 15 '15 at 22:01