Anyone know of a software that runs in the system tray that I can link to my Unity build to make it hide / continue running in the background?
so other words trying to make it so I can have a system tray icon for Unity build.
I read somewhere that it seems the best idea is to just use a separate software that can hide and show the Unity build.
Running in the background is a standard option in Unity.
For the system tray icon and window hiding I assume you are targeting Windows only. In that case I would use some Windows specific calls (from a Unity script) to do the job. I’m guessing these functions are in user32.dll. I’ve placed some code on Windows specific window maximizing in this topic:
Your setup should be fairly similar, but of course with different calls to user32.dll.
Edit: Hiding a window can be done with the code I’ve linked to above.
ShowAsync(CmdShow.Hide);
For the system tray you just have to create a NotifyIcon and set it to visible:
Thanks man that’s a big step forward for me.
I haven’t made a c charp app before using visual studio for the build.
So that may be the only hic cup for me.
Hopefully that’s pretty simple.
I went from php/ java script straight to unity as a coder
So I skipped building with visual studio
Hes got you in the right direction, if you succeed with using Windows.Forms namespace and sytem32.dll in unity, then Ill add something too keep an eye out for.
Tray-icons don’t always disappear after closing the app and it will linger until you mouse ovedr, especially when running outside of build mode.
If this happens you need to hook into the event when the app closes and set your Tray Icon to .Hide() and .Dispose() or something along them lines.
Just thought I’d save you a google search later on
Thanks cyber
I already failed massively on trying to do the tutorial lol.
I might have to pay someone to do it.
I’m really time sensitive atm.
I got stuck on this part of the tutorial
Inside the constructor for MyApplicationContex, you can insert the code to initialize the NotifyIcon.
MenuItem configMenuItem = new MenuItem("Configuration", new EventHandler(ShowConfig));
MenuItem exitMenuItem = new MenuItem("Exit", new EventHandler(Exit));
NotifyIcon notifyIcon = new NotifyIcon();
notifyIcon.Icon = TaskTrayApplication.Properties.Resources.AppIcon;
notifyIcon.ContextMenu = new ContextMenu(new MenuItem[]
{ configMenuItem, exitMenuItem });
notifyIcon.Visible = true;
ShowConfig says it doesn’t exist so idk. Plus several other things.
I’m probably not even setting up the forms project correctly.
It just says create a WinsForms project.
I even noticed visual studio basic tutorials are super out of date from the newest version of Visual Studio. I couldn’t even follow the “Build your first app in 5 minutes” tutorial cause of how out of date it is. Which was the very first thing when you first open Visual studio.
So what I did was I created a new project as a RuForm that seem to match up to the tutorial but its probably whats wrong.
You shouldn’t have to pay someone, it is rather easy. Well I have only done it for a WinForm and never in Unity. I am aware though that there are issues when it comes to referencing the namespace like I mentioned earlier. I would google “using system.windows.forms in unity”
As for the code itself, here is something you should be able to chuck straight into an application and test. I recommend doing this outside of Unity first. Start a new project in Visual Studio (Windows Forms Presentation).
Once you understand what’s happening there, here is something to get you going for Unity, it wont work of the bat, or may, I have not tested. But like I said, first address the issue from the top paragraph.
FYI, from the Microsoft example Form1() is basically Start() for a script in Unity.
using System;
using System.Drawing;
using System.Windows.Forms;
using UnityEngine; //Drop if thorwing an error
//You probs need to use this instead of unitys : MonoBehaviour
public class TrayIconTest : System.Windows.Forms.Form
{
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.ComponentModel.IContainer components;
public void MakeTrayIcon()
{
components = new System.ComponentModel.Container();
contextMenu1 = new System.Windows.Forms.ContextMenu();
menuItem1 = new System.Windows.Forms.MenuItem();
contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {menuItem1});
menuItem1.Index = 0;
menuItem1.Text = "E&xit";
menuItem1.Click += new System.EventHandler(menuItem1_Click);
notifyIcon1 = new System.Windows.Forms.NotifyIcon(components);
//Note This is a System.Drawing.Icon type, google if you get stuck
notifyIcon1.Icon = new Icon("appicon.ico");
notifyIcon1.ContextMenu = contextMenu1;
notifyIcon1.Text = "Unity Tray Icon Test";
notifyIcon1.Visible = true;
notifyIcon1.DoubleClick += new System.EventHandler(notifyIcon1_DoubleClick);
}
/* Learn were to call this, you want it to be when the application is closing
Sorry not sure were to do it in Unity*/
public void Cleanup()
{
//If your getting errors here, play around with the syntax.
//Your wanting to cleanup and remove the tray icon before the application closes,
//so it doesnt linger in limbo which happens sometimes as a windows bug
notifyIcon1.Hide();
notifyIcon1.Dispose();
}
// I forgot what Dispose() is, and if it will even work in Unity haha :)
// It might be the equivient for OnDestroy() for Unity Objects
//See how you go running withought it
/*
protected override void Dispose( bool disposing )
{
// Clean up any components being used.
if( disposing )
if (components != null)
components.Dispose();
base.Dispose( disposing );
} */
private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
{
Debug.Log("You double clicked the icon");
}
private void menuItem1_Click(object Sender, EventArgs e)
{
Debug.Log("You Selected Exit");
}
}
Oh, and for the Icon. If your having trouble I would look at declaring it as a variable at the top and referencing the file at the start.
Oh, and most importantly haha
To use this in unity, you would call it from a script attached to a game object. like this.
TrayIconTest trayIconTest = new TrayIconTest();
trayIconTest.Icon = Unity.LoadResource // Or something like that if you declared an icon variable :)
trayIconTest.MakeTrayIcon();