Skip to content

Commit b2f5766

Browse files
authored
Merge pull request #15 from ProGM/feature/extending
Extending ReactUnity
2 parents 5c6bdb1 + 2ee78d4 commit b2f5766

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

src/content/learn/howto/extending.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: Extending ReactUnity
3+
---
4+
5+
<Intro>
6+
7+
Extending ReactUnity
8+
9+
</Intro>
10+
11+
## Declaring custom components
12+
13+
You can declare custom components in ReactUnity. This is useful when you want to create a component that needs to interact with the underlying backend (`ugui` or `uitoolkit`).
14+
15+
To declare a custom component, you need to create a new class extending `UGUIComponent` or `UIToolkitComponent` and register it in `UGUIContext.ComponentCreators` or `UIToolkitContext.ComponentCreators` respectively.
16+
17+
### Example
18+
First we create the custom component. For instance, we are declaring a custom button component that has a red background color.
19+
20+
```cs
21+
using ReactUnity.Styling;
22+
using ReactUnity.UGUI;
23+
using UnityEngine;
24+
25+
public class CustomButtonComponent : ButtonComponent
26+
{
27+
public CustomButtonComponent(UGUIContext context, Color backgroundColor) : base(context)
28+
{
29+
Style[StyleProperties.backgroundColor] = backgroundColor;
30+
}
31+
}
32+
```
33+
34+
Then we need to register this component in `UGUIContext.ComponentCreators` dictionary. This can be done in a `MonoBehaviour` that is attached to the same `GameObject` as `ReactRendererUGUI` component.
35+
The declaration must be done before `ReactRendererUGUI` is enabled.
36+
37+
```cs
38+
using ReactUnity.UGUI;
39+
using UnityEngine;
40+
41+
public class CustomComponentInitializer : MonoBehaviour
42+
{
43+
public ReactRendererUGUI reactUnity;
44+
public Color customButtonColor;
45+
46+
void Start()
47+
{
48+
UGUIContext.ComponentCreators["customButton"] = (type, text, context) => new CustomButtonComponent(context, customButtonColor);
49+
50+
reactUnity.enabled = true;
51+
}
52+
}
53+
```
54+
55+
### Typescript declaration
56+
You may also need to declare your custom component in Typescript
57+
58+
```ts
59+
declare global {
60+
interface ReactUnityCustomElements {
61+
mycomponent: { myprop?: number };
62+
}
63+
}
64+
```
65+
66+
#### Example:
67+
```ts
68+
declare global {
69+
interface ReactUnityCustomElements {
70+
button: Button;
71+
}
72+
}
73+
```

src/content/reference/components/image.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@ export default function App() {
2020

2121
### Properties
2222

23-
- **source**: Source of the image. Can be a url, the `Texture2D` object or the `Sprite` object.
23+
- **source**: Source of the image. Can be a url, a resource path (e.g. 'res:/path/to/file'), the `Texture2D` object or the `Sprite` object.
2424
- **fit**: Determines how to position the image inside the element.
25+
26+
### Notes
27+
28+
- The css `color` property can be used to tint the image.

src/content/reference/components/index.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ This page lists the Components supported by ReactUnity in alphabetical order.
1313
- icon
1414
- image
1515
- input
16+
- rawimage
1617
- render
1718
- script
1819
- scroll
1920
- style
2021
- text
2122
- toggle
23+
- video
2224
- view

src/sidebarLearn.json

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
"title": "React - C# communication (Interop)",
2828
"path": "/learn/howto/interop"
2929
},
30+
{
31+
"title": "Extend ReactUnity",
32+
"path": "/learn/howto/extending"
33+
},
3034
{
3135
"title": "Debugging React code",
3236
"path": "/learn/howto/debug"

0 commit comments

Comments
 (0)