[Unity] Maximizing reuse with WPF: Compiling Noesis as DLLs
Posted: 25 Mar 2015, 21:25
Has anyone tried to share code with an existing WPF app?
Also, maybe I don't "get" Unity yet, but I have always tried to avoid having loose C# files everywhere, or from having to touch MonoDevelop. So I'm trying to go with DLLs and Visual Studio.
[[UPDATE]]: Whether I'm crazy may still be TBD, but this works! (On Windows and Windows editor at least, with some DLL juggling. I'm going to try more platforms later.) Cooool!
Also, maybe I don't "get" Unity yet, but I have always tried to avoid having loose C# files everywhere, or from having to touch MonoDevelop. So I'm trying to go with DLLs and Visual Studio.
- Create a dummy Unity project that you will use as reference. Extract the NoesisGUI unity package to it.
- Take the Assets/Plugins/NoesisGUI/* directory and turn it into a C# DLL for Windows standalone (set define flags: UNITY_STANDALONE;UNITY_STANDALONE_WIN)
- Take the Assets/Editor/NoesisGUI directory AND all of the same above files, and turn it into a DLL. (set define flags: UNITY_EDITOR.
- Pro-tip: edit a .csproj file, and add the line <Compile Include="**\*.cs" /> to grab all files in a tree.
- On Windows, I use the Link Shell Extension program to clone a tree of hard links for all the C# files. (Soft-links don't seem to work in Visual Studio.) This can save some disk space.
- In the Unity project, in the Assets directory, import the Editor/Plugins stuff, except delete (or don't import) all of the Noesis .cs files out of the above two directories that are already in the DLL.
- I gave both DLLs the same name! My post-build event creates an empty marker file (and deletes other marker files) so I can tell which file is in there. (You could also look at the DLL size.)
- Swap the DLLs depending on what you want to do: When doing a build from Unity, make sure the Windows standalone DLL is in your Unity project's Assets/ folder. When hitting play button from Unity editor, make sure the Editor dll is in there. (But don't do a copy local on UnityEditor.dll, or Bad Things(TM) will happen.)
- For some reason, to get editor working, I had to put the Assets/Editor/NoesisGUI dir into my system environment path so it could find tbb.dll, etc. I'm hoping to find another fix for this.
- My code-behind lives in a separate DLL. Reference either one the above DLLs. (Only a few things were different from my proof-of-concept WPF codebase: Points use float instead of double, good idea. And Matrix4.Transform is missing..hmmm, not sure yet what to do about that.)
- I created a simple MonoBehaviour file to take a WPF-style code-behind file (with a few ifdefs), use reflection to look for event handlers: e.g. if void MyButton_Click(object sender, RoutedEventArgs e) {...} exists, it will try to find MyButton, and its Click event, and add a delegate. (Check out the gist: https://gist.github.com/jaredthirsk/38d ... 2df41e4718)
[[UPDATE]]: Whether I'm crazy may still be TBD, but this works! (On Windows and Windows editor at least, with some DLL juggling. I'm going to try more platforms later.) Cooool!