Archive for June, 2012
Inheritable RPC Calls for Unity
Posted by whydoidoit in Programming, Project With Code on June 29, 2012
If you use RPC calls in Unity you sometimes want to be able to call an RPC defined on a base class and not overridden in a derived class. This isn’t normally possible and leads to some complicated workarounds, so I’ve written an extension that allows you do do it generically.
Having installed the package you will be able to do networkView.RPCEx(…) and such calls will be routed to base class members. The only requirements are:
- The game object that is to receive these calls needs to have an InheritableRPC script attached to it
- You cannot pass NetworkPlayer and NetworkViewID by these calls
You can however use this RPCEx method to pass parameters that are of any serializable class type – you are not limited to the usual list of simple parameters offered by Unity’s standard RPC methods.
using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using System.Linq; using Serialization; public class SomeBaseClass : MonoBehaviour { [RPC] protected void PrintThis(string text) { Debug.Log(text); } } [AddComponentMenu("Tests/Inherited")] public class Inherited : SomeBaseClass { }
Using normal RPC you couldn’t call PrintThis on something that had the Inherited script attached, with RPCEx you can
From Unity Script you would need to call the RPCEx like this:
InhertiableRPCExtensions.RPCEx(networkView, routineName, mode /* e.g. RPCMode.All */, parameter1 /* Any number of parameters */, parameter2);
New Demo of Unity Serializer – Add Save Game to Angry Bots in < 10 minutes!
Posted by whydoidoit in Uncategorized on June 24, 2012
The new video is available for Unity Serializer showing how amazingly fast it is to set up a full save game feature for your project!
Who Killed Who – Multicolor scrolling Unity Messages
Posted by whydoidoit in Programming, Project With Code on June 11, 2012
Hey here’s my script for multi-color scrolling Who Killed Who messages.
Installation
Download the package and import it. You are going to want to modify this file though, it contains examples.
In Use
Note the whole thing is driven off statics so you don’t need references you can just access the features through Messages.Message from your script.
Configuration
- Configure the screen location you want by changing Messages.Message.messageStartPosition either in the code or at runtime. Note: start positions will be rounded to a multiple of verticalSpacing on start up. If you move it, perhaps to account for screen resolution, make sure that it is a multiple of verticalSpacing or the results will not be pretty.
- Set the Y coordinate where fades will start irrespective of time. It’s Messages.Message.fadeY
- Set the spacing of messages. Messages.Message.verticalSpacing
- Choose your GUI skin/style for Label in the OnGUI call.
Displaying a Message
To display a message:
- Call Messages.Message.QueueMessage(“Your message”); from any script
- Messages support multiple color strings by using some custom formatting in the string:
- Either a plain string or red,green,blue:My string|red,green,blue:Another string
You can have any number of those combinations: an example is “1,0.4,0.5:Hello |1,1,1:World”
- You can also display a multi colour string from any OnGUI by using
Messages.Message.DrawGuiString(message, position)
Where position is a Vector2
I’ve included some examples to see it working…
.NET 2.0 Unity WeakTable – Store extended variables against anything
Posted by whydoidoit in Programming, Project With Code on June 11, 2012
Sometimes you need to store variables next to something that you don’t own! This happens quite a lot in Unity, especially when working with things like NetworkPlayer – you want to associate something with an instance, but you don’t control the lifetime of that instance. Or you want to associate something with a GameObject, but you don’t want to add a script.
In .NET 4 you get support for this stuff already, using a WeakTable – a kind of dictionary that has elements whose lifetime is controlled by the lifetime of the items they reference. .NET 2 doesn’t have this built in by default – but I’ve written a replacement.
This technique will work from Javascript and C#, but the code has to be in C# hence I’ve built it as a plugin unity package you can download from here.
You use it like this (clearly this is an example, I could just modify test!):
//Javascript //You can have any number of these type of classes class MyExtendedProperties { //Any number of members public var aProperty : String; } //This is a test class for this example //try to believe that it's an object that we've //got that we can't change the source of class test { //This is just so that there is something there public var test: String = "Hello"; } function Start () { //Create a new instance of test var o = new test(); //Give it an extra property in a new instance of MyExtendedProperties Extension.Get.<MyExtendedProperties>(o).aProperty = "World"; //Read it back Debug.Log(Extension.Get.(o).aProperty); //Next time the garbage collector runs our extended properties will be cleaned up //as nothing has a reference to o }
The C# is probably pretty obvious from that Javascript apart from the fact that the .Get method is an extension method callable in C# like this
anythingAtAll.Get<MyExtendedProperties>().aProperty = "cool huh";
So the basic idea it to have a dictionary, keyed off the object that contains another class that you can put extra information in. The problem of just doing this straight off is that the fact that the object is the key will keep the object alive forever – read massive memory leaks if this gets used too widely. The answer to that is to use weak references and get a notification when the garbage collector runs to enable you to clean up the extra instances associated with objects that have been killed.
PlayMaker support added to Unity Serializer
Posted by whydoidoit in Uncategorized on June 11, 2012
I’ve added support for PlayMaker Finite State Machines as an extension to the Unity Serializer project. You can download it from here.
100% Free Unity Serialization – Load & Save game
Posted by whydoidoit in Programming, Project With Code on June 5, 2012
UnitySerializer is a full level serialisation plug in. It enables you to save the progress of a game and restore it at some point in the future. It does this with minimal impact on the design of your game and requires minimal modification to your behaviours and classes.
UnitySerializer has been tested working on iOS and should work fine on Droid.
The serializer supports storing newly instantiated prefabs, as well as the status of existing scene objects (such as those that might be destroyed during the course of a game).
UnitySerializer supports the serialization of your custom classes and properties as well as standard Unity components
- Animation support
- Rigidbody support
- Support for NavMeshAgent (although the agent must recalculate its path on restoration)
- Transform – including parent/child relationships
- Important: UnitySerializer cannot store the current state of an iTween at present, due to the way in which iTween works. If you need path following you can use the code found elsewhere on this blog which will resume fine.
The status of your level is stored in a string which can be placed in PlayerPrefs – stored on a server or in a file.
UnitySerializer has its own page here.