onejs-react 0.1.11 → 0.1.12
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/package.json +1 -1
- package/src/components.tsx +31 -15
- package/src/host-config.ts +10 -1
- package/src/types.ts +2 -4
package/package.json
CHANGED
package/src/components.tsx
CHANGED
|
@@ -27,8 +27,8 @@ declare function useExtensions(typeRef: any): void
|
|
|
27
27
|
// Register ImageConversion extension methods so tex.LoadImage(bytes) works
|
|
28
28
|
useExtensions(CS.UnityEngine.ImageConversion)
|
|
29
29
|
|
|
30
|
-
// Module-level
|
|
31
|
-
const
|
|
30
|
+
// Module-level image cache shared across all Image instances
|
|
31
|
+
const _imageCache = new Map<string, any>()
|
|
32
32
|
|
|
33
33
|
function _resolveAssetPath(src: string): string {
|
|
34
34
|
const Path = CS.System.IO.Path
|
|
@@ -41,8 +41,8 @@ function _resolveAssetPath(src: string): string {
|
|
|
41
41
|
return Path.Combine(CS.UnityEngine.Application.streamingAssetsPath, "onejs", "assets", src)
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
function
|
|
45
|
-
const cached =
|
|
44
|
+
function _loadImageAsset(src: string): any | null {
|
|
45
|
+
const cached = _imageCache.get(src)
|
|
46
46
|
if (cached) return cached
|
|
47
47
|
|
|
48
48
|
const fullPath = _resolveAssetPath(src)
|
|
@@ -51,20 +51,32 @@ function _loadTexture(src: string): any | null {
|
|
|
51
51
|
return null
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
54
|
+
let result: any
|
|
55
|
+
if (src.toLowerCase().endsWith(".svg")) {
|
|
56
|
+
const svgText = CS.System.IO.File.ReadAllText(fullPath)
|
|
57
|
+
result = CS.OneJS.SVGUtils.LoadFromString(svgText)
|
|
58
|
+
} else {
|
|
59
|
+
const bytes = CS.System.IO.File.ReadAllBytes(fullPath)
|
|
60
|
+
const tex = new CS.UnityEngine.Texture2D(2, 2)
|
|
61
|
+
tex.LoadImage(bytes)
|
|
62
|
+
tex.filterMode = CS.UnityEngine.FilterMode.Bilinear
|
|
63
|
+
result = tex
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
_imageCache.set(src, result)
|
|
67
|
+
return result
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function _isVectorImage(obj: any): boolean {
|
|
71
|
+
return obj != null && obj.GetType?.().Name === "VectorImage"
|
|
60
72
|
}
|
|
61
73
|
|
|
62
74
|
/**
|
|
63
|
-
* Clear the Image component's
|
|
75
|
+
* Clear the Image component's image cache.
|
|
64
76
|
* Call this if you need to force-reload images (e.g., after replacing files on disk).
|
|
65
77
|
*/
|
|
66
78
|
export function clearImageCache(): void {
|
|
67
|
-
|
|
79
|
+
_imageCache.clear()
|
|
68
80
|
}
|
|
69
81
|
|
|
70
82
|
// Props with ref support for intrinsic elements
|
|
@@ -152,11 +164,15 @@ export const ScrollView = forwardRef<ScrollViewElement, ScrollViewProps>((props,
|
|
|
152
164
|
ScrollView.displayName = 'ScrollView';
|
|
153
165
|
|
|
154
166
|
export const Image = forwardRef<ImageElement, ImageProps>(({ src, image, ...rest }, ref) => {
|
|
155
|
-
const
|
|
156
|
-
if (src) return
|
|
167
|
+
const resolved = useMemo(() => {
|
|
168
|
+
if (src) return _loadImageAsset(src)
|
|
157
169
|
return image
|
|
158
170
|
}, [src, image])
|
|
159
|
-
|
|
171
|
+
const isVector = useMemo(() => _isVectorImage(resolved), [resolved])
|
|
172
|
+
if (isVector) {
|
|
173
|
+
return <ojs-image ref={ref} vectorImage={resolved} {...rest} />;
|
|
174
|
+
}
|
|
175
|
+
return <ojs-image ref={ref} image={resolved} {...rest} />;
|
|
160
176
|
});
|
|
161
177
|
Image.displayName = 'Image';
|
|
162
178
|
|
package/src/host-config.ts
CHANGED
|
@@ -642,7 +642,16 @@ function applyToggleProps(element: CSObject, props: Record<string, unknown>) {
|
|
|
642
642
|
// Apply Image-specific properties
|
|
643
643
|
function applyImageProps(element: CSObject, props: Record<string, unknown>) {
|
|
644
644
|
const el = element as any;
|
|
645
|
-
if (props.image !== undefined)
|
|
645
|
+
if (props.image !== undefined) {
|
|
646
|
+
const img = props.image;
|
|
647
|
+
if (img != null && (img as any).GetType?.().Name === "VectorImage") {
|
|
648
|
+
el.image = null;
|
|
649
|
+
el.vectorImage = img;
|
|
650
|
+
} else {
|
|
651
|
+
el.vectorImage = null;
|
|
652
|
+
el.image = img;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
646
655
|
if (props.sprite !== undefined) el.sprite = props.sprite;
|
|
647
656
|
if (props.vectorImage !== undefined) el.vectorImage = props.vectorImage;
|
|
648
657
|
if (props.scaleMode !== undefined) {
|
package/src/types.ts
CHANGED
|
@@ -528,14 +528,12 @@ export interface ScrollViewProps extends BaseProps {
|
|
|
528
528
|
}
|
|
529
529
|
|
|
530
530
|
export interface ImageProps extends BaseProps {
|
|
531
|
-
/** Path to image asset relative to the assets/ folder
|
|
531
|
+
/** Path to image asset relative to the assets/ folder. Supports PNG, JPG, and SVG files. */
|
|
532
532
|
src?: string;
|
|
533
|
-
/** Pre-loaded Texture2D
|
|
533
|
+
/** Pre-loaded Texture2D or VectorImage object. Type is auto-detected at runtime. */
|
|
534
534
|
image?: object;
|
|
535
535
|
/** Sprite to display (alternative to image) */
|
|
536
536
|
sprite?: object;
|
|
537
|
-
/** Vector image to display */
|
|
538
|
-
vectorImage?: object;
|
|
539
537
|
/** How the image scales to fit the element */
|
|
540
538
|
scaleMode?: 'StretchToFill' | 'ScaleAndCrop' | 'ScaleToFit';
|
|
541
539
|
/** Tint color applied to the image */
|