A game about forced loneliness, made by TACStudios
at master 113 lines 4.5 kB view raw view rendered
1# String support 2 3Burst supports string usage in the following scenarios: 4 5* [`Debug.Log`](https://docs.unity3d.com/ScriptReference/Debug.Log.html) 6* Assigning a string to the [`FixedString`](https://docs.unity3d.com/Packages/com.unity.collections@2.2/api/Unity.Collections.FixedString.html) structs that `Unity.Collections` provides, for example [`FixedString128Bytes`](https://docs.unity3d.com/Packages/com.unity.collections@2.2/api/Unity.Collections.FixedString128Bytes.html). 7* The [`System.Runtime.CompilerServices`](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices?view=net-6.0) attributes `[CallerLineNumber]`, `[CallerMemberName]`, and `[CallerFilePath]` on arguments to Burst functions. However, you can only pass the strings directly to calls to `Debug.Log`. 8 9A string can be either: 10* A string literal. For example: `"This is a string literal"`. 11* An interpolated string using `$"This is an integer {value}` or using `string.Format`, where the string to format is also a string literal. 12 13For example, Burst supports the following constructions: 14 15* Logging with a string literal: 16 17 ```c# 18 Debug.Log("This a string literal"); 19 ``` 20 21* Logging using string interpolation: 22 23 ```c# 24 int value = 256; 25 Debug.Log($"This is an integer value {value}"); 26 ``` 27 28 This is the same as using `string.Format` directly: 29 30 ```c# 31 int value = 256; 32 Debug.Log(string.Format("This is an integer value {0}", value)); 33 ``` 34 35## Supported `Debug.Log` methods 36 37Burst supports the following [`Debug.Log`](https://docs.unity3d.com/ScriptReference/Debug.Log.html) methods: 38 39* `Debug.Log(object)` 40* `Debug.LogWarning(object)` 41* `Debug.LogError(object)` 42 43## String interpolation support 44 45String interpolation has the following restrictions: 46 47* The string must be a string literal 48* Burst supports the following `string.Format` methods: 49 * `string.Format(string, object)` 50 * `string.Format(string, object, object)` 51 * `string.Format(string, object, object, object)` 52 * `string.Format(string, object[])`. Use this for a string interpolation that contains more than three arguments, for example `$"{arg1} {arg2} {arg3} {arg4} {arg5}"`. In this case, the `object[]` array needs to be a constant size and no arguments should involve control flows (for example, `$"This is a {(cond ? arg1 : arg2)}"`). 53* The string must only use value types 54* The string must take only built-in type arguments: 55 * `char` 56 * `boolean` 57 * `byte` / `sbyte` 58 * `double` 59 * `float` 60 * `short` / `ushort` 61 * `int` / `uint` 62 * `long` / `ulong` 63* Burst supports sll vector types (for example `int2`, `float3`), except `half` vector types. For example: 64 65 ```c# 66 var value = new float3(1.0f, 2.0f, 3.0f); 67 // Logs "This value float3(1f, 2f, 3f)" 68 Debug.Log($"This value `{value}`"); 69* Burst doesn't support `ToString()` of structs. It displays the full name of the struct instead. 70 71For more information, see the .NET documentation on [String interpolation](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated) and [Standard numeric format strings](https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings). 72 73## Managed strings 74 75You can pass a managed `string` literal or an interpolated string directly to `Debug.Log`, but you can't pass a string to a user method or to use them as fields in a struct. To pass around or store strings, use one of the [`FixedString`](https://docs.unity3d.com/Packages/com.unity.collections@1.2/api/Unity.Collections.FixedString.html) structs in the `Unity.Collections` package: 76 77```c# 78int value = 256; 79FixedString128 text = $"This is an integer value {value} used with FixedString128"; 80MyCustomLog(text); 81 82// ... 83 84// String can be passed as an argument to a method using a FixedString, 85// but not using directly a managed `string`: 86public static void MyCustomLog(in FixedString128 log) 87{ 88 Debug.Log(text); 89} 90``` 91 92## Arguments and specifiers 93 94Burst has limited support for string format arguments and specifiers: 95 96```c# 97int value = 256; 98 99// Padding left: "This value ` 256` 100Debug.Log($"This value `{value,5}`"); 101 102// Padding right: "This value `256 ` 103Debug.Log($"This value `{value,-5}`"); 104 105// Hexadecimal uppercase: "This value `00FF` 106Debug.Log($"This value `{value:X4}`"); 107 108// Hexadecimal lowercase: "This value `00ff` 109Debug.Log($"This value `{value:x4}`"); 110 111// Decimal with leading-zero: "This value `0256` 112Debug.Log($"This value `{value:D4}`"); 113```