Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Winforms in Unity?

Discussion in 'Windows' started by TOES, Jun 24, 2017.

  1. TOES

    TOES

    Joined:
    Jun 23, 2017
    Posts:
    134
    I am making a Windows application that needs to combine both traditional Winforms and Unity 3D rendering. It is not a game, but a 3D tool.

    So far I have been approaching the problem by making two separate applications. One using standard Winforms in Visual Studio, and one using Unity. Then these two communicate using sockets.

    It works, but, it is a very clumsy approach.

    Another approach, that seems far better, is to use Winforms from within Unity itself. I found some old tutorials that basically tells you to copy System.Windows.Forms.dll to your Unity asset folder, turn on full Net2.0 API support and then go ahead.

    However, even the simplest test, like the one below, causes a crash and an exception (ArgumentException: GCHandle value cannot be zero)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Windows.Forms;
    3.  
    4. public class TestForms : MonoBehaviour {
    5.     void Start () {
    6.         Form f = new Form();
    7.         f.ShowDialog();  //Works, shows the form, but causes a crash
    8.     }
    9. }
    10.  
    I read somewhere that Mono no longer supports Winforms, is that true? What would be the best approach now?
     
  2. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Why do you need win forms?? you can create whatever components there using unity UI and make the whole thing a single Unity project.
     
  3. TOES

    TOES

    Joined:
    Jun 23, 2017
    Posts:
    134
    Using a custom UI gives a bad user experience.
     
  4. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,053
    I have done this but with a WPF application that integrates a 3D viewer developed in Unity that communicates with WPF via .NET remoting, so it's possible.
     
  5. TOES2

    TOES2

    Joined:
    May 20, 2013
    Posts:
    135
    Why do you ask?? It should be perfectly obvious that it is better to use the systems own UI than something you hack together yourself, which the user is not familiar with. This is not amateur hour, and naturally there are loads of uses for Unity outside the realm of games.
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,696
    There are multiple ways to integrate Unity into your application that uses different UI framework, like the -parentHwnd cmd line parameter. However, Unity does not support WinForms or anything similar outside of our own UI system in our scripting runtimes, so whatever you end up using will have to be used from outside of Unity.
     
    TOES and Huszky like this.