ReactUnity Reference
website·Indexed 29 days ago
https://reactunity.github.io/Technical reference for ReactUnity, featuring documentation on CSS properties, CSS rules (including media queries, nesting, and cascade layers), animations (such as scroll-driven and motion), and Material Components like buttons, dialogs, and virtual scrolls.
What's inside ReactUnity
- ReactUnity is a UI framework for Unity that enables developers to use React and CSS to build user interfaces. It operates by running JavaScript code inside a JS engine and outputting Unity components for direct rendering within the Unity environment, providing a declarative approach to UI development similar to web technologies.
Overview of supported CSS Rules in ReactUnity
ReactUnity supports various CSS at-rules and syntax patterns to control how styles are applied, scoped, and prioritized. Key supported rules include:
@media: Applies styles based on screen, player, or device characteristics.@container: Applies styles based on an ancestor's size or style.@supports: Applies styles only if ReactUnity supports a specific property or value.@layer: Manages the cascade by ordering groups of rules independently of selector specificity.@scope: Limits the application of a block of styles to a specific subtree.Nesting: Allows rules to be nested inside other rules to avoid repeating selectors.@property: Registers custom properties to ensurevar()calls have a consistent value.
Clip elements using the clip-path CSS property
Theclip-pathproperty clips an element and all its children to a specific shape. Unlikemask-image,clip-pathchanges the actual shape of the element, meaning pointer events (like clicks) that land outside the clipped area will pass through to elements behind it. All basic CSS shapes are supported, buturl()pointing to an SVG<clipPath>is not supported..stage { width: 300px; height: 200px; background-image: linear-gradient(135deg, gold, crimson); clip-path: circle(45%); }Blend background layers using background-blend-mode
Thebackground-blend-modeproperty blends an element's background images and its background color together. Only the background is affected; borders, text, and child elements are painted over the result. To blend the entire element into the page (including its content), usemix-blend-modeinstead..stage { width: 300px; height: 200px; background-color: #202040; background-image: linear-gradient(120deg, gold, transparent 70%), linear-gradient(20deg, deepskyblue, crimson); background-blend-mode: multiply; }Control CSS precedence with Cascade Layers
The@layerat-rule allows you to group CSS rules into named layers. The order of these layers determines which rules win, regardless of selector specificity. This is primarily used to prevent framework styles from overriding application-specific styles.@layer framework, app; @layer framework { #title { color: white; /* High specificity, but in an earlier layer */ } } @layer app { text { color: red; /* Low specificity, but in a later layer; this wins */ } }Use the <view> component as a basic container
The<view>component is the simplest container component and the fundamental building block of ReactUnity. It functions similarly to adivelement in web development, allowing developers to group other components and apply layout styles such as padding and background colors.export default function App() { return ( <view style={{ padding: 20, backgroundColor: 'crimson', color: 'white', }}> Hello world </view> ); };Animate elements in ReactUnity
ReactUnity provides several mechanisms for animating elements and handling transitions, primarily leveraging CSS-like properties:
- Custom Animations: Use CSS
keyframesand theanimationproperty. - Scroll-Driven Animations: Use
animation-timelineto drive keyframes based on scroll position or an element's passage through a scroll view instead of a clock. - Property Transitions: Use the
transitionproperty to animate changes between property states. - Lifecycle Animations: Use the
:enterand:leavepseudo-classes combined with thetransitionproperty to animate elements when they are inserted into or removed from the UI. - Rect Transform Animations: Use the
motionproperty to animate changes specifically to the rect transform. - Audio Playback: Use the
audioproperty to trigger and play audio clips.
- Custom Animations: Use CSS
Implement size-based container queries
Size queries monitor features likewidth,height,inline-size,block-size,aspect-ratio, andorientation. They supportmin-/max-prefixes, range syntax, and logical operators (and,or,not). Lengths in conditions are resolved against the container's font size (e.g.,20emis 20x the container's font size).@container (min-width: 400px) { ... } @container (width > 400px) { ... } @container (400px <= width < 800px) { ... } @container sidebar (orientation: portrait) { ... } @container (aspect-ratio > 16/9) and (height >= 200px) { ... } @container not (width < 300px) { ... }Set up Unity UI (ugui) and UI Toolkit (uitoolkit) runtime backends
To use runtime UI backends in Unity, add the corresponding renderer component to a GameObject and configure it via the inspector:
- For Unity UI: Add the
ReactRendererUGUIcomponent. - For UI Toolkit: Add the
ReactRendererUIToolkitcomponent.
- For Unity UI: Add the
Implement scroll-state container queries
Scroll-state queries require
container-type: scroll-state.scrollable: Checks if the container can still scroll towards specific edges (top,right,bottom,left,block-start,block-end,inline-start,inline-end), axes (x,y,inline,block), ornone.stuck: Checks if aposition: stickybox is currently stuck to an edge. The element withposition: stickymust also havecontainer-type: scroll-state.
/* Scrollable example */ scroll { container-type: scroll-state; } @container scroll-state(scrollable: bottom) { scroll .more-below { opacity: 1; } } /* Sticky/Stuck example */ .header { position: sticky; top: 0; container-type: scroll-state; } @container scroll-state(stuck: top) { .header .title { font-size: 14px; } }Animate element insertion and removal using :enter and :leave
Use the CSS
:enterand:leavepseudo-selectors in combination with thetransitionproperty to create animations when elements are inserted into or removed from the DOM.:enter: Applied when an element is inserted into the DOM.:leave: Applied when an element is removed from the DOM.state-duration: Use this property to control how long these pseudo-classes remain applied to the element.
.item { flex-direction: row; align-items: center; transition: all 0.5s; state-duration: 0.5s; } .item:enter { state-duration: 0s; translate: -100px 0; opacity: 0; } .item:leave { translate: 100px 0; opacity: 0; }Use the <video> component to play videos
The<video>component is used to embed and play video content. It accepts asourceprop which can be a URL string or aVideoobject. To control playback (Play/Pause), you can use a ref to access the underlyingVideoPlayerobject.import { useState, useEffect } from 'react'; export default function App() { const videoAddress = 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4'; const [videoRef, setVideoRef] = useState(); useEffect(() => { if (videoRef) { // Start playback when the component is ready videoRef.VideoPlayer.Play(); } }, [videoRef]); const toggleVideo = () => { const vp = videoRef.VideoPlayer; if (vp.isPlaying) vp.Pause(); else vp.Play(); }; return <> <video style={{ objectFit: 'contain', width: 500 }} source={videoAddress} ref={setVideoRef} onPointerClick={toggleVideo} /> </>; }