Adaug proiectul Unity

This commit is contained in:
2026-11-01 23:39:55 +02:00
parent cb14d9d37d
commit 863eac8f2c
23463 changed files with 5412601 additions and 0 deletions
@@ -0,0 +1,79 @@
using Unity.Collections;
using Unity.Jobs;
namespace Doc.CodeSamples.Collections.Tests
{
class AliasingExample
{
public void foo()
{
#region allocation_aliasing
NativeList<int> nums = new NativeList<int>(10, Allocator.TempJob);
nums.Length = 5;
// Create an array of 5 ints that aliases the content of the list.
NativeArray<int> aliasedNums = nums.AsArray();
// Modify the first element of both the array and the list.
aliasedNums[0] = 99;
// Only the original need be disposed.
nums.Dispose();
// Throws an ObjectDisposedException because disposing
// the original deallocates the aliased memory.
aliasedNums[0] = 99;
#endregion
}
public void foo2()
{
#region allocation_reinterpretation
NativeArray<int> ints = new NativeArray<int>(10, Allocator.Temp);
// Length of the reinterpreted array is 20
// (because it has two shorts per one int of the original).
NativeArray<short> shorts = ints.Reinterpret<int, short>();
// Modifies the first 4 bytes of the array.
shorts[0] = 1;
shorts[1] = 1;
int val = ints[0]; // val is 65537 (2^16 + 2^0)
// Like with other aliased collections, only the original
// needs to be disposed.
ints.Dispose();
// Throws an ObjectDisposedException because disposing
// the original deallocates the aliased memory.
shorts[0] = 1;
#endregion
}
public void foo3()
{
#region allocation_dispose_job
NativeArray<int> nums = new NativeArray<int>(10, Allocator.TempJob);
// Create and schedule a job that uses the array.
ExampleJob job = new ExampleJob { Nums = nums };
JobHandle handle = job.Schedule();
// Create and schedule a job that will dispose the array after the ExampleJob has run.
// Returns the handle of the new job.
handle = nums.Dispose(handle);
#endregion
}
}
struct ExampleJob : IJob
{
public NativeArray<int> Nums;
public void Execute()
{
throw new System.NotImplementedException();
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 754bd1bfc77f45aea641971c9495f0f7
timeCreated: 1616748125
@@ -0,0 +1,73 @@
using System.Collections.Generic;
using Unity.Collections;
using Unity.Jobs;
namespace Doc.CodeSamples.Collections.Tests
{
struct ExamplesCollections
{
public void foo()
{
#region parallel_writer
NativeList<int> nums = new NativeList<int>(1000, Allocator.TempJob);
// The parallel writer shares the original list's AtomicSafetyHandle.
var job = new MyParallelJob {NumsWriter = nums.AsParallelWriter()};
#endregion
}
#region parallel_writer_job
public struct MyParallelJob : IJobParallelFor
{
public NativeList<int>.ParallelWriter NumsWriter;
public void Execute(int i)
{
// A NativeList<T>.ParallelWriter can append values
// but not grow the capacity of the list.
NumsWriter.AddNoResize(i);
}
}
#endregion
public void foo2()
{
#region enumerator
NativeList<int> nums = new NativeList<int>(10, Allocator.Temp);
// Calculate the sum of all elements in the list.
int sum = 0;
var enumerator = nums.GetEnumerator();
// The first MoveNext call advances the enumerator to the first element.
// MoveNext returns false when the enumerator has advanced past the last element.
while (enumerator.MoveNext())
{
sum += enumerator.Current;
}
// The enumerator is no longer valid to use after the array is disposed.
nums.Dispose();
#endregion
}
#region read_only
public struct MyJob : IJob
{
// This array can only be read in the job.
[ReadOnly] public NativeArray<int> nums;
public void Execute()
{
// If safety checks are enabled, an exception is thrown here
// because the array is read only.
nums[0] = 100;
}
}
#endregion
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d9848a39deeb4ff9aa9e4cec03215a4b
timeCreated: 1616749165
@@ -0,0 +1,20 @@
{
"name": "Unity.Collections.DocCodeSamples",
"rootNamespace": "",
"references": [
"GUID:e0cd26848372d4e5c891c569017e11f1"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: d1fecb7e41996c7468b18df518b721d9
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: