Saturday, 29 March 2014

Unity Inspector Tool Tips and more

When I started working with Unity 3D I looked up adding tool tips to properties in the inspector.  All the results of the searches came with a lot of code and no downloadable simple examples.

I did not spend much time on the subject because I was looking for a quick answer.  I was also probably using the wrong search criterion.



Further along I had a more important requirement that I thought was worth coding because it would save me making mistakes when setting up scenes.  As a consequence, I have leaned enough, very quickly, to create a simple but effective tool tip option for the properties, visible in the inspector.



I don't know if it is because I am using the latest version 4.3 of Unity but my solution does not need much code.  Many of the samples I looked at had huge switch or if statement blocks to allow for different property types.  I was able to use a built in method that replicated the default behaviour and allowed me to add the tool tip.




public class ToolTip : PropertyAttribute
{
    public string TipText = "";

    /// Display text when the mouse is over the label.
    public ToolTip(string message)
    {
 TipText = message;
    }
}





using UnityEditor;
using UnityEngine;
using System;

/// Display a tool tip when the mouse is over the label.
[CustomPropertyDrawer(typeof(ToolTip))]
public class ToolTipDrawer : PropertyDrawer
{
    private ToolTip source { get { return ((ToolTip)attribute); } }

 /// The height returned here must be appropriate for the property
 /// being dawn.
 public override float GetPropertyHeight (SerializedProperty property,
                                          GUIContent label) 
 {
  return GetOriginalHeight (property, label);
 }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
  // Must reset the tip otherwise all labels get the same tooltip displayed!
  string previousTip = label.tooltip;
  if (!string.IsNullOrEmpty (source.TipText))
  {
   label.tooltip = source.TipText;
  }

  // Display the default fields for this property
  EditorGUI.PropertyField (position, property, label, true);

  // Must reset the tip otherwise all labels get the same tooltip displayed!
  label.tooltip = previousTip;
 }

 /// Returns the height of the default control.
 /// 
 /// This can be reused by other property drawers.
 public static float GetOriginalHeight (SerializedProperty prop,
                                        GUIContent label) 
 {
  const float spacing = 3f;
  
  float baseSize = EditorStyles.label.lineHeight;
  
  // Most types only use one line so they work with the base but a few are 
  // multiline and need more work.
  float extraLines = 0f;
  if (prop.propertyType == SerializedPropertyType.Bounds)
  {
   extraLines = EditorStyles.label.lineHeight * 2.2f;
   baseSize = baseSize + extraLines + spacing;
  }
  else if (prop.propertyType == SerializedPropertyType.Rect)
  {
   extraLines = EditorStyles.label.lineHeight * 1f;
   baseSize = baseSize + extraLines + spacing;
  }
  
  return baseSize + spacing;
 }
}



That's it, not much code.

If you don't want to copy and paste, I've included the tool tip code and the code for other property attributes in to a GitHub project called Unity3D Utility Kit.

Download the Utility Kit from:
https://github.com/ThatJCB/Unity3DUtilityKit

The kit includes a test scene to show the usage of the attributes and to prove that they work.

At the moment the property attributes included are:
- A box to display a helpful message, which was my first idea before I got tool tips working.



- The tool tip which displays when the mouse hovers over the label.
- A Regular Expression mask for fields, created by  , many thanks.
- An integer slider that also works with float values to keep whole numbers.
- A divider with an optional heading.
- Scene selection list so you can only enter a scene name that has been included in the build list.
There's some work in progress in there as well and I'll probably add more to the list over time.



That last item, for scene selection, was the reason I started looking at the Property Attributes and Drawers in the first place.

I hope the examples will be useful to others.

Git Broke My Unity Project

I've been using Subversion (SVN) for source control for a few years but finally decided I needed to give Git a try.

For my purposes, working on my own, Subversion has served me very well.  I only decided to move because when I went to add an open source project to Codeplex it made it quite clear at every stage that SVN was not recommended and I got the feeling it was being depreciated out of existence.

It took me a while to get my head round Git and despite what the web sites claimed, I did not think that there are more choices of good Git clients than there are SVN clients.  That's just my opinion.

The Git tutorials emphasized it's de-centralised design, which for my purposes has little benefit.  I want a central master repository that holds the one version of the truth...  that's a discussion for another day.



The one command that the Git tutorials were fairly quiet about was the most important for me.  Push. After all the Add and Commit stuff is out of the way, Push uploads it to a server.  There is also the reverse Pull that I will need.



After having tried a few, the client I settled on was SourceTree.  It's written by the people who provide the BitBucket repository.  After I had used it a few times with GitHub I got to like it.  It has some handy options that make it easy for me to select the files, add them quickly and then commit and push in one easy action.


It was fairly easy to install but could do with a better getting started guide instead of some basic FAQ's.  I am not sure if it is necessary but I did need to read the instructions to create the SSH key:


I would rather not run two source control clients and I liked it enough that I have migrated my active projects.

Now for the one glitch.

All was fine to start with but then MonoDevelop v4.0.1 lost the plot!  This is the IDE launched from within Unity v4.3.  It would no longer recognise changes I made to classes, so the auto-complete functions stopped working and many sections of my code became coloured red!

The only change was that MonoDevelop had automatically recognised the Git source control and had started to use its Git Add-in.



It only took me a moment to decide that I needed to disable that.



Git add-in disabled, changes committed, Unity and Mono restarted and all was back to normal.


I am not a great fan of having everything built in to the IDE so it is no loss to me that I have had to disable the add-in.  I didn't even notice it was there until it went wrong.

Now I can get on and migrate more of my projects to Git repositories.

Saturday, 22 March 2014

Networking with Photon for Unity

I have not had as much time to spare for this project over the last month as I would have liked.  My real work that pays the bills has been so busy I have only been able to tinker here and there but I am still making good progress. Tinkering in Unity produces results much quicker than it did in XNA.

My decision to develop the menus first to learn how to use Unity has been a good choice. I have learnt a lot about the component model used by Unity and some of the best ways to do things.



I have a menu system that works with the keyboard, mouse and gamepads and I have got as far as adding split screen including a test scene to show it in action.  I have added the usual console style features, like popping up a message when  a gampad disconnects and buttons to show which keys to press.

I am itching to get on and build the levels and add characters to the game BUT from my experiences with XNA I want to sort out how network play will work before I start on the game scenes.  I will eventually get to the main subject of this article which is about using the Photon Unity Networking service.  First a bit about why I chose to use that.

With XNA I made the mistake of developing movement that was difficult to translate in to something that works over a network.

To avoid that I have been reading up on Unity networking.  To cut a long story short the Unity network model is limiting.  Not bad, it just has limits, which is exactly what I needed to know before going too far in the wrong direction.

I won't go in to detail about how Unity networking is used because that was not the point of writing this article.

All that I need to say is that having read lots of forums and tutorials most people have concluded that using a third party network provider has advantages over the built in Unity networking.  Most commonly in threads people quote that Unity have not put much effort in to improving their networking for several years.  The other area mentioned is that the Network Address Translation (NAT) punch through that end users can often have to struggle with on their home routers can be avoided with a cloud provider.

I have therefore decided to use a third party network solution by Exit Games called Photon.

There are several providers to select from but I liked Photon because it has a similar object model to the built in solution which helps when reading samples and tutorials and is also relatively easy to convert from one to the other.  They have also put a lot of effort in to producing Unity samples.

Another important consideration, for me, is that during development it is low cost.  In fact free for 10 concurrent users, which is plenty for testing.

Photon have good documentation and is easy to sign up to at no cost.

https://www.exitgames.com/

I should point out that I have no connection with Exit Games or the Photon service.

Adding Photon networking in to the game is as easy as importing the package.  Making the game work as a network game still requires the same processes of adding network views to moving objects or using Remote Procedure Call (RPC) methods but Exit games have several examples and tutorials.

I was up and running in no time and the add-in includes a Unity Editor menu for putting in the necessary details in to the demo project:


You just need your own AppID from the Photon web site:



