onejs-react 0.1.12 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -0
- package/package.json +1 -1
- package/src/__tests__/mocks.ts +1 -0
- package/src/components.tsx +0 -8
- package/src/host-config.ts +13 -6
- package/src/types.ts +11 -2
package/README.md
CHANGED
|
@@ -258,6 +258,27 @@ type MeshGenerationContext = CS.UnityEngine.UIElements.MeshGenerationContext
|
|
|
258
258
|
type GenerateVisualContentCallback = (context: MeshGenerationContext) => void
|
|
259
259
|
```
|
|
260
260
|
|
|
261
|
+
## C# Interop Utilities
|
|
262
|
+
|
|
263
|
+
### `toArray<T>(collection): T[]`
|
|
264
|
+
|
|
265
|
+
Converts C# collections (`List<T>`, arrays) to JavaScript arrays. C# collections exposed through the OneJS proxy have `.Count`/`.Length` and indexers but lack `.map()`, `.filter()`, and other array methods.
|
|
266
|
+
|
|
267
|
+
```tsx
|
|
268
|
+
import { toArray } from "onejs-react"
|
|
269
|
+
|
|
270
|
+
// Convert a C# List for use in JSX
|
|
271
|
+
{toArray<Item>(inventory.Items).map(item => <ItemView key={item.Id} item={item} />)}
|
|
272
|
+
|
|
273
|
+
// Convert a C# array
|
|
274
|
+
const resolutions = toArray<Resolution>(Screen.resolutions)
|
|
275
|
+
|
|
276
|
+
// Safe with null — returns []
|
|
277
|
+
const npcs = toArray(currentPlace?.NPCs)
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Supports objects with `.Count` (List, IList) or `.Length` (C# arrays). Returns `[]` for null/undefined.
|
|
281
|
+
|
|
261
282
|
## Dependencies
|
|
262
283
|
|
|
263
284
|
- `react-reconciler@0.31.x` (React 19 compatible)
|
package/package.json
CHANGED
package/src/__tests__/mocks.ts
CHANGED
package/src/components.tsx
CHANGED
|
@@ -67,10 +67,6 @@ function _loadImageAsset(src: string): any | null {
|
|
|
67
67
|
return result
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
function _isVectorImage(obj: any): boolean {
|
|
71
|
-
return obj != null && obj.GetType?.().Name === "VectorImage"
|
|
72
|
-
}
|
|
73
|
-
|
|
74
70
|
/**
|
|
75
71
|
* Clear the Image component's image cache.
|
|
76
72
|
* Call this if you need to force-reload images (e.g., after replacing files on disk).
|
|
@@ -168,10 +164,6 @@ export const Image = forwardRef<ImageElement, ImageProps>(({ src, image, ...rest
|
|
|
168
164
|
if (src) return _loadImageAsset(src)
|
|
169
165
|
return image
|
|
170
166
|
}, [src, image])
|
|
171
|
-
const isVector = useMemo(() => _isVectorImage(resolved), [resolved])
|
|
172
|
-
if (isVector) {
|
|
173
|
-
return <ojs-image ref={ref} vectorImage={resolved} {...rest} />;
|
|
174
|
-
}
|
|
175
167
|
return <ojs-image ref={ref} image={resolved} {...rest} />;
|
|
176
168
|
});
|
|
177
169
|
Image.displayName = 'Image';
|
package/src/host-config.ts
CHANGED
|
@@ -65,6 +65,7 @@ declare const CS: {
|
|
|
65
65
|
GPU: {
|
|
66
66
|
GPUBridge: {
|
|
67
67
|
SetElementBackgroundImage: (element: CSObject, rtHandle: number) => void;
|
|
68
|
+
SetElementBackgroundFromObject: (element: CSObject, obj: CSObject) => void;
|
|
68
69
|
ClearElementBackgroundImage: (element: CSObject) => void;
|
|
69
70
|
};
|
|
70
71
|
};
|
|
@@ -350,12 +351,18 @@ function applyStyle(element: CSObject, style: ViewStyle | undefined): Set<string
|
|
|
350
351
|
s[prop] = parsed;
|
|
351
352
|
appliedKeys.add(prop);
|
|
352
353
|
}
|
|
353
|
-
} else if (key === "backgroundImage"
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
354
|
+
} else if (key === "backgroundImage") {
|
|
355
|
+
if (value == null) {
|
|
356
|
+
CS.OneJS.GPU.GPUBridge.ClearElementBackgroundImage(element);
|
|
357
|
+
} else if (isRenderTextureHandle(value)) {
|
|
358
|
+
// GPU compute RenderTexture handles (via rt.getUnityObject())
|
|
359
|
+
const handle = getRenderTextureHandle(value);
|
|
360
|
+
if (handle >= 0) {
|
|
361
|
+
CS.OneJS.GPU.GPUBridge.SetElementBackgroundImage(element, handle);
|
|
362
|
+
}
|
|
363
|
+
} else if (typeof value === "object" && "__csHandle" in value) {
|
|
364
|
+
// C# objects: Texture2D, Sprite, VectorImage, RenderTexture
|
|
365
|
+
CS.OneJS.GPU.GPUBridge.SetElementBackgroundFromObject(element, value);
|
|
359
366
|
}
|
|
360
367
|
appliedKeys.add(key);
|
|
361
368
|
} else {
|
package/src/types.ts
CHANGED
|
@@ -82,10 +82,19 @@ export interface ViewStyle {
|
|
|
82
82
|
/** Background color. Examples: "#3498db", "rgba(0,0,0,0.5)", "red" */
|
|
83
83
|
backgroundColor?: StyleColor;
|
|
84
84
|
/**
|
|
85
|
-
* Background image - accepts
|
|
85
|
+
* Background image - accepts Texture2D, Sprite, VectorImage, RenderTexture,
|
|
86
|
+
* or a GPU compute RenderTexture handle.
|
|
86
87
|
*
|
|
87
|
-
* For GPU compute RenderTextures, you can pass the RenderTexture object directly:
|
|
88
88
|
* @example
|
|
89
|
+
* // Texture2D
|
|
90
|
+
* const tex = loadImage("images/card-bg.png")
|
|
91
|
+
* <View style={{ backgroundImage: tex }} />
|
|
92
|
+
*
|
|
93
|
+
* // Sprite from C#
|
|
94
|
+
* const sprite = getSpriteFromCSharp()
|
|
95
|
+
* <View style={{ backgroundImage: sprite }} />
|
|
96
|
+
*
|
|
97
|
+
* // GPU compute RenderTexture
|
|
89
98
|
* const rt = compute.renderTexture({ width: 100, height: 100 })
|
|
90
99
|
* <View style={{ backgroundImage: rt }} />
|
|
91
100
|
*/
|