# UnityService

<Badge tone="type">sealed class</Badge> <Badge>Unity component</Badge> <Badge>singleton</Badge>

```csharp
public sealed class UnityService : Singleton<UnityService>, IUnityService
```

**Namespace:** `AR51.Unity.SDK` · **Implements:** [`Singleton<UnityService>`](/docs/sdk-api/unity-api/classes/servicemanager) · `IUnityService`

A remote-control façade over Unity scene operations — create, find, and transform GameObjects, and set materials — driven over the service layer. It is the **server-side handler for remote scene commands**: a peer issues commands and `UnityService` applies them to the local Unity scene.

:::note[Most app code does not call this directly]
`UnityService` exists to execute scene commands arriving over the service layer. In normal application code you drive the scene with ordinary Unity APIs and use the higher-level services ([`RenderService`](/docs/sdk-api/unity-api/classes/renderservice), [`GameService`](/docs/sdk-api/unity-api/classes/gameservice)). Reach for `UnityService` only when implementing or extending the remote scene-command surface.
:::

## Remote-operation conventions

Because operations are **remote**, the surface is intentionally loosely typed:

- **GameObjects are passed as `object`.** A handle is returned from `NewGameObject` / `CreatePrimitive` / `FindGameObject` / `FindAnchorGameObject` and passed back into the mutators. Do not assume it is a `UnityEngine.GameObject` on the calling side.
- **Transforms are flat `float[]`.** `GetTransform` / `SetTranform` exchange transform data as packed arrays — 16-element matrices, or 3/4-element vectors/quaternions — selected by [`TransformTypes`](/docs/sdk-api/unity-api/skeletons-and-characters).
- **Positions are in meters**; rotations are Unity quaternions.

## Members

<div className="apiPropTable">

| Member | Type | Description |
|---|---|---|
| **Configuration** | | |
| `Prefabs` | `GameObject[]` | prefabs available to instantiate by name |
| `DefaultMaterial` | `Material` | material applied when none is specified |
| `MainCamera` | `object` *(get)* | handle to the scene's main camera |
| **Scene graph** | | |
| `NewGameObject(string name)` | `object` | create an empty GameObject; returns its handle |
| `CreatePrimitive(...)` | `object` | create a primitive (positions in **meters**); see below |
| `Destroy(object gameObject)` | `void` | destroy a GameObject |
| `ClearInstances()` | `void` | destroy all instances created through this service |
| `SetParent(object gameObject, object parent)` | `void` | re-parent a GameObject |
| `FindGameObject(string name)` | `object` | find a GameObject by name |
| `FindAnchorGameObject(string anchorId)` | `object` | find the GameObject for a spatial anchor |
| `GetName` / `SetName` | `string` / `void` | read / set a GameObject's name |
| `IsDestroyed(object obj)` | `bool` | whether the handle's object has been destroyed |
| `GetActive` / `SetActive` | `bool` | read / set active state |
| `SetRendererEnable(object, bool)` | `void` | enable/disable the renderer |
| **Transforms** (flat `float[]`, meters) | | |
| `GetTransform(object, TransformTypes)` | `float[]` | read a transform as a packed array |
| `SetTranform(object, TransformTypes, float[])` | `void` | apply a packed transform *(name spelled `SetTranform`)* |
| `SetCameraProject(float[] projectionMatix)` | `void` | set the camera projection matrix *(16-element)* |
| **Materials** | | |
| `SetMaterialTexture(...)` | `void` | set a texture from JPG bytes |
| `SetMaterialField<T>(object, string, T, bool)` | `void` | set an arbitrary material field |
| `SetMaterialColor` / `GetMaterialColor` | `void` / `AR51.Core.Color` | set / read a material color |

</div>

⚠️ The method name `SetTranform` (missing an `s`) is the real signature in this SDK version — preserved here for source-accuracy. The transport spelling in related types similarly varies (e.g. `Extrensic`).

### CreatePrimitive

<ApiBody kind="method">

```csharp
public object CreatePrimitive(
    int primitiveType, string name,
    AR51.Core.Vector3 position, AR51.Core.Quaternion rotation, AR51.Core.Vector3 scale,
    AR51.Core.Color color, object parent);
```

Create a primitive GameObject in the scene and return its handle. `position` and `scale` are in **meters**; `rotation` is a Unity quaternion; `parent` is a GameObject handle (or null).

<Returns type="object">a handle to the new GameObject</Returns>

</ApiBody>

:::note[Internal — not documented]
The handler methods of `UnityService` (the `object`-typed command surface above) are the server-side implementation of the remote scene-command protocol. The command dispatch, the proto/wire mapping behind the `object` and `float[]` parameters, and the handle bookkeeping are internal plumbing and are not documented as a caller-facing API. Use [`RenderService`](/docs/sdk-api/unity-api/classes/renderservice) and [`GameService`](/docs/sdk-api/unity-api/classes/gameservice) for application-level scene control.
:::

## See also

- [`ServiceManager`](/docs/sdk-api/unity-api/classes/servicemanager) — the singleton host that stands up the service layer this handler runs on
- [`RenderService`](/docs/sdk-api/unity-api/classes/renderservice) — application-level scene rendering control
- [`GameService`](/docs/sdk-api/unity-api/classes/gameservice) — application-level game/scene lifecycle
- [Class index](/docs/sdk-api/unity-api/classes) — all Unity SDK types
