Last week I talked about how I used UDP to open up a TCP socket to send a file to the client. This week I did some more testing and it turns out that I don’t need to open up TCP in order to send an image. Since my solution was to use localhost as the communication platform between the two programs, the files that you want to use will also be on the local harddrive.
If the program was meant to be used remotely it would make more sense. So now all the images are loaded from the harddrive and the files are not copied to the root folder as they were.
The Unity interface now runs at around 1-2% cpu which was where my target performance should be. It runs to around 20-25% if you want to render as many fps as you want. I’m leaving the option there for now. But later I will remove that part. Since the capture software only runs at 30fps anyway there’s no real point in running 1000 fps.
The project
I created a windows forms application. I did some modifications to it, so I have a gameloop instead of the event only based system that you normally get in windows forms. The good thing is that I can set my own update and draw loops if I so desire. That said, in this case it turns out that the control panel takes up way more CPU than intended, so that will have to change. Luckily the rest of the code is within a standard windows forms, so the code can be copied and pasted in and it should still work.
So far the control panel of the project looks like this:
I will have to change that, so far it’s just basic controls. Each of the color buttons, opens up a color picker dialogue box. The Add or change buttons opens up a file dialog box. Rest is just to turn stuff on and off.
The Unity window looks like this:
In this window you can rotate, move around different elements. Color changes and images are gotten from the control panel. The ticker on the bottom is a text that scrolls along the bottom. After a certain interval it restarts and displays the text again.
When exiting Unity, the text, text color, logo image, logo placement and scale, the background and background of the ticker, are all saved to a local xml file. So if you close the program down, you have the settings from last time.
I have also added a basic functionality for showing a webcam on the screen. But so far there is no chroma key for it, that is something I have to add, which shouldn’t be too hard to do. The sorting order is not yet saved and the same with adding new images to the scene. That is something I’m working on.
The saving method is using the IFormatter and XMLWriter to create an XML file. In the beginning I wanted to serialize using binary format. But then I figured that using XML let the user read the file and modify it. If they want to do that it’s fine by me.
Going forward
I consider the program to be more or less “done” now. It’s not finished, but what I set out do do and more I have accomplished with this. Normally I would start the other way, reaching out to people asking what they need and create a spec and create prototypes. That way people can point to features they would like and how they want to use the program. In this case however I just wanted to see how to communicate between programs. Using the Twitch overlay to do this was more or less just a way to test that out.
But now that I have a fully working system, I was thinking of maybe expanding upon this. Using overlay like NodeCG is a bit cumbersome, and even though every function needs to be programmed in, you can do a lot more faster and easier once the system is setup as it should.
All that remains now is really to save the other images and save their positions.
Lessons learned
Using the UDP packet to open up TCP is a fine solution. Not really the solution I needed, but I see how powerful that combination can be.
Creating a robust package type and handler that all are encoded in the same way making it easier to add functionality. I went from 4 different package types to 10 and implemented how it works in a matter of an hour or so. Where most of the time was spent to create the event for that package arriving and working around the problem of not running gameobject related code in a separate thread. The packages I created was really simple too. Just a header with the type of package arriving with the length of that package and everything that follows is the information. Whether it is text or numbers. As long as you decode the package on the other end in the same fashion it’s easy to handle the packages.
If you want to create a program for a specific need, research that need first then do the program. I fear that my lack of research will make the program either obsolete or usable but bad to use. I know that a lot of streamers use hotkeys a lot, and my program has none so far. Also I put the setting on the Unity window, for all I know maybe you rather want settings in the control panel so that you can load up settings for a certain game instead. Or maybe they want both.
I don’t know and until I can reach a streamer that is interested in something like this, it’s a guessing game. That being said, it’s very easy to change aspects of the program if I so desire.
Creating textures on the fly in Unity is very simple. All you need is to create a texture of a certain width and height. Then you fill an array with the same amount of colors that you have in the array and set that into the texture. Since I was using a sprite I used the Sprite.Create method and added in the texture, and a rectangle and the pivot point. For me the pivot was always new Vector2 (0.5f, 0.5f) to make the pivot in the middle.
Another thing I learned is that the deserialize method doesn’t work as well as the serialize method does. I had to write my own XML parser for the file. But I had created an XML parser before so that was not so difficult.
That’s it for now. My next task ahead is to make sure that if you add images that they get added back in. Other than that I have no more plans for this program until I get any feedback. I might fiddle with the webcam and chroma key, just for the heck of it.