Giter VIP home page Giter VIP logo

Comments (6)

KurtGokhan avatar KurtGokhan commented on May 29, 2024 1

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.

KurtGokhan avatar KurtGokhan commented on May 29, 2024

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.

KurtGokhan avatar KurtGokhan commented on May 29, 2024

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.

Raicuparta avatar Raicuparta commented on May 29, 2024

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.

KurtGokhan avatar KurtGokhan commented on May 29, 2024

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.

Raicuparta avatar Raicuparta commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.