Watch out for their web site though.  It has a drop down to swap between the various services they provide.  Look out for Photon Unity Networking (PUN).



You get different menus, different documentation and different account details if you have selected a different service.

Testing

Unity will only run one instance of the same project so I made two copies of the Photon Demo project, set one to run in the background...




...left the other unchanged.  Opened both in Unity and ran them both side by side:





Photon has another advantage in that single player and multiplayer can share the same code.  Photon can be set to offline mode and all the code just works without any changes.  I have tried that out to confirm it and it does.




if (PhotonNetwork.offlineMode)
{
    if (GUILayout.Button("Use Online Mode"))
    {
        PhotonNetwork.offlineMode = false;
    }
}
else
{
    if (GUILayout.Button("Use Offline Mode"))
    {
        PhotonNetwork.offlineMode = true;
    }
}



I added the above code in to the networked Photon Viking demo to try offline single player.

The one missing bit that I would like, is local LAN play for testing multiplayer code while not connected to the Internet.  This is not documented by Exit Games.  They do offline single player or online multiplayer.

There is a solution for offline multiplayer if you have a Windows PC.  It requires installing the server locally.  Photon provide the server executables so you can run your own server.  I don't think it was intended for testing offline but it works.

Photon provide good and quick documentation to get the server installed on a Windows PC so I won't repeat that.

I will just summarize it:

  • Download the server package
  • Copy it all to a folder
  • Run 'Photon Control' from the appropriate folder for your machine.



When I tried it out I was connected to a network.  I ran the 'Load Balancing (My Cloud)' option and set the IP address to Local.  That was just two clicks from the sys tray menus.


I had to initially set the Windows Firewall options to enable network access but that was from an automated Windows popup.  It all worked flawlessly.

I could swap between local and cloud server by changing the settings from the Photon menu:



I used the same demo, side by side but this time it used the local server.




if (!PhotonNetwork.connected)
{
    if (GUILayout.Button("Use Local Cloud"))
    {
        PhotonNetwork.PhotonServerSettings.UseMyServer(
                ServerSettings.DefaultServerAddress, 
                ServerSettings.DefaultMasterPort, 
                PhotonNetwork.PhotonServerSettings.AppID);

        PhotonNetwork.ConnectUsingSettings("v1.0"); 
    }
    else if (GUILayout.Button("Use Internet Cloud"))
    {
        PhotonNetwork.PhotonServerSettings.UseCloud (
                PhotonNetwork.PhotonServerSettings.AppID, 
                 (int)CloudServerRegion.EU);

        PhotonNetwork.ConnectUsingSettings("v1.0");
    }
}



I added the above code in to the networked Photon Viking demo to swap between local and cloud servers.

The above is all that most people will need.

The following is only useful to a very small minority that want to test networking while totally disconnected, such as on a train or in an airplane.

I hit one snag with the local server.  When I had no network connection at all the server does not run. This happens to me every day when I am working on the train.

As far as I can tell this is an oversight in the design. If all the network adapters on a Windows PC are disconnected then there is no IP address for the Photon server to bind to.

There may be some Photon server settings to enable it to work but I got round this quickly by installing the Microsoft Loopback adapter.  That is basically a dummy network interface that is always present. It is intended for testing networks but it does no harm and is included as standard with Windows installs, although not enabled.

For Windows 8 it is installed from Device Manager.
  • Open Device Manager
  • Select the Network Adapters line
  • Action -> Add legacy hardware -> Network Adapters





  • ...Manually Select from a list (It takes a minute to display the list) then select...
  • Microsoft -> Microsoft KM-TEST Loopback Adapter



  • Next, Next, Finish


That will add the extra adapter in to networking but I did not have to do anything with it.

By default this is set to use DHCP but luckily it falls back to Microsoft's
default IP address which is fine.  Just the adapter's presence is enough.



The Photon Server will now always have an IP to use locally.  Remember that IP is only any good for testing and only really necessary when no other network connections are available.

That last little bit may only be useful to a few but there is little if any documentation that I could find on using the server totally disconnected.

I can now run the demos completely offline:



I'm all setup to work on the connection and lobby scenes.