Archive for category Programming

Inheritable RPC Calls for Unity



Download

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);

, ,

Leave a comment

Who Killed Who – Multicolor scrolling Unity Messages


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…

, , ,

Leave a comment

.NET 2.0 Unity WeakTable – Store extended variables against anything


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.

, , ,

Leave a comment

100% Free Unity Serialization – Load & Save game


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.
In addition, presuming you use my extended coroutine start functions, all of your coroutines will continue executing from the point that they left off and will be restored with all local variables intact. You need make no changes to your coroutines – just to the way that you start them.

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.

, , , , ,

Leave a comment

Unity curved path following with easing at constant speed



Download

The Problem

I have a character than needs to follow paths curved through a series of control points as it moves around the world. I also want to ease in and out to speed of movement as the path starts and ends. Sometimes in the middle of a path follow I need to change my mind and have some other path taken or perhaps change from path following to another state. This isn’t possible with iTween at constant speed unless you use PutOnPath, which doesn’t allow easing functions. I would also like to be able to move at a constant speed – either by ignoring the length of individual path elements (and thereby specifying at time fraction for the whole path) or by having a maximum speed my object can travel at.

Solution

Building on the great work done by Andeeee and Renaud, I’ve made a path following class that enables you to specify from 2 to N points and have it build a curved path (if it has more than 2 points!), you can then follow that path using a time from 0…1 and also apply an easing function to make the start and the end points smooth.

It supports Linear, Sine, Quadratic, Cubic, Quintic and Quartic easing functions with control on over whether the path is eased in and out separately.


Line 1 – EaseIn, Line 2 – EaseOut, Line 3 – EaseIn and EaseOut

This allows fine grained control of an eased, spline path in an Update function that is not possible using iTween – this enables you to abort a movement halfway through if something more important is happening .

To use it you download and import the unity package.

Eased spline paths

To use no easing call Spline.Interp(arrayOfPoints, time). Where time is a float between 0 and 1.

The array of points can be an array of Vector3s, GameObjects or Transforms which are implicitly converted to a Spline.Path instance containing an array of Vector3s. DON’T BOTHER to create your own Spline.Path instances!

e.g. transform.position = Spline.Interp(myPathObject.GetComponentsInChildren<Transform>(), time);

This would use the myPathObject and all of its children to define the path. (For example only, don’t go calling GetComponentsInChildren every frame).

To ease the function you can supply one or more of the easing parameters:

Spline.Interp(arrayOfPoints, time, easingType /* e.g. EasingType.Sine */, easeIn /* e.g. true (default) */, easeOut /*e.g. false (default true) */ );

The Spline class also supports Andeeee’s Velocity and GizmoDraw functions to help you with debugging.

PLEASE NOTE: the function uses a different algorithm for short paths so that you can get a number of different effects. If your path has 2 points it does a linear interpolation, 3 points does a Quadratic spline and 4 points does a Cubic spline. The 5 or more point version uses Catmul Rom splines and ensures that the path passes through every point.

You can double up start and end points easily with the Spline.Wrap() call on your path array – you may want to do this for short paths if you want to ensure that all of the points are passed through – or just do it yourself. There should be no effect if you are using InterpConstantSpeed or MoveOnPath when you do this (See below).

Constant Speed

I’ve added an InterpConstantSpeed function too, this tries to make the speed of movement constant between sections. It works in the same way as Interp, with a time value between 0 and 1.

Now just a note on that.  If your path points are moving then you need to watch constant speed interpolations as one section getting much longer or shorter could cause the position to vary wildly.  If your path sections are increasing or decreasing in size then you are better off using Interp and trying to make sure that each path section is roughly the same size (short sections will appear to have lower velocity than large sections, so they need to be “kind of” the same size).  Interp says if your path has 5 sections, each one takes 1/5 of the time to cross.  If you are in section 2 and section 3 gets bigger, you will complete section 2 in the normal time.  Using constant speed, the position at time “t” is along the magnitude of the whole path, if it gets longer or shorter then the position at “t” will change between sections moving the Vector unrealistically forwards or backwards. This is a problem if you are in an early stage of the path and a later path point, far off screen, is moving.  The Vector will stutter for apparently no reason.  If you want constant speed then you need to ensure that your path points maintain exactly the same distance from each other when they move (they need to move in an arc centered on the previous end point).

The second point is that applying “Constant Speed” and “Easing” appears to create a paradox; but InterpConstantSpeed accepts easing functions! Well the “Constant” bit refers to the sizes of path sections being converted to a path magnitude and the easing is applied to the “time” fed into the path calculator: so you can have your cake and eat it 🙂

Path Movement and Character Speed

I’ve also added a speed limited version, this also solves the constant speed issue by having a maximum possible movement speed, you can therefore move those path elements as often as you like – but you can’t specify the time it takes to complete the path. It works kind of like a SmoothDamp – here’s how you go about it:

public class TestFollow : MonoBehaviour {

	public Transform[] path;

	float t = 0;

