Jelajahi Sumber

Document Godot preprocessor defines

Aaron Franke 5 tahun lalu
induk
melakukan
3c99a213ae
1 mengubah file dengan 67 tambahan dan 0 penghapusan
  1. 67 0
      getting_started/scripting/c_sharp/c_sharp_features.rst

+ 67 - 0
getting_started/scripting/c_sharp/c_sharp_features.rst

@@ -169,3 +169,70 @@ Finally, signals can be created by calling ``AddUserSignal``, but be aware that
         AddUserSignal("MyOtherSignal");
         EmitSignal("MyOtherSignal");
     }
+
+Preprocessor defines
+--------------------
+
+Godot has a set of defines that allow you to change your C# code
+depending on the environment you are compiling to.
+
+.. note:: If you created your project before Godot 3.2, you have to modify
+          or regenerate your `csproj` file to use this feature
+          (compare ``<DefineConstants>`` with a new 3.2+ project).
+
+Examples
+~~~~~~~~
+
+For example, you can change code based on the platform:
+
+.. code-block:: csharp
+
+        public override void _Ready()
+        {
+    #if GODOT_SERVER
+            // Don't try to load meshes or anything, this is a server!
+            LaunchServer();
+    #elif GODOT_32 || GODOT_MOBILE || GODOT_WEB
+            // Use simple objects when running on less powerful systems.
+            SpawnSimpleObjects();
+    #else
+            SpawnComplexObjects();
+    #endif
+        }
+
+Or you can detect which engine your code is in, useful for making cross-engine libraries:
+
+.. code-block:: csharp
+
+        public void MyPlatformPrinter()
+        {
+    #if GODOT
+            GD.Print("This is Godot.");
+    #elif UNITY_5_3_OR_NEWER
+            print("This is Unity.");
+    #else
+            throw new InvalidWorkflowException("Only Godot and Unity are supported.");
+    #endif
+        }
+
+Full list of defines
+~~~~~~~~~~~~~~~~~~~~
+
+* ``GODOT`` is always defined for Godot projects.
+
+* One of ``GODOT_64`` or ``GODOT_32`` is defined depending on if the architecture is 64-bit or 32-bit.
+
+* One of ``GODOT_X11``, ``GODOT_WINDOWS``, ``GODOT_OSX``, ``GODOT_ANDROID``, ``GODOT_HTML5``,
+  or ``GODOT_SERVER`` depending on the OS. These names may change in the future.
+  These are created from the ``get_name()`` method of the :ref:``OS <class_OS>`` singleton,
+  but not every possible OS the method returns is an OS that Godot with Mono runs on.
+
+When **exporting**, the following may also be defined depending on the export features:
+
+* One of ``GODOT_PC``, ``GODOT_MOBILE``, or ``GODOT_WEB`` depending on the platform type.
+
+* One of ``GODOT_ARM64_V8A`` or ``GODOT_ARMEABI_V7A`` on Android only depending on the architecture.
+
+* One of ``GODOT_S3TC``, ``GODOT_ETC``, or ``GODOT_ETC2`` depending on the texture compression type.
+
+* Any custom features added in the export menu will be capitalized and prefixed: ``foo`` -> ``GODOT_FOO``.