Unity_版本切换_预编译手段

我们首先用枚举来列举格式

using UnityEngine;
using System.Collections;
public enum CodeType
{
    PC,
    VR
}
public class TestBranch : MonoBehaviour {

    public CodeType codeType;   
    void Update()
    {
        if(codeType ==  CodeType.PC)
        {
            Debug.Log("执行PC的代码");
        }
        else if(codeType == CodeType.VR)
        {
           Debug.Log("执行VR代码");
        }
    }
}

上面这种方法有时会繁琐(如果需要在多个地方进行分支处理就会很繁琐),因此我们引入预编译手段。


#if VR
        Debug.Log("执行VR代码");
#elif PC
        Debug.Log("执行PC代码");
#endif
}

}

我们写VR游戏时可能要写出两种运行方式。
我们用预编译方法来实现:

#if VR
运行方法一
#elif PC
运行方法二
#endif

操作方式:
单击File-Bulid Settings–Player Settings–Other Settings+”PC”或者+”VR”。
Unity_版本切换_预编译手段_第1张图片

Unity_版本切换_预编译手段_第2张图片

Unity_版本切换_预编译手段_第3张图片

你可能感兴趣的:(虚拟现实)