Comments (6)
Yep, ScriptObject
will also work, but it is specific to one engine (either Jint or ClearScript). Callback
should be able to handle all existing (and future) Javascript engines ReactUnity supports. So that you can switch the JS engine without needing to change any code.
from core.
Are you trying to pass a Javascript function as argument to C# side? If so ReactUnity has a helper class to do that, which is used internally to handle event callbacks, but is also intended for external usage. You can use it like:
// unity.cs
int counter = 0;
public void DoThing(ReactUnity.Helpers.Callback callback)
{
counter++;
callback.Call(counter);
}
//react.tsx
// You should probably memo this for better performance
const callback = Interop.ReactUnity.Helpers.Callback.From((counter: number) => {
console.log(counter);
});
...
onClick={() =>
globals.unity.DoThing(callback)
}
Note that you should also memo onClick
function but it is up to you.
from core.
Alternatively, you can convert to Callback in C# side:
// unity.cs
int counter = 0;
public void DoThing(object cb)
{
var callback = ReactUnity.Helpers.Callback.From(cb);
counter++;
callback.Call(counter);
}
// react.tsx
onClick={() =>
globals.unity.DoThing((counter: number) => {
console.log(counter);
})
}
from core.
Yes, exactly what I needed. I actually just came back here to say I figured out that using ScriptObject
worked, but that helper method seems better. Thanks!
Just for reference, this is what I was doing:
public void DoThing(ScriptObject callback)
{
counter++;
callback.Invoke(false, counter);
}
from core.
Thanks for clarifying. I mention some React tips from time to time because I guess most people using Unity won't know how to use React well. It is all ok if you know what you are doing.
from core.
Ah you're too fast. I deleted my remark about memo because I realized you suggested it due to Helpers.Callback.From()
, I'm guessing that's the part that can be slow. In which case the suggestion makes sense. Thanks again!
from core.
Related Issues (20)
- Support flex gap css property? HOT 1
- Scroll position resetting after clicking input HOT 3
- console.log only shows the first parameter HOT 1
- [UGUI] Text scaling with `rem` is not the same as in web browsers HOT 4
- [UGUI] Line-Height with `rem` is not the same as in web browsers HOT 3
- [UGUI] `<br>` tag is not respected when considering line height HOT 4
- ReactUnity polyfils are not loaded from Resources HOT 4
- `<view>` GameObject is not removed after Restart HOT 6
- [UGUI] Performance Improvement to not create GameObjects that are empty HOT 1
- [UGUI] Performance improvement for non rounded elements HOT 2
- [UGUI] Pointer events are not consistent if parent have none and children have auto HOT 3
- Image Quality/Resolution HOT 5
- onMouseDown, onPointerDown and probably more events do not work as intended HOT 5
- Auto-Reload after changes in code are not reflected or automaticaly updated. HOT 1
- Created canvases should inherit properties from the main canvas HOT 1
- What's the best practice to use Router? HOT 2
- Chinese didn't render in ReactUnity canvas HOT 2
- Inconsistent element rendering HOT 4
- [CSS] "width: 100%" seems to not work inside of Unity HOT 1
- compare with react-unity-webgl HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.