Log in or register to post comments

Using AR Game objects as buttons to launch Java class

September 22, 2011 - 4:55am #1

When the objects are located I am trying to use the game objects in my view as buttons and launch a (Android) java class.

So I added the following code to my java class UnityPlay:
private Intent myIntent;
In my onCreate i added:
myIntent = new Intent(this, myClass.class);
And the method Launch:

	public void Launch(){
		startActivity(myIntent);
	}

I create a script with raycast that I attach to the game object:

using UnityEngine;
using System.Collections;

void Update () {
}

public class TouchDrag : MonoBehaviour
{
    public GameObject obj;
    Transform objPosition = null;

    void Awake()
    {
        Input.multiTouchEnabled = false;

        if (obj)
        {
            objPosition = obj.transform;
        }
        else
        {
            TouchDrag script = GetComponent<TouchDrag>();
            script.enabled = false;
        }
    }

    void FixedUpdate()
    {
        if (obj)
        {
            Camera cam = Camera.main;

            foreach (Touch touch in Input.touches)
            {
                Ray ray = Camera.main.ScreenPointToRay(touch.position);
                RaycastHit hit;

                if (touch.phase == TouchPhase.Began)
                {
                    print("TouchPhase.Began\n");

                    if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                    {
                        if (hit.collider.gameObject == obj)
                        {
                            print("hit.collider.gameObject-> selected!\n");
                        }
                    }
                }
                else if (touch.phase == TouchPhase.Moved)
                {
                    print("TouchPhase.Moved\n");
                }
                else if (touch.phase == TouchPhase.Ended)
                {
                    AndroidJavaClass jc = new AndroidJavaClass("com.company.UnityPlay");
		    		AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
		    		jo.Call("Launch");
                }
            }
        }
    }
}

In Unity I get a lot of error messages that I do not understand (the main one is):
assets/Scripts/button.js(12,17): UCE0001: ';' expected. Insert a semicolon at the end.
12,17 is after the void Awake()

Each of these places have a ; at the end so I do not quite understand what they are relating too...

Re: Using AR Game objects as buttons to launch Java class

September 23, 2011 - 8:21am #4

I think that I'm on the right track now anyway.

I detect the three Game Objects with find(), then get hold of their position and then find the one that is closest to the place where the ray was hit, and send that data to the java application...

Re: Using AR Game objects as buttons to launch Java class

September 23, 2011 - 6:59am #3

Thanks Kim, feelt abit like "Doooh!" when you wrote it...

Anyway, I am trying to figure out what game object that is hit by the ray... In my scene I have one ImageTarget and three Game objects that would act as buttons for different purposes. At the moment every time a game object button is hit the script is activated (even thought the script is not connected to that object, it attached to one of the objects). The best would be if I could identify which game object that is hit and map it to that game objects name (which I would later use in my java code to decide what each button would do).

On the object that had the script attached, in the Inspector, I choose a object and picked ImageTarget from the list. I thought perhaps that I could use the RaycastHit.point to get what I needed (but looks like 3D coordinates) but do not know how to use it. The three Game Objects have been marked with "Is Trigger".

using UnityEngine;
using System.Collections;

public class GameObjectButton : MonoBehaviour {
    public GameObject obj;
    Transform objPosition = null;

    void Awake()
    {
        Input.multiTouchEnabled = false;

        if (obj)
        {
            objPosition = obj.transform;
        }
        else
        {
            GameObjectButton script = GetComponent<GameObjectButton>();
            script.enabled = false;
        }
    }
   

    void FixedUpdate()
    {
        if (obj)
        {
            foreach (Touch touch in Input.touches)
            {
                Ray ray = Camera.main.ScreenPointToRay(touch.position);
                RaycastHit hit;

                if (touch.phase == TouchPhase.Began)
                {
					
                    print("TouchPhase.Began\n"); 
					
					if (Physics.Raycast(ray, out hit))
					{
						print("****************\n" + hit.point + "\n*****************" + "\n" + objPosition);
						var jc = new AndroidJavaClass("com.company.project.UnityPlay");
			    		        var jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
			    		        jo.Call("Launch");
					}
                }
            }
        }
    }
	void Update () {}
}

Re: Using AR Game objects as buttons to launch Java class

September 22, 2011 - 7:45pm #2

It looks like you're trying to put C# code in a javascript file. Our samples use C#. Make this a C# script and get rid of the Update method outside of the class definition. Make sure your filename and class name match.

- Kim

Log in or register to post comments