Skip to content

Commit

Permalink
Merge pull request #16 from ProGM/prefab-docs
Browse files Browse the repository at this point in the history
Documenting the <prefab> component
  • Loading branch information
KurtGokhan authored Apr 10, 2024
2 parents ff2c79e + 2d28130 commit c9a17a6
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/content/reference/components/prefab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: <prefab>
layout: API
---

`<prefab>` allows to instantiate a custom prefab as a React Component.

<Sandpack>

```js
export default function App() {
// Load the prefab using globals
const { prefab } = useGlobals();
// or alternatively you can load the prefab from resources
// const prefab = useMemo(() => Interop.UnityEngine.Resources.Load('MyPrefab') as UnityEngine.GameObject, []);

return <prefab target={prefab} />;
};
```

</Sandpack>

### Lifecycle events and properties

`<prefab>` component instantiates the prefab, then searches for a component implementing the `IPrefabTarget` interface and calls the `Mount`, `UnMount` lifecycle hooks with the prefab instance.

You can use them to handle custom logic, events, and properties passed to the prefab.

#### Example of a custom prefab target component:
```csharp
public class MyComponentPrefabTarget : MonoBehaviour, IPrefabTarget
{
PrefabComponent Component { get; set; }

public Action AddEventListener(string eventName, Callback callback)
{
// Here you can handle custom events passed to the prefab.
return null;
}

public void Mount(PrefabComponent cmp)
{
Debug.Log("Mounting MyComponentPrefabTarget");
// Saving the reference of cmp to use it later
// For instance you can use it to emit custom events to React `Component.FireEvent("onSomething", value)`
Component = cmp;
}

public bool SetProperty(string propertyName, object value)
{
// Here you can handle custom properties passed to the prefab
switch (propertyName)
{
case "myprop":
Debug.Log("Doing something with myprop");
return true;
}
return false;
}

public void Unmount(PrefabComponent cmp)
{
Debug.Log("Unmounting MyComponentPrefabTarget");
Component = null;
}
}
```

##### Example of a prefab with a custom target component:
```js

export default function App() {
const prefab = useMemo(() => Interop.UnityEngine.Resources.Load('MyPrefab') as UnityEngine.GameObject, []);

return (
<prefab
target={prefab}
custom={{
// This will be passed to the prefab target component
myprop: 'something'
}}
// This will be invoked when `FireEvent` is called from the prefab target component
onSomething={val => console.log('Something happened', val)}
/>
);
};
```
4 changes: 4 additions & 0 deletions src/sidebarReference.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
"title": "style",
"path": "/reference/components/style"
},
{
"title": "prefab",
"path": "/reference/components/prefab"
},
{
"title": "svg",
"path": "/reference/components/svg"
Expand Down

0 comments on commit c9a17a6

Please sign in to comment.