Skip to content

Instantly share code, notes, and snippets.

@AngryAnt
Created May 28, 2013 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngryAnt/5661770 to your computer and use it in GitHub Desktop.
Save AngryAnt/5661770 to your computer and use it in GitHub Desktop.
Example of a server-side managed pickup base behaviour for Unity networking.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Collider))]
[RequireComponent (typeof (NetworkView))]
public abstract class Pickup : MonoBehaviour
{
bool available;
protected bool Available
{
get
{
return available;
}
set
{
if (value == available)
{
return;
}
networkView.RPC ("SetAvailable", RPCMode.Others, value);
SetAvailable (value);
}
}
[RPC]
void SetAvailable (bool value)
{
available = value;
OnFlipAvailable ();
}
[RPC]
void GetState (NetworkMessageInfo messageInfo)
{
networkView.RPC ("SetAvailable", messageInfo.sender, available);
}
protected abstract bool OnPickup (Player player);
protected virtual void OnFlipAvailable ()
{}
void Reset ()
{
networkView.stateSynchronization = NetworkStateSynchronization.None;
networkView.observed = this;
collider.isTrigger = true;
}
void Start ()
{
if (!networkView.isMine)
{
networkView.RPC ("GetState", networkView.owner);
}
}
void OnTriggerEnter (Collider other)
{
if (!available || !networkView.isMine)
{
return;
}
Player player = other.transform.root.GetComponentInChildren<Player> ();
if (player != null && OnPickup (player))
{
Available = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment