![](/img/trans.png)
[英]Unity3d - Vuforia-Unity: How to enable or disable a virtual button?
[英]The cube does not rotate by pressing Vuforia virtual button in Unity3d
我在Vuforia的圖像目標上添加了多維數據集。 我還在圖像目標上添加了虛擬按鈕。 現在,我想通過按虛擬按鈕旋轉立方體。 為此,我實現了以下腳本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
public class rotate : MonoBehaviour, IVirtualButtonEventHandler {
public GameObject vbtn;
public GameObject cube;
public Renderer rend;
// Use this for initialization
void Start () {
vbtn = GameObject.Find ("virtualbtn5");
vbtn.GetComponent<VirtualButtonBehaviour> ().RegisterEventHandler (this);
cube = GameObject.Find ("Cube");
rend = cube.GetComponent<Renderer>();
}
public void OnButtonPressed(VirtualButtonBehaviour vb){
Debug.Log ("Button pressed");
cube.transform.Rotate (new Vector3(0,Time.deltaTime*1000,0));
rend.material.color = Color.blue;
}
public void OnButtonReleased(VirtualButtonBehaviour vb){
Debug.Log ("Button released");
rend.material.color = Color.red;
}
}
該按鈕似乎在工作,因為Debug.Log ("Button pressed");
和rend.material.color = Color.blue;
onButtonPressed
函數中的語句工作正常。 但是cube.transform.Rotate (new Vector3(0,Time.deltaTime*1000,0));
旋轉立方體不起作用。
簡單的是,按鈕可以更改多維數據集的顏色,但不會旋轉多維數據集。
因此,問題是如何通過按下vuforia的虛擬按鈕來旋轉多維數據集。
問題已更新:
我也嘗試了以下代碼,但是按按鈕時立方體仍然不旋轉。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
public class rotate : MonoBehaviour, IVirtualButtonEventHandler {
public GameObject vbtn;
public GameObject cube;
public Renderer rend;
public bool rotateit;
public float speed;
// Use this for initialization
void Start () {
vbtn = GameObject.Find ("virtualbtn5");
vbtn.GetComponent<VirtualButtonBehaviour> ().RegisterEventHandler (this);
cube = GameObject.Find ("Cube");
speed = 100f;
rend = cube.GetComponent<Renderer>();
rotateit = false;
}
void Update(){
if (rotateit) {
cube.transform.Rotate(new Vector3(0, Time.deltaTime * speed, 0));
}
}
public void OnButtonPressed(VirtualButtonBehaviour vb){
//Debug.Log ("Button pressed");
//cube.transform.RotateAround(cube.transform.position, new Vector3(0, 1, 0), 10000f * Time.deltaTime);
//cube.transform.Rotate (new Vector3(0,Time.deltaTime*1000,0));
rend.material.color = Color.blue;
rotateit = true;
Debug.Log ("Button pressed "+rotateit);
}
public void OnButtonReleased(VirtualButtonBehaviour vb){
//Debug.Log ("Button released");
rend.material.color = Color.red;
rotateit = false;
Debug.Log ("Button released "+rotateit);
}
}
也看看控制台窗口
如果要在按住按鈕的同時旋轉每幀,但在釋放按鈕時停止旋轉,請使用布爾變量執行此操作。 將其設置為true
在OnButtonPressed
和false
的OnButtonReleased
。 在Update
功能中檢查此標志是否為true
,然后旋轉多維數據集。
public GameObject vbtn;
public GameObject cube;
public Renderer rend;
bool pressed = false;
public float speed = 100f;
// Use this for initialization
void Start()
{
vbtn = GameObject.Find("virtualbtn5");
vbtn.GetComponent<VirtualButtonBehaviour>().RegisterEventHandler(this);
cube = GameObject.Find("Cube");
rend = cube.GetComponent<Renderer>();
}
public void OnButtonPressed(VirtualButtonBehaviour vb)
{
Debug.Log("Button pressed");
pressed = true;
rend.material.color = Color.blue;
}
public void OnButtonReleased(VirtualButtonBehaviour vb)
{
Debug.Log("Button released");
pressed = false;
rend.material.color = Color.red;
}
void Update()
{
if (pressed)
cube.transform.Rotate(new Vector3(0, Time.deltaTime * speed, 0));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.