基于VR的应用中的十字准线进展圆

时间:2015-12-24 08:18:50

标签: unity3d google-cardboard virtual-reality

我正在尝试在unity3d中制作一个360度图像GoogleCardoard应用程序。

我创建了一个Sphere并制作了5种材质并在材质中应用了360度图像。我正在通过Raycast交换材料及其工作但问题是当十字准线接触箭头时(在箭头和十字准线上应用光线投射以交换材料)材料变化非常快。我应用了一个径向进度条,但它没有工作。我想要的是每次材料更换时十字线加载动画

这是我的c#代码

using UnityEngine;
using System.Collections;

public class next : MonoBehaviour {

    public CardboardHead head;
    public Material[] myMaterials = new Material[5];
    int maxMaterials;
    int arrayPos = 0;
    public GameObject Sphere;
    public int timeToCompleteLoading = 3;

    void Start ()
    {
        head = Camera.main.GetComponent<StereoController> ().Head;
        maxMaterials = myMaterials.Length-1;

    }

    public void ToggleVRMode() 
    {
        Cardboard.SDK.VRModeEnabled = !Cardboard.SDK.VRModeEnabled;
    }

    IEnumerator RadialProgress(float time)
    {
        yield return new WaitForSeconds(time);
        float rate = 1 / time;
        float i = 0;
        while (i < 1)
        {
            i += Time.deltaTime * rate;
            gameObject.renderer.material.SetFloat("_Progress", i);

        }
    }

    void Update ()
    {
                RaycastHit hit;
                Collider collider = GetComponent<Collider> ();

                if (collider) {
                        bool isLookAt = collider.Raycast (head.Gaze, out hit, 6.0f);
                        if (isLookAt == true) {

                            if (collider.gameObject.tag == "next") {
                                StartCoroutine (RadialProgress (timeToCompleteLoading));
                                if (arrayPos == maxMaterials){

                                    arrayPos = 0;
                                }else{

                                    arrayPos++;
                                    Sphere.renderer.material = myMaterials [arrayPos];
                            }
                    }
            }
    }
}
}

请帮助。感谢

1 个答案:

答案 0 :(得分:0)

如果更新中的 StartCoroutine ,则下一帧中的 StartCoroutine 将不会再次停止呼叫。您无法延迟更新功能。但您可以使用布尔标志来运行部分代码:

using UnityEngine;
using System.Collections;

public class next : MonoBehaviour {

    public CardboardHead head;
    public Material[] myMaterials = new Material[5];
    int maxMaterials;
    int arrayPos = 0;
    public GameObject Sphere;
    public int timeToCompleteLoading = 3;
    bool wait = false;

    void Start ()
    {
        head = Camera.main.GetComponent<StereoController> ().Head;
        maxMaterials = myMaterials.Length-1;

    }

    public void ToggleVRMode() 
    {
        Cardboard.SDK.VRModeEnabled = !Cardboard.SDK.VRModeEnabled;
    }

    IEnumerator RadialProgress(float time)
    {
        wait = true;
        yield return new WaitForSeconds(time);

        float rate = 1 / time;
        float i = 0;
        while (i < 1)
        {
            i += Time.deltaTime * rate;
            gameObject.renderer.material.SetFloat("_Progress", i);

        }
        wait = false;
    }

    void Update ()
    {
                RaycastHit hit;
                Collider collider = GetComponent<Collider> ();

                if (collider) {
                        bool isLookAt = collider.Raycast (head.Gaze, out hit, 6.0f);
                        if (isLookAt == true) {
                            if(wait == false){
                                if (collider.gameObject.tag == "next") {
                                    StartCoroutine (RadialProgress (timeToCompleteLoading));
                                    if (arrayPos == maxMaterials){

                                        arrayPos = 0;
                                    }else{

                                        arrayPos++;
                                        Sphere.renderer.material = myMaterials [arrayPos];
                                }
                        }
                    }
            }
    }
}
}