Sorry for the delay. I looked into this and there seems to be a bug with that version of the method. You can either use the other method:
public VirtualButtonBehaviour CreateVirtualButton(string vbName,
Vector2 position,
Vector2 size)
Or fix the existing one by changing the signature:
public VirtualButtonBehaviour CreateVirtualButton(string vbName,
Vector3 localScale,
GameObject immediateParent)
Here's the script I used for testing, in case it's helpful:
using UnityEngine;
using System.Collections;
public class VBCreateTest : MonoBehaviour {
private int mButtonCounter = 0;
void Update ()
{
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
Debug.Log("tap!");
// Assumes this script is attached to the ImageTarget, and that there is an event handler also attached
ImageTargetBehaviour imageTarget = GetComponent<ImageTargetBehaviour>();
VirtualButtonEventHandler eventHandler = GetComponent<VirtualButtonEventHandler>();
// Get touch point on target plane
Plane targetPlane = new Plane(imageTarget.transform.up, imageTarget.transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
float dist = 0.0f;
targetPlane.Raycast(ray, out dist);
Vector3 touchPoint = ray.GetPoint(dist);
// Instantiate a prefab (in this case, a cube) and place above the touch point
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.parent = imageTarget.transform;
cube.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
cube.transform.position = touchPoint;
cube.transform.localPosition += new Vector3(0.0f, 0.3f, 0.0f);
// Create a virtual button and register the event handler
string buttonName = "mybutton" + mButtonCounter;
VirtualButtonBehaviour virtualButton =
imageTarget.CreateVirtualButton(buttonName, new Vector3(1.0f, 1.0f, 1.0f), cube);
virtualButton.RegisterEventHandler(eventHandler);
Debug.Log("created button " + buttonName + " with position " + virtualButton.transform.localPosition);
// Add a mesh representing the virtual button, for debugging purposes
CreateVBMesh(virtualButton);
// Increment button counter for unique button names
mButtonCounter++;
}
}
public static void CreateVBMesh(VirtualButtonBehaviour vb)
{
GameObject vbObject = vb.gameObject;
MeshFilter meshFilter = vbObject.GetComponent<MeshFilter>();
if (!meshFilter)
{
meshFilter = vbObject.AddComponent<MeshFilter>();
}
// Setup vertex positions.
Vector3 p0 = new Vector3(-0.5f, 0, -0.5f);
Vector3 p1 = new Vector3(-0.5f, 0, 0.5f);
Vector3 p2 = new Vector3(0.5f, 0, -0.5f);
Vector3 p3 = new Vector3(0.5f, 0, 0.5f);
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[] { p0, p1, p2, p3 };
mesh.triangles = new int[] {
0,1,2,
2,1,3
};
// Add UV coordinates.
mesh.uv = new Vector2[]{
new Vector2(0,0),
new Vector2(1,0),
new Vector2(0,1),
new Vector2(1,1)
};
// Add empty normals array.
mesh.normals = new Vector3[mesh.vertices.Length];
// Automatically calculate normals.
mesh.RecalculateNormals();
mesh.name = "VBPlane";
meshFilter.sharedMesh = mesh;
MeshRenderer meshRenderer = vbObject.GetComponent<MeshRenderer>();
if (!meshRenderer)
{
meshRenderer = vbObject.AddComponent<MeshRenderer>();
}
}
}
- Kim
If I read again aini's post, he was facing this error:
Static member `ImageTargetBehaviour.CreateVirtualButton(string, UnityEngine.Vector2, UnityEngine.GameObject)' cannot be accessed with an instance reference, qualify it with a type name instead
As I said in the other link, this is because the code snippet below was using the wrong API, i.e. the static version of the CreateVirtualButton() method, but trying to call it as a non-static one (i.e. as an instance method, by calling "itb.CreateVirtualButton())" .
The "instance method" version of CreateVirtualButton() takes a Vector2 as third parameter, while the static version of the CreateVirtualButton() takes a GameObject as third parameter.
So, this line of code:
VirtualButtonBehaviour vbb = itb.CreateVirtualButton(vbName[i], mSemut.transform.localPosition, mSemut);
is incorrect (API-wise) and that's what causes the compile error reported by aini.
See also here:
https://developer.vuforia.com/resources/api/unity/class_image_target_abstract_behaviour
I was not suggesting any "workaround", but only saying "use the right API" and make sure your code compiles, first of all.