在Unity3D中切换两个游戏对象的活动状态

时间:2016-02-10 17:20:03

标签: unity3d google-cardboard

使用Unity3D我试图创建一个带有alpha纹理作为轮廓的场景,在查找时添加,然后向下看会移除。

目前我在查找时改变了一个equirectangular图像的曝光,但我的剪影对象说我还没有将它分配给一个实例:

enter image description here

enter image description here

正如您从控制台中看到的那样,它最终被识别,但我无法设置活动状态。这是我的代码应用于场景的当前状态:

using UnityEngine;
using System.Collections;

public class switchScript : MonoBehaviour {

    public Cardboard cb;

    public Renderer leftEyeDay;

    public Renderer rightEyeDay;

    private GameObject[] dayObjects;

    public GameObject nightObject;

    void Start () {
        MeshFilter filter = GetComponent(typeof (MeshFilter)) as MeshFilter;

        if (filter != null) {
            Mesh mesh = filter.mesh;

            Vector3[] normals = mesh.normals;
            for (int i=0;i<normals.Length;i++)
                normals[i] = -normals[i];
            mesh.normals = normals;

            for (int m=0;m<mesh.subMeshCount;m++)
            {
                int[] triangles = mesh.GetTriangles(m);
                for (int i=0;i<triangles.Length;i+=3)
                {
                    int temp = triangles[i + 0];
                    triangles[i + 0] = triangles[i + 1];
                    triangles[i + 1] = temp;
                }
                mesh.SetTriangles(triangles, m);
            }
        }
    }

    // Update is called once per frame
    void Update () {
        float xAngle = cb.HeadPose.Orientation.eulerAngles.x;

        if (isLookingUp (xAngle)) {
            var exposureValue = getExposureValue (xAngle);
            leftEyeDay.material.SetFloat ("_Exposure", exposureValue);
            rightEyeDay.material.SetFloat ("_Exposure", exposureValue);
            toggleNight ();
        } else {
            leftEyeDay.material.SetFloat ("_Exposure", 1F);
            rightEyeDay.material.SetFloat ("_Exposure", 1F);
            toggleNight ();
        }
    }

    public bool isLookingUp (float xAngle) {
        return xAngle > 270 && xAngle < 340;
    }

    public float getExposureValue (float xAngle) {
        var _xAngle = Mathf.Clamp (xAngle, 320, 340);
        return ScaleValue (320.0F, 340.0F, 0.3F, 1.0F, _xAngle);
    }

    public float ScaleValue (float from1, float to1, float from2, float to2, float v) {
        return from2 + (v - from1) * (to2 - from2) / (to1 - from1);
    }

    void toggleDay() {

        print (nightObject);

        nightObject = GameObject.FindGameObjectWithTag ("Night");

        nightObject.SetActive (false);

    }

    void toggleNight() {

        print (nightObject);

        nightObject = GameObject.FindGameObjectWithTag ("Night");

        nightObject.SetActive (true);

    }

}

2 个答案:

答案 0 :(得分:0)

当整个对象设置为!active时,GameObject.FindGameObjectWithTag(&#34; Night&#34;)返回null。只需切换该对象上的脚本而不是整个GO。

答案 1 :(得分:0)

好的,如果有人需要这个,这是一种创建场景切换的简单方法:

void Awake() {

        silhouetteObject = GameObject.FindGameObjectWithTag ("Night");

        silhouetteObject.GetComponent<Renderer>().enabled = false;
    }