gpt4 book ai didi

c# - Unity如何区分Android和iOS

转载 作者:行者123 更新时间:2023-11-30 20:25:41 24 4
gpt4 key购买 nike

假设我们制作了一款基于加速度计的手机游戏。我们正在使用 Input.accelerometer 类。

using UnityEngine;

public class ExampleClass : MonoBehaviour {
public float speed = 10.0F;
void Update() {
Vector3 dir = Vector3.zero;
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
dir.Normalize();

dir *= Time.deltaTime;
transform.Translate(dir * speed);
}
}

此代码适用于 Android 和 iOS。我想知道,Unity 如何知道我们是在 Android 还是 iOS 上运行游戏?我检查了UnityEngine.dll文件,没有发现操作系统之间有任何if-else语句。

最佳答案

Unity 基本上有两个方面,即托管端和 native 端。托管部分只是 C# API, native 端只是用 C++ 编写的代码的 native 端,与 iOS 的 Object-C Swift 或 Android 的 Java 进行通信以进行访问。

托管部分扩展名通常是.dll,Android 的 native 端通常是.so.aar。 jar 或只是一个 .dex

iOS native 端的托管部分扩展通常为 .a.m.mm.c.cpp。对于 iOS,一些管理部分不会被编译,并且仍然以 .cpp 的形式,直到您使用 xCode 构建生成的项目。

This code works on Andoid and on iOS. Im wondering, how does Unity know if we're running the game on Android or iOS?

他们不会在运行时执行此操作。 Unity API 太多了,每次都这样做会显得多余和困惑。

为了简化这一点,它针对不同的平台使用不同的托管文件和 native 文件。这些文件在构建时包含在内,并且一旦您单击构建按钮,就会在编辑器中做出此决定。

首先,转到<UnityInstallationDirecory>\Editor\Data\PlaybackEngines

enter image description here

避免使用一些 preprocessor directives , Application.platform 或者丢失不同平台的代码,这是在构建时在编辑器中完成的。如果您选择 iOS 并构建项目,它将包括 UnityEngine.dll对于 iOS,位于 C:\Program <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\iOSSupport小路。对于 Android 来说,它也会做同样的事情,但在 <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer 中。路径。

每个平台都有不同的UnityEngine.dll。你可以在我上面提到的路径中看到它们。在这个 UnityEngine.dll 和其他托管 dll 中,您将看到对代码 native 端的调用 DllImport或与 AndroidJavaClass 。请注意,UnityEngine.dll 并不是该版本中包含的唯一文件。这些路径中的大多数文件以及我上面提到的扩展名都包含在内。其中包括托管文件和 native 文件。

关于c# - Unity如何区分Android和iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51125021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com