Archive for July, 2011

SingletonBehavior – Auto gameobject management

Through all my development in unity, having a Monobehavior be a singleton has been very useful in a lot of cases. Making a gameObject a singleton isn’t very difficult but it can be a little annoying to do over and over again, so I’ve made a child class for MonoBehavior.

Here is the class itself

using UnityEngine;
using System.Collections;

public class SingletonBehaviour : MonoBehaviour where T : UnityEngine.Component
{
static T instance;
static public T Instance
{
get
{
if (instance == null)
{
instance = MonoBehaviour.FindObjectOfType(typeof(T)) as T;
if (instance == null)
{
GameObject go = new GameObject(typeof(T).ToString());
instance = go.AddComponent();
}
}

return instance;
}
}

protected virtual void Start()
{
if (instance != null && instance != this)
{
Destroy(gameObject);
return;
}

DontDestroyOnLoad(gameObject);
}
}

implementing it in a class is super easy.

public class YourClass: SingletonBehavior
{
}

networkView.group powers

Something that is almost completely missed in the documentation/tutorials for networking in unity is the ability to set a networkView’s group. Because you can do this, you can choose to have a client ignore different ‘channels’ of information. To set a networkView’s group, you just have to assign it in a behavior script, like so.

networkView.group = 11;

For example, if you want the client to only get updates for the lobby chat window, you just set the NetworkPlayer to not receive these messages, or the server to not send these messages. Via the two very useful static network methods.  Network.SetSendingEnabled will allow you to prevent the server from sending a networkView group to the client, or you can choose to ignore the received messages client side with Network.SetReceivingEnabled, this however will have the server send those messages to the client, they just wont be used.

Using one of these methods will allow you to control when a client gets a specific kind of update. Like I said before, if you have a lobby chat, simply set the group to some group not used by any other system, and set the server to not send those group updates to the client, this will mean that the user in the lobby will only get the lobby update group. Then after they join the server they can have the other (or the specific scenes!) groups turned on.

This also means that its possible to have a networked game with multiple different scenes running at a time, you just need to control what groups are being sent, and possibly use an offset for each internal different group type, for example, the ‘dungeon of angry squirrels’ uses group 11, and the chat window inside the dungeon uses group 12 (scene group +1), or for simplicity sake, you can keep it all on the single group, but if you wanted say, global chat, or to be able to see your party members health, you’d use a group that is always left on.

Social Games Get over yourselves !

Recently, I am seeing loads of articles popping up on social games being “copied” by other game companies.   I would like to say to those companies, welcome to the real world.

 

The only thing that is protected about a game is Content related, and that’s how it should stay.  Your games are simple, and if they are successful they will be copied, and possibly done better then your original implementation.   This is part of the game industry and what drives people to try to do things better or to just replicate with a different spin.

 

Stop trying to play mud smear wars and get off your asses and make a better game.  Do you want to cry about having competition?  I am sure all the developers out there who slave over FPS games will surely rub your eyes and tell you it’s all gonna be OK.   I’m sure after they will whack you upside the head and remind you their genre is probably one of the most saturated and competitive.

 

In short, get real.  You are in one of the competitive cut throat areas of game development today.  You’re just gonna have to adapt!  If you think I am being harsh or that for some reason your implementation should have the right to patent your ideas.   Let me give you a strong word of warning:

 

Your game was done before, by someone else that you copied bits and pieces from.  Perhaps some kid made a game exactly like the one you did on Kongragate, but just not as good as you because he has no budget.  If he could patent his idea, then you would be paying him royalties as long as your game made money.

 

To wrap this up, get over it and welcome to our cutthroat world.   This is what you have to look forward too when you make games more about money then about a good product.

In-Game Console

In-Game Console Screenshot

In the process of learning more about network code in unity, to our eventual goal of making an online game that is peer-to-peer it became useful to have an in-game console (like old school fps consoles that pop down from the top of the screen). I made a pretty good implementation of a console that works very well and is really easy to expand.

Console.Log(“message”); // create a message in the log

The above example would print the message to the log, (a timestamp would be printed before it). All of the needed functions for the console are static so they can be accessed easily from anywhere. This includes the following function, ‘Console.AddCommand’ and the above ‘Console.Log’.

Console.AddCommand(new ConsoleCommand(“command”,
“help text displayed, when help is called”,
CmdParameterType.None, targetGameObject,
“MethodInGameObject”)); // This method (And class) creates a new console command

The above example, would work like so, type “command” into the console, and the targetGameObject would have a ‘SendMessage’ fired to ‘MethodInGameObject’

The console commands work in a very simplistic way, they simply handle a SendMessage operation for you, as well as parsing your parameter into something useful for the receiving message method. So far the supported parameter types are, int, float, string, gameobject(a string that is used to preform a ‘GameObject.Find’ operation), Vector3 and Vector2. If anyone has any suggestions for more supported parameter types id love to hear them, but keep in mind, due to the fact this all works via SendMessage, only one parameter is possible, however, you can send a string and parse in the reviving message method.

I also plan on writing some default commands, right now there is just a help command that prints out all of the bound commands, and when a command is added after words (‘help’) it will print out the help text created with the command. I’m also looking for suggestions on what kind of default commands would be useful, so please, comment away! Apon completion it will become available on the unity asset store.

Unity Integrated Networking

I’ve been trying to learn the integrated networking (Raknet) for unity over the last few weeks. We are planning on making a peer-to-peer game using it in the near future. After a lot of reading of tutorials and looking at projects source code I finally understand it to a pretty good degree. In the process I developed a very clean master game server connection script that includes the ability to create a server, find servers with the same game type name, and verify the connection before moving the user onto a new scene. Below is the script I ended up creating.

 

 

using UnityEngine;
using System.Collections;

public class MasterGameConnector : MonoBehaviour
{
	public int port = 31337;
	public string gameTypeName = "change this name";
	public string serverDefaultName = "some server";
	public string readyScene = "sceneName";

	enum ScreenState
	{
		Start,
		Create,
		Find,
		Setup,
		Ready
	}
	Vector2 serverScroll = Vector2.zero;
	ScreenState currentScreenState = ScreenState.Start;
	string serverNameField;
	bool connected = false;

	Vector2[] windowSizes = new Vector2[] {
		new Vector2(150, 75),
		new Vector2(200, 120),
		new Vector2(200, 300),
		new Vector2(200, 75)
	};

	Rect GetCenteredRect(Vector2 size)
	{
		return new Rect((Screen.width*0.5f)-(size.x*0.5f),
		                (Screen.height*0.5f)-(size.y*0.5f),
		                size.x, size.y);
	}

	// Use this for initialization
	void Start ()
	{
		serverNameField = serverDefaultName;
	}

	void OnDisconnectedFromServer()
	{
		currentScreenState = ScreenState.Start;
		connected = false;
	}

	void OnServerInitialized()
	{
		connected = true;
	}

	void OnConnectedToServer()
	{
		connected = true;
	}

	void OnGUI()
	{
		if (currentScreenState == ScreenState.Ready)
			return;

		GUI.Window(GetHashCode(), GetCenteredRect(windowSizes[(int)currentScreenState]), DrawWindow, currentScreenState.ToString());
	}

	void DrawWindow(int id)
	{
		SendMessage("Draw" + currentScreenState.ToString(), SendMessageOptions.DontRequireReceiver);
	}

	void DrawStart()
	{
		if (GUILayout.Button("Create Server"))
			currentScreenState = ScreenState.Create;
		if (GUILayout.Button("Find Server"))
		{
			currentScreenState = ScreenState.Find;
			MasterServer.RequestHostList(gameTypeName);
		}
	}

	void DrawCreate()
	{
		serverNameField = GUILayout.TextField(serverNameField);
		if (GUILayout.Button("Create"))
		{
			Network.InitializeServer(32, port, true);
			MasterServer.RegisterHost(gameTypeName, serverNameField);
			serverNameField = serverDefaultName;

			currentScreenState = ScreenState.Setup;
		}

		if (GUILayout.Button("Back"))
			currentScreenState = ScreenState.Start;
	}

