One of the most common things to do when developing iOS or Android application is to interface with external plugins (i.e. facebook, gree,urban airship) that are not provide natively through Unity. The old method used to be sticking a scripts in the “Assets\Editor” directory called either “PostprocessBuildPlayer” on iOS or adding a menu item for windows accessible via the editor. This method of madness is documented on Unity Build Player Pipeline page. However with the advent of 3.5.2, you can now write C# scripts to automate the process with having to write python/perl scripts. Documentation is lacking at this time but you can see an example here. Hopefully Unity will start documenting these features soon.
This blog is more of a reference (for me) to keep track of my efforts to research creating plug-ins for iOS and Android and streamline the build process out from Unity. Right now besides the two links describe above, I am also looking into efforts to add files, directories to an XCode project. My initial research starts with:
- Mod PBXProj – Which is a python script which allows you to modify the xcode project. While is still useful for research purposed with the advent being able to control the build using C# scripts this becomes less relevant. Still looking at the script allows an insight into how and what to manipulate to modify an xcode project to allow references to be added.
- dcariola/XCodeEditor-for-Unity – This seems a lot more relevant but haven’t found out how usable is it. If you grab the source at the time this blog was posted, you will also need to grab a the MiniJSON.cs from here and not the darktable one that pops up when you do a MiniJSON search via google. Basically what this should do is similar to the Mod PBXProj allow you to add files and directories to the XCode project. Importantly, you can also import a ‘projmods’ JSON files which will allow you to specify sequence of commands to add specific files. I haven’t yet been able to track down what format this file takes and will post more when i find out.
My current path is to start with the XCodeEditor for Unity to see if it does anything useful yet and perhaps contribute to it. If anybody can point me to the format for the projmods JSON file and the format of the .pbxproj file that would really help.
I have been using Unity for a while and just my my sake I wanted to compile a list of Do’s and Don’ts that will save you a lot of time (or if you are like me stop from going Doh! and hitting your forehead a few hours later).
1. Importing from Blender to Unity. Make sure you set the import to calculate your normals again. At least in Blender 2.56a the exports product models with extremely dark Textures. So unless you want environments in your game to be a homage to 2001 Space Odyssey.
3.Never use gravity a local variable name. In fact make sure if the editor highlight any variables that you aren’t using as a public or private variable. Bad things happen.
4. Always print out the gameobject you are acting against if you can’t find the components on it. I was trying to figure out why my projectiles wasn’t able to SendMessages or access components on a gameobject. The Tag was right. Made a fool of myself on Unity answers.
5. You CAN attach multiple scripts to a player. You can attach it to colliders on the bones that are hidden ONLY if you drag it to the scene and inspect it there. It won’t show up on the prefab.
6. Use the 80/20 rule of networking (well my 80/20 rule anyways)..If you think or findout that 80 percent of the time the variable needs to be updated, use state updates to do it versus RPC’s. I haven’t done a benchmark on Unity3D RPC’s but in my past experience with newtork coding RPC are slow, not timely and fat on network bandwidth. Easy to use again, but not to often.
7. Put something in your script name to help you find where it is. When inspecting a game object you can find scripts attached but have to hunt for the scripts. Putting all the scripts in 1 place and for me starting with a Prefix is a must. For example call player code ‘PlayerInputController’ and put it under a player folder in scripts. Good for you and good for any programmers that follow
8. Understand the code your write (or copy). There are alot of helpful scripts but the need to complete a project will drive you to cut and paste code. Soon you will have jumble of unoptimized code and if you don’t understand it a nightmare just waiting to happen.
9. The inspector can be useful to debug your game. If you select it during gameplay, you can check out public variables. It’s a quick and dirty way to do debugging rather then setting breakpoints which I haven’t done yet because it’s a pain to switch to the MonoDevelop (which is buggy, the scroll bars don’t respond to my mouse wheel and the folder ‘lock up’ so I can open it to click on scripts).
10. Beer and code don’t mix unless you have to re-write some particularly painful code. ;P
This post is a follow up to my first one which gives you some concepts on working with networking. In this post, I will cover getting an a client/server connection going which is the first thing you have to do in building a multiplayer game. Your first point of reference is to make sure you are familiar with the Unity3D Network class which provide most the functionality you need to manage an initial network session.
I would like to cover some Unity3D specific topics on how networking is handled. Unity uses a Network View Component that is attached to every game object who’s state you want to synchronize with. It also allows you to send messages directly to those objects because each one is issued a network ID. The following picture should make things clearer:
Let’s also cover the ‘responsibilities’ in networking between the Client and Server.
- The Server’s primary responsibility is managing the network session and propagating/validating state changes to connecting clients. It is also responsible for control of non-player game objects(i.e. enemies) by creating them when needed, running the AI routines which tell them to move. There are 2 modes that the server can operate under, authoritative and non. Authoritative servers have more work because they have to validate state changes from the client rather than merely passing it along to other clients. This is usually a slower way to operate as each move, action needs to be checked. If found to be invalid then the client that sent this original state changes need to be ‘brought in line’ with the way the server is thinking. A non-authoritative server on the other hand is more of a delivery person who simply relies information over to connected clients
- The Client is responsible for the view of the world a player sees and for receiving input and forwarding it over to the server. In a non-authoritative mode, the client can also process game logic (i.e. decrease health, move the player, move other objects) and these are sent to the server to be relayed to the other clients.
The first step in getting a network session up and running is initialize a server, which should be done when a level in the game loads up. This is done using the method on the Network class called “IntializeServer“. The first parameter is the number of connections to take. The second the listening port for the server, which is used by the clients to determine which application to connect on the server (as a server may be running multiple network applications). The last parameter if Network Address Translation should be used or not. All these except for the number of connections should be eventually made user configurable when you are writing your game.
Once the server is running, clients can connect using the Network.Connect method. The most important parameters for now is the ip address of the server and the remote port(which was the listening port specified earlier when the server was initialized).
That’s pretty much all there is for setting up a session. Be sure to check the Network class for more information on how to handle disconnects and network failures.
I have set about learning about networking on Unity3D in order to implement multiplayer support for Zombie Gecko’s Comrade In Arms. I have a fair amount of experience with networking, having first cut my teeth writing real-time transactional system allowing Unix hosts to update banking details to a IBM Mainframe using XNA, then working on early Internet credit card authorizations system and finally developing the server/networking code for Fung Wan Online MMOG. Well enough about me ;P
Unity3D uses RakNet for standard networking and who ever is implementing things will have a lot of plumbing to take care of inorder to get things working. Unity uses a “Networking View” component which must be attached to all objects which in turn Network State Synchronization and Remote Procedure Calls(RPC’s) to occur. You can set specific spatial components to be observed by a network view but usually you will implement a script that will pre/post process network information sent. This was something I missed when I first started out tinkering with networking on Unity
Let’s go back a bit and talk about the 2 different methods of communicating in Unity:
- State synchronization - this is basically a method of setting the network view to observe a script component that processes changes in states that can be sent to the server which in turn are transmitted to all listening clients.
- Remote Procedure Calls – This is a common enough networking concept that allows you send invoke functions on the server or all clients with parameters being passed.
You can do some additional reading on networking by looking up the following resource:
- Unity documentation
- iPhone Multiplayer demo – still works and you can substitute a web view for an iphone
- Unity Networking Example – Still works on Unity 3.3
- M2H networking tutorials – It cost money now, but there was a simple network tutorial that is floating around the network.
In a follow up posting, I will go through making connections,extrapolation,interpolation and hooking RigidBody objects.