Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8341669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:33:10+00:00 2026-06-09T05:33:10+00:00

is it possible at all to make C/C++ function callback into Unity scripts, provided

  • 0

is it possible at all to make C/C++ function callback into Unity scripts, provided that you can create a new thread from the scripts? I tried but Unity crashes as soon as the scripts get executed.

I googled about it and found this thread which says

Unity is not callback nor threadsafe, any access to classes that
extend UnityEngine.Object is only allowed from within the same thread
as unity scripts are running in, not asyncronous from other threads
nor from asyncrnous callbacks from COM / OS async operations

If thats the case for you there are two possible ways:

(1) Write a wrapper that gets these callbacks and queues the stuff and
then expose a function that allows unity to request the next event /
dataobject or whatever in a blocking, unthreaded form (2) Make the
callbacks call into a static function on something extending from
System.Object and write the same kind of logic as above to request the
information on classes extending UnityEngine.Object

But I think if I create a thread and callback into that thread, it will be okay right? I am thinking like this because I’ve read threads like this one that introduces how to make C functions calling back C# functions. So I reasoned that if I create a new thread, it’s no longer Unity, it will just be mono and C#.

Here is my code that crashes Unity:

The C++ code:

#include <iostream>
// #include "stdafx.h"

typedef int (__stdcall * Callback)(const char* text);

Callback Handler = 0;

extern "C" __declspec(dllexport)
void __stdcall SetCallback(Callback handler) {
    Handler = handler;
}

extern "C" __declspec(dllexport)
void __stdcall TestCallback() {
    int retval = Handler("hello world");
}

The C# code:

using UnityEngine;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;


class UnManagedInterop : MonoBehaviour {
  private delegate int Callback(string text);
  private Callback mInstance;   // Ensure it doesn't get garbage collected


  public void Test() {
        mInstance = new Callback(Handler);
        SetCallback(mInstance);
        TestCallback();
  }

  private int Handler(string text) {
    // Do something...
    print(text);
    return 42;
  }

  [DllImport("test0")]
  private static extern void SetCallback(Callback fn);
  [DllImport("test0")]
  private static extern void TestCallback();

    void Start()
    {
        Thread oThread = new Thread(new ThreadStart(Test));

        // Start the thread
        oThread.Start();


    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:33:12+00:00Added an answer on June 9, 2026 at 5:33 am

    The answer is Yes!

    I tested again on August 8, 2012, with Unity 3.5.2f2, Pro license. Thanks for @hatboyzero’s comment I found this example.

    Although the code in my question doesn’t work, the following code works:

    // C#
    using System.Runtime.InteropServices;
    
    class Demo {
        delegate int MyCallback1 (int a, int b);
    
        [DllImport ("MyRuntime")]
        extern static void RegisterCallback (MyCallback1 callback1);
    
        static int Add (int a, int b) { return a + b; }
        static int Sub (int a, int b) { return a - b; }
    
        void Init ()
        {
            // This one registers the method "Add" to be invoked back by C code
            RegisterCallback (Add);
        }
    }
    
    
    // C
    typedef int (*callback_t) (int a, int b);
    static callback_t my_callback;
    
    void RegisterCallback (callback_t cb)
    {
        my_callback = cb;
    }
    
    int InvokeManagedCode (int a, int b)
    {
        if (my_callback == NULL){
             printf ("Managed code has not initialized this library yet");
             abort ();
        }
        return (*my_callback) (a, b);
    }
    

    I didn’t have to embed MonoRuntime as the tutorial suggests. Just the above two pieces of code solved my problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a function that can take an enum type, display all
Is it possible to make all function's vars global without typing all of them
I would like to make a function that will create objects that have a
am making a function that will send me a list of all possible elemnts
Is it at all possible to use Type Erasure to create objects that encapsulate
What all possible support is there from open source world for building web based
Possible Duplicate: Python code to pick out all possible combinations from a list? I
Is it at all possible to create a static/tv interference/twitch effect similar to what
How can I enumerate all possible instances of System.Char ? I need to see
I want to keep everything contained within a single function, and not make all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.