	void DrawFind()
	{
		serverScroll = GUILayout.BeginScrollView(serverScroll, GUI.skin.box);
		{
			HostData[] hosts = MasterServer.PollHostList();
			foreach(HostData host in hosts)
			{
				if (GUILayout.Button(host.gameName))
				{
					Network.Connect(host);
					currentScreenState = ScreenState.Setup;
				}
			}
		}
		GUILayout.EndScrollView();

		if (GUILayout.Button("Back"))
			currentScreenState = ScreenState.Start;
	}

	void DrawSetup()
	{
		GUILayout.FlexibleSpace();
		if (connected)
		{
			if (GUILayout.Button("Ready"))
			{
				currentScreenState = ScreenState.Ready;
				Application.LoadLevel(readyScene);
			}
		}
		else
		{
			GUILayout.Label("Connecting...");
		}

		if (GUILayout.Button("Cancel"))
		{
			connected = false;
			Network.Disconnect();
			currentScreenState = ScreenState.Start;
		}
	}
}

Unity overlay

I’m not much of a web developer, but over the last few weeks my work has made me learn a lot about jqury and javascript, so I figured I would put that into effect.

So, I made an overlay function in javascript and jquery that would create a frame for unity play in and append it to the content div for wordpress, so It would dynamically load content. This is a much better way for me to show off samples and demos then directing to a new page so I figured it was worth the effort. I included the code in the link below, Personally I just dropped this into the theme header so it would be loaded, and then use a link to call the javascript function like so.

<a href=”javaScript:UnityApp(
‘http://www.aWebPage.com/WebPlayer.unity3d’,
1000,600, ‘GameTitle’);”>LinkText</a>

 

<script type="text/javascript" src=
"http://webplayer.unity3d.com/download_webplayer-3.x/3.0/uo/UnityObject.js">
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"
type="text/javascript"></script>
<script type="text/javascript">

function UnityApp(unityApp, width, height, title) {
	$("#content").append("<div id='unityWindow'
style='background-color:#000000; position:absolute;
text-align:right; width:" + (width + 20) + "px; height:" +
(height + 40) + "px'><div id='title' style='color:white;
font-size:150%;'>" + title + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href='javascript:closeUnityApp()'>X</a></div><div id='unityPlayer'>
</div></div>");

$("html, body").animate({ scrollTop: 0 }, "fast");
	unityObject.embedUnity("unityPlayer", unityApp, width, height);

}

function closeUnityApp() {
	$("#unityWindow").remove();
}

</script>

EZSprite review

EZSprite is a really good package for those trying to cut through the fat and just make a 2d game.

Overall this package has a very easy entry point and a great sample that comes with it. It only takes a few moments to setup a character to animate. The animations work very similar to the normal animations GUI that models have.

Of all the assets and scripts in the package the most useful thing over all is the single sided mesh that is provided with the project. Its used in normally by the 2d sprites, but its very useful for whatever you need, like a platform that only has collision on the top.

 

The only issue in the system is the atlas generator, it uses a built in function unity has to compact a list of images into one image. This is completely random order however and results in an unusable image sheet.

 

 Measuring_a_Designers_Value

Really hits home on some good skills a designer should hold.

 

 

Almost moved in so I can start up tutorials again.  Things are always taking longer than I wish they would, but it’s been a wild ride.

Dungeon Blaster Sample

Hello everyone, Havent posted a prototype in awhile, this is a little thing I’ve been working on, this play style is becoming rather popular wanted to see how hard it was to replicate the control structure and enemies, found it pretty easy, produced this in a few hours.

 

DungeonBlaster

My end of tutorials soon.

Full internet will be restored to my new place soon.  Once that happens, the flood gates of vids should start.  Thank you again for all your patients.  It is appreciated!