Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. embedding unity player with pyqt5

embedding unity player with pyqt5

Scheduled Pinned Locked Moved Unsolved Qt for Python
1 Posts 1 Posters 240 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pythonic person
    wrote on 30 Nov 2023, 16:46 last edited by
    #1

    hi, am trying to embed unity player within my pyqt5 application.
    the following code works good with displaying the content of the game, but the problem is I can't interact with the game at all.

    when I launch the game separately wherever I move the mouse I can change the camera view. but not any more with embedding it.

    here is my current code:

    import win32api
    import win32process
    from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel
    from PyQt5.QtCore import Qt, QProcess
    import sys
    import time
    import win32gui
    from PyQt5.QtGui import QWindow
    import ctypes
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        main_window = QWidget()
        main_window.setMinimumSize(600, 600)
        container = QWidget()
    
        # Create layout
        grid_layout = QGridLayout(main_window)
    
        unity = QProcess()
        unity.setProgram("C:/Users/USER/My project (2)/win32/My project (2).exe")
        unity.start()
        unity.waitForStarted()
        time.sleep(15)
    
        # Get the window handle for the spawned Unity instance
        hwnd = win32gui.FindWindow(0, "My project (2)")
        UnitySceneWindow = QWindow.fromWinId(hwnd)
        unityWidget = QWidget.createWindowContainer(UnitySceneWindow, main_window, Qt.FramelessWindowHint)
        unityWidget.setFocusPolicy(Qt.TabFocus)
    
        # Get the thread IDs for the Qt and Unity windows
        qt_thread_id = win32api.GetCurrentThreadId()
        _, unity_thread_id = win32process.GetWindowThreadProcessId(hwnd)
    
        # Detach the Qt process input from the Unity process
        ctypes.windll.user32.AttachThreadInput(qt_thread_id, unity_thread_id, False)
    
        # Detach the Unity process input from the Qt process
        ctypes.windll.user32.AttachThreadInput(unity_thread_id, qt_thread_id, False)
    
        # Activate the Unity window
        ctypes.windll.user32.SendMessageA(hwnd, 6, 1, 0)
    
        main_window.show()
        app.exec_()
    
    

    I read about the same problem with the following thread:
    https://forum.unity.com/threads/unity-player-embedded-into-qt-application.537879/
    but couldn't fix the problem
    please help I was stuck with this problem 3days

    1 Reply Last reply
    0

    1/1

    30 Nov 2023, 16:46

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved