文档

Unity Engine

Unity Engine

测试条件编译

Code example and steps to test your conditionally compiled code.
阅读时间1 分钟最后更新于 1 个月前

以下示例展示了如何测试条件编译代码。它还会根据为目标构建选择的平台打印一条消息。

代码示例

using UnityEngine;using System.Collections;public class PlatformDefines : MonoBehaviour {void Start () { #if UNITY_EDITOR Debug.Log("Unity Editor"); #endif #if UNITY_IOS Debug.Log("Unity iOS"); #endif #if UNITY_STANDALONE_OSX Debug.Log("Standalone OSX"); #endif #if UNITY_STANDALONE_WIN Debug.Log("Standalone Windows"); #endif} }

测试说明

  1. 打开构建配置文件 (Build Profiles) 窗口(菜单:文件 (File) > 构建配置文件 (Build Profiles))。
  2. 检查要用来测试代码的平台是否为当前激活 (Active) 的平台配置文件。如否,请选择首选平台,然后单击切换配置文件 (Switch Profile)。
  3. 创建脚本并复制和粘贴以下示例代码。
  4. 在游戏 (Game) 视图中,单击播放 (Play) 按钮,进入播放 (Play) 模式。检查与 Unity 控制台中所选平台相关的消息,确认代码是否正常工作。例如,如果选择 iOS,则消息
    Unity Editor
    和
    Unity iOS
    将显示在控制台中。

其他资源