I’m tinkering about with some AR in unity. I need to call a method at run-time in order to create and register a virtual button and attach it to an instantiated object,
To do this I’ve created a script called “vbSpawn” that I was hoping would call the method and make it create a virtual button. This is what I’ve used:
using UnityEngine; using System.Collections; public class vbSpawn : MonoBehaviour { // Use this for initialization void Start () { ImageTargetBehaviour createScript = GetComponent< ImageTargetBehaviour >(); createScript.CreateVirtualButton(string vbName, Vector2 localScale, GameObject parent); } }
With it I am trying to activate this:
// This methods adds the Virtual Button as a child of "immediateParent". // Returns null if "immediateParent" is not an Image Target or a child of an // Image Target. public VirtualButtonBehaviour CreateVirtualButton(string vbName, Vector2 localScale, GameObject immediateParent) { GameObject virtualButtonObject = new GameObject(vbName); VirtualButtonBehaviour newVBB = virtualButtonObject.AddComponent(); GameObject rootParent = immediateParent.transform.root.gameObject; ImageTargetBehaviour parentImageTarget = rootParent.GetComponentInChildren(); if (parentImageTarget == null) { Debug.LogError("Could not create Virtual Button. " + "immediateParent\"immediateParent\" object is not " + "an Image Target or a child of one."); GameObject.Destroy(virtualButtonObject); return null; } // Add Virtual Button to its parent game object virtualButtonObject.transform.parent = immediateParent.transform; // Set Virtual Button attributes newVBB.InitializeName(vbName); newVBB.transform.localScale = localScale; TrackerBehaviour tracker = (TrackerBehaviour)UnityEngine.Object.FindObjectOfType( typeof(TrackerBehaviour)); if ((tracker == null) || !tracker.RegisterVirtualButton(newVBB, this.TrackableName)) { Debug.LogError("Could not register Virtual Button."); GameObject.Destroy(virtualButtonObject); return null; } else { Debug.Log("Registered Virtual Button successfully."); } // If we manually register the button it should be unregistered if the // Unity object is destroyed. newVBB.UnregisterOnDestroy = true; return newVBB; }
Any ideas as to how I can implement this correctly? Help / a slap around the head would be appreciated =)