	// Update is called once per frame
	void Update () {
		transform.position = Spline.MoveOnPath(path, transform.position, ref t, 0.5f);

	}
}

Basically you pass the path, the current position and a reference to a float that will contain the currently targeted path point, the final parameter shown here is the number of world units per second to move.

This routine also has the added benefit that it will move to the start of the path before following it.

Here are all of the parameters to MoveOnPath:

Parameter Meaning
pts An array of transforms, Vector3s or GameObjects that describe the path
currentPosition The current position of the object being moved
pathPosition The position along the path, this is updated by the call so must be a variable of the class
(Optional)rotation A quaternion that will be updated to show the rotation that should be used. This can change quickly for slow moving objects, so you might want to smooth it. See below.
maxSpeed The maximum number of world units to move in one second, default is 1
smoothnessFactor The smoothing factor is the number of steps on the path that will be used, default is 100
ease The type from EasingType.Linear, EasingType.Sine, EasingType.Cubic, EasingType.Quadratic, EasingType.Quartic and EasingType.Quintic. Default is Linear
easeIn true if you want to ease in the function or false if you don’t. Default is true
easeOut true if you want to ease out the function or false if you don’t. Default is true

Here’s an example of the version that returns a rotation, in this example I use the SmoothQuaternion from a previous post to smooth out the rotations.

#pragma strict

var pathPoints : Transform[];

var t : float;
var sr : SmoothQuaternion;

function Start() {
	sr = transform.rotation;
	sr.Duration = 0.5f;
}

function Update () {
	var q : Quaternion;
	transform.position = Spline.MoveOnPath(pathPoints, transform.position, t, q, 0.5f, 100, EasingType.Sine, true, true);
	sr.Value = q;
	transform.rotation = sr;

}

Javascript

You can use the Spline class from JavaScript, as long as the .cs files are somewhere inside a Assets/Plugins folder (the download does this) and that JavaScript is not inside a Plugins folder.

This example will follow a constant speed path for 20 seconds.

#pragma strict

var pathPoints : Transform[];

var t : float;

function Update () {
	transform.position = Spline.InterpConstantSpeed(pathPoints, t, EasingType.Sine, true, true);
	t += Time.deltaTime/20;
}

The next one moves an item on a path at a maximum of 0.5 world units per second

#pragma strict

var pathPoints : Transform[];

var t : float;

function Update () {
	transform.position = Spline.MoveOnPath(pathPoints, transform.position, t, 0.5f, 100, EasingType.Sine, true, true);

}

, , , , , , ,

37 Comments

Serializing classes between Silverlight and .NET 4


I’ve realized that I didn’t detail how to ensure you can serialize your own classes between Silverlight and .NET 4 using my SilverlightSerializer class. 

The vital thing to do in these circumstances is to define the classes you want to share in a Silverlight assembly that only references System, System.Core and mscorlib.  These are the requirements for assemblies that can be used in both types of project.  If you define your classes in this way then they can be deserialized on the back end without a problem, reference anything else and it won’t work.  Fortunately most of what you will need is included in those system assemblies!

, ,

1 Comment

Element is already the child of another element – Arghhhhhh


Sometimes I just hate Silverlight. Spent 5 hours debugging a problem that resulted in the “Element is already the child of another element” InvalidOperation exception. I looked at everything; looked to see if I was accidentally reparenting something: no. Looked for unexpected recursion: no. Played with every debugger setting to get more information and finally disected the application – all to no avail.

Then I noticed I’d commented out an Image in one of my templates (derived from a Telerik template for tree nodes) and happened to notice that it was the target of a Storyboard. Yep that was it – not an error that reads “You’ve targeted an element that doesn’t exist” oh no. “Element is already the child of another element” – like hell it was 🙂

So word to the wise, if you don’t think you are actually making an element the child of another element, look for any inconsistencies in your storyboards and animations.

Help is at hand:

Since writing this post I’ve come up with an algorithm to help identify XAML markup that causes this exception. The post here has a link to the project and a description of the approach.

, , ,

5 Comments

Checking in at 30,000ft


Well I can confirm that GoGoInflight, VPN and TFS are working fine together – just did my first code check in while flying!  Unfortunately I am flying to Chicago rather than England thanks to the Icelandic Volcano… Oh well, while my plans have changed I think I’ve got it lucky compared to many.

Just been looking through the performance of my Silverlight app – drop shadows are still a real killer, they appear to spend an inordinate amount of time rendering the drop shadows of text that will be obscured completely by the rectangle on which it sits.  I’ve ended up doing a lot of fake shadows and written a component that does this using embedded borders with semi transparent fill.  It looks good and the performance is much better.

I’ve also spent a while looking at my flip control – the performance was getting very poor when the sides were full of complex objects – guess there’s no surprise there – so I’ve modified it to take an image of both sides during the flip and it’s back to smooth as silk no matter what I’m turning over.

I’ll extract the code for those two components into a separate project and post up here shortly, in case anyone needs that kind of functionality.

, , , , ,

2 Comments