Unity_版本切换_预编译手段

来源:互联网 发布:淘宝广场舞服装春装 编辑:程序博客网 时间:2024/05/01 03:41

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

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”。
这里写图片描述

这里写图片描述

这里写图片描述