Skip to content
This repository was archived by the owner on Feb 12, 2023. It is now read-only.

Commit 73b0d87

Browse files
authored
Version 2 Rewrite (#1)
1 parent a825520 commit 73b0d87

File tree

272 files changed

+35015
-5501
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

272 files changed

+35015
-5501
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ Add a dependency into your `manifest.json`.
1111
```json
1212
{
1313
"dependencies": {
14-
"com.mkmarek.uielements.react": "https://github.com/mkmarek/unity-react-uielements.git#releases/v0.0.1",
14+
"com.mkmarek.uielements.react": "https://github.com/mkmarek/unity-react-uielements.git#releases/vX.X.X",
1515
}
1616
}
1717
```
1818

19+
For released version numbers check the releases tab.
20+
1921
## How to start
2022

2123
1. In your unity `Assets` folder create a `Resources` folder.
@@ -31,23 +33,22 @@ const style = {
3133
height: "80%",
3234
backgroundColor: "#ffffff",
3335
fontSize: 21,
34-
alignSelf: 'center',
35-
alignItems: 'center',
36-
justifyContent: 'center'
36+
alignSelf: 'Center',
37+
alignItems: 'Center',
38+
justifyContent: 'Center'
3739
}
3840

3941
function App() {
40-
return <element style={style}>
42+
return <visualElement style={style}>
4143
Hello Unity!
42-
</element>
44+
</visualElement>
4345
}
4446

4547
render(<App />)
4648
```
4749

48-
4. In your Unity scene create a new empty object and add `ReactRenderer` and `EventSystem` monobehaviours to it with the following settings.
49-
50-
![](./media/settings.jpg)
50+
4. In your Unity scene create a new empty object and add `PanelRenderer` and `EventSystem` monobehaviours to it as you would normaly do for runtime UIElements.
51+
5. Attach `ReactRuntime` monobehaviour to the object and drag your `index.jsx` into the `Root` field.
5152

5253
Set the root property to your `index.jsx`. Just drag it from your `Resources` folder into the field.
5354

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,59 @@
11
import React, { useState } from 'react';
2+
import { useQuery } from 'unity-renderer';
23
import Button from './button';
3-
import { useQuery, createEntity, removeEntity } from 'unity-renderer';
44

55
function TodoItem({ value, onRemove }) {
66

77
return (
8-
<Button innerStyle={{ flexDirection: 'row', justifyContent: 'space-between' }} style={{ height: 70 }}>
9-
<element style={{ marginLeft: 10 }}>{value}</element>
10-
<Button onClick={onRemove} style={{ width: 70, height: 50, marginRight: 5 }}>Delete</Button>
8+
<Button innerStyle={{ flexDirection: 'Row', justifyContent: 'SpaceBetween' }} style={{ height: 70 }}>
9+
<visualElement style={{ marginLeft: 10 }}>{value}</visualElement>
10+
<Button onMouseUpEvent={onRemove} style={{ width: 70, height: 50, marginRight: 5 }}>Delete</Button>
1111
</Button>
1212
)
1313
}
1414

15+
const todoItemComponentName = 'TodoItemComponent';
16+
const TodoItemComponent = getFactory(todoItemComponentName);
17+
1518
export default function Todo() {
16-
const [isInitializing, todoItems] = useQuery('TodoItems');
1719
const [value, setValue] = useState('');
20+
const query = useQuery([todoItemComponentName]);
21+
22+
const items = [];
23+
for (let i = 0; i < query.getSize(); i++) {
24+
items.push({
25+
entity: query.getElementAt('Entity', i),
26+
value: query.getElementAt(todoItemComponentName, i).Content
27+
});
28+
}
1829

1930
const addItem = () => {
20-
const utf8 = unescape(encodeURIComponent(value));
31+
const newValue = new TodoItemComponent();
32+
newValue.Content = value;
2133

22-
const arr = [];
23-
for (let i = 0; i < utf8.length; i++) {
24-
arr.push(utf8.charCodeAt(i));
25-
}
26-
27-
createEntity([['TodoItemComponent', {
28-
data: arr
29-
}]])
34+
executeBuffer((buffer) => {
35+
const entity = buffer.createEntity();
36+
buffer.setComponent(todoItemComponentName, entity, newValue);
37+
})
3038

3139
setValue('');
3240
}
3341

34-
const mapArrayToString = (arr) => {
35-
if (!arr) return '';
36-
37-
const utf8 = String.fromCharCode(...arr);
38-
return decodeURIComponent(escape(utf8));
42+
const removeItem = (entity) => {
43+
executeBuffer((buffer) => {
44+
buffer.destroyEntity(entity);
45+
});
3946
}
4047

41-
const removeItem = (index) => {
42-
removeEntity(index, [ 'TodoItemComponent' ]);
43-
}
44-
45-
const items = isInitializing
46-
? []
47-
: todoItems.map(e => mapArrayToString(e.data));
48-
4948
return (
50-
<element style={{ height: '100%' }}>
51-
<scrollview style={{ height: '100%' }}>
52-
{items.map((e, i) => <TodoItem key={i} value={e} onRemove={() => removeItem(i)} />)}
53-
</scrollview>
54-
<textfield text={value} onChange={e => setValue(e.value)} />
55-
<Button style={{ height: 70, minHeight: 70, alignSelf: 'flex-end' }} onClick={addItem}>
49+
<visualElement style={{ height: '100%' }}>
50+
<scrollView style={{ height: '100%' }}>
51+
{items.map((e, i) => <TodoItem key={i} value={e.value} onRemove={() => removeItem(e.entity)} />)}
52+
</scrollView>
53+
<textField value={value} onChange={newValue => setValue(newValue)} />
54+
<Button style={{ height: 70, minHeight: 70, alignSelf: 'FlexEnd' }} onMouseUpEvent={addItem}>
5655
Add TODO item
5756
</Button>
58-
</element>
57+
</visualElement>
5958
);
6059
}

examples/Assets/Resources/components/button.jsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const borderColor = (color) => ({
1414
borderRightColor: color
1515
});
1616

17-
export default function Button({ style, innerStyle={}, onClick, children }) {
17+
export default function Button({ style, innerStyle={}, onMouseUpEvent, children }) {
1818
const outerStyle = Object.assign(borderColor('#0b2431'), borderWidth('1'),{
1919
width: '100%',
2020
height: '100%',
@@ -24,18 +24,18 @@ export default function Button({ style, innerStyle={}, onClick, children }) {
2424
width: '100%',
2525
height: '100%',
2626
backgroundColor: '#173e54',
27-
alignItems: "center",
28-
alignContent: "center",
29-
justifyContent: "center",
30-
flexDirection: "column",
31-
unityTextAlign: 'middle-center',
27+
alignItems: "Center",
28+
alignContent: "Center",
29+
justifyContent: "Center",
30+
flexDirection: "Column",
31+
unityTextAlign: 'MiddleCenter',
3232
}, innerStyle);
3333

3434
return (
35-
<element style={outerStyle}>
36-
<element onClick={onClick} style={innerBorderStyle}>
35+
<visualElement style={outerStyle}>
36+
<visualElement onMouseUpEvent={onMouseUpEvent} style={innerBorderStyle}>
3737
{children}
38-
</element>
39-
</element>
38+
</visualElement>
39+
</visualElement>
4040
);
4141
}

examples/Assets/Resources/components/component-preview.jsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const ComponentWithHeader = ({ header, component }) => <>
77

88
export default function ComponentPreview() {
99
return (
10-
<scrollview style={{ backgroundColor: '#173e54' }}>
10+
<scrollView style={{ backgroundColor: '#173e54' }}>
1111
<ComponentWithHeader header="Box" component={
1212
<box style={{ color: '#000000' }}>
1313
Some content in the box
@@ -27,16 +27,16 @@ export default function ComponentPreview() {
2727
<label />
2828

2929
ListView:
30-
<listview />
30+
<listView />
3131

3232
MinMaxSlider:
33-
<minmaxslider />
33+
<minMaxSlider />
3434

3535
PopupWindow:
36-
<popupwindow />
36+
<popupWindow />
3737

3838
Repeat button:
39-
<repeatbutton />
39+
<repeatButton />
4040

4141
Scroller:
4242
<scroller />
@@ -45,16 +45,16 @@ export default function ComponentPreview() {
4545
<slider />
4646

4747
SliderInt:
48-
<sliderint />
48+
<sliderInt />
4949

5050
TemplateContainer:
51-
<templatecontainer />
51+
<templateContainer />
5252

5353
Textfield:
54-
<textfield />
54+
<textField />
5555

5656
Toggle:
5757
<toggle />
58-
</scrollview>
58+
</scrollView>
5959
)
6060
}

examples/Assets/Resources/components/counter.jsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { useState } from 'react';
2-
import Button from './button';
1+
import React from 'react';
32
import { useQuery } from 'unity-renderer';
3+
import Button from './button';
44

55
const margin = (margin) => ({
66
marginTop: margin,
@@ -26,24 +26,42 @@ const countStyle = Object.assign(margin('5px'), {
2626
height: '40%'
2727
});
2828

29+
const counterComponentName = 'CounterComponent';
30+
const CounterComponent = getFactory(counterComponentName);
31+
2932
export default function Counter() {
30-
const [initializing, counterComponents, setCounterComponent] = useQuery('Counter');
33+
const query = useQuery([counterComponentName]);
34+
35+
if (query.getSize() == 0) return <></>;
36+
37+
const { Count } = query.getElementAt(counterComponentName, 0);
38+
39+
const setCount = (value) => {
40+
const counterComponentEntity = query.getElementAt('Entity', 0);
3141

32-
if (initializing) return <element />;
42+
const newValue = new CounterComponent();
43+
newValue.Count = value;
3344

34-
const count = counterComponents[0].count;
45+
executeBuffer((buffer) => {
46+
buffer.setComponent(counterComponentName, counterComponentEntity, newValue);
47+
})
48+
}
3549

3650
return (
37-
<element style={Object.assign(margin('auto'), { width: "100%", height: "100%", alignItems: "center" })}>
38-
<element style={countStyle}>{count}</element>
39-
<element style={{ flexDirection: 'row' }}>
51+
<visualElement style={{
52+
width: "100%",
53+
height: "100%",
54+
alignItems: 'Center',
55+
justifyContent: 'Center'}}>
56+
<visualElement style={countStyle}>{Count}</visualElement>
57+
<visualElement style={{ flexDirection: 'Row' }}>
4058
<Button
4159
style={Object.assign(itemStyle, { fontSize: '18'})}
42-
onClick={() => setCounterComponent(0, { count: count + 1 })}>Increment</Button>
60+
onMouseUpEvent={() => setCount(Count + 1)}>Increment</Button>
4361
<Button
4462
style={Object.assign(itemStyle, { fontSize: '18'})}
45-
onClick={() => setCounterComponent(0, {count: count - 1 })}>Decrement</Button>
46-
</element>
47-
</element>
63+
onMouseUpEvent={() => setCount(Count - 1)}>Decrement</Button>
64+
</visualElement>
65+
</visualElement>
4866
)
4967
}

examples/Assets/Resources/components/panel.jsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ export default function Panel({ innerStyle, outerStyle, children }) {
3333
}, innerStyle);
3434

3535
return (
36-
<element style={outerStyle}>
37-
<element style={midBorderStyle}>
38-
<element style={innerBorderStyle}>
36+
<visualElement style={outerStyle}>
37+
<visualElement style={midBorderStyle}>
38+
<visualElement style={innerBorderStyle}>
3939
{children}
40-
</element>
41-
</element>
42-
</element>
40+
</visualElement>
41+
</visualElement>
42+
</visualElement>
4343
);
4444
}

examples/Assets/Resources/components/popup.jsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,38 @@ import React from 'react';
22
import Panel from './panel';
33

44
const backdropStyles = {
5-
position: 'absolute',
5+
position: 'Absolute',
66
left: 0,
77
top: 0,
88
width: '100%',
99
height: '100%',
1010
backgroundColor: '#000000aa',
11-
justifyContent: 'center'
11+
justifyContent: 'Center'
1212
}
1313

1414
const panelOuterStyle = {
1515
width: 400,
1616
height: 300,
17-
alignSelf: 'center',
18-
verticalAlign: 'center'
17+
alignSelf: 'Center',
18+
verticalAlign: 'Center'
1919
}
2020

2121
const panelInnerStyle = {
22-
flexDirection: 'row'
22+
flexDirection: 'Row'
2323
}
2424

2525
const buttonStyle = {
26-
alignSelf: 'flex-end',
26+
alignSelf: 'FlexEnd',
2727
width: 100,
2828
marginBottom: 15,
2929
marginLeft: 15
3030
};
3131

3232
export default function Popup({ onClose }) {
3333

34-
return <element style={backdropStyles}>
34+
return <visualElement style={backdropStyles}>
3535
<Panel outerStyle={panelOuterStyle} innerStyle={panelInnerStyle}>
36-
<button style={buttonStyle} onClick={onClose} text="Close" />
36+
<button style={buttonStyle} onMouseUpEvent={onClose} text="Close" />
3737
</Panel>
38-
</element>
38+
</visualElement>
3939
}

examples/Assets/Resources/components/tab-panel.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ export default function TabPanel({ style, children }) {
66
const [tabIndex, setTabIndex] = useState(0);
77

88
return (
9-
<element style={Object.assign(style, { color: '#ffffff', fontSize: '18px' })}>
10-
<element style={{ flexDirection: 'row', width: '100%', height: '64px' }}>
9+
<visualElement style={Object.assign(style, { color: '#ffffff', fontSize: '18px' })}>
10+
<visualElement style={{ flexDirection: 'Row', width: '100%', height: '64px' }}>
1111
{children.map((e, i) =>
1212
<Button
1313
key={i}
14-
onClick={() => setTabIndex(i)}
14+
onMouseUpEvent={() => setTabIndex(i)}
1515
innerStyle={{ backgroundColor: i === tabIndex ? '#003f6b' : '#133b50' }}
1616
style={{ width: '128px', height: '100%' }}>
1717
{e.props.name}
1818
</Button>)}
19-
</element>
19+
</visualElement>
2020
<Panel outerStyle={{}} innerStyle={{}}>
2121
{children[tabIndex].props.children}
2222
</Panel>
23-
</element>
23+
</visualElement>
2424
)
2525
}
2626

0 commit comments

Comments
 (0)