cancel
Showing results for 
Search instead for 
Did you mean: 

Small ReplicatedGameObject framework for multi-player Bullets

jsissler
Explorer

I teach an Intro to XR class and implemented a light framework for replicating multi-player objects such as Bullets. Would be interested in any feedback. Depends on Photon Pun.

Unity Package may be found here.

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class ReplicatedGameObject : MonoBehaviour
{
    private string owner;
    private float createtime;

    private void Awake()
    {
        owner = PhotonNetwork.NickName;
        createtime = Time.time;
    }

    protected virtual void Start()
    {
        getManager().Add(this);
    }

    public string UID
    {
        get { return owner + "-" + createtime.ToString(); }

        set
        {
            int split = value.IndexOf('-');
            owner = value.Substring(0, split);
            createtime = float.Parse(value.Substring(split + 1));
        }
    }

    public string Owner
    {
        get { return owner; }
    }

    public abstract ReplicatedGameObjectManager getManager();

    private void OnDestroy()
    {
        getManager().Remove(this);
    }
}

 

using Photon.Pun;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class ReplicatedGameObjectManager : MonoBehaviourPunCallbacks
{
    public ReplicatedGameObject prefab;
    private Dictionary<string, ReplicatedGameObject> cache = new Dictionary<string, ReplicatedGameObject>();

    public void Add(ReplicatedGameObject rgo)
    {
        bool cached = cache.ContainsKey(rgo.UID);
        Debug.Log("ReplicatedGameObjectManager.Add: adding " + rgo.UID + " cached=" + cached);
        if (!cached)
        {
            cache.Add(rgo.UID, rgo);
            photonView.RPC("RPC_Add", RpcTarget.Others, rgo.UID, rgo.gameObject.transform.position, rgo.gameObject.transform.forward);
        }
    }

    public void Remove(ReplicatedGameObject rgo)
    {
        bool cached = cache.ContainsKey(rgo.UID);
        Debug.Log("ReplicatedGameObjectManager.Remove: removing " + rgo.UID + " cached=" + cached);
        if (cached)
        {
            photonView.RPC("RPC_Remove", RpcTarget.Others, rgo.UID);
            cache.Remove(rgo.UID);
        }
    }

    [PunRPC]
    public void RPC_Add(string UID, Vector3 position, Vector3 forward)
    {
        bool cached = cache.ContainsKey(UID);
        Debug.Log("ReplicatedGameObjectManager.RPC_Add: UID=" + UID + " position=" + position + " forward=" + forward + " cached=" + cached);
        if (!cached)
        {
            ReplicatedGameObject rgo = Instantiate(prefab, position, Quaternion.identity) as ReplicatedGameObject;
            rgo.gameObject.transform.forward = forward;
            rgo.UID = UID;
            cache.Add(rgo.UID, rgo);
        }
    }

    [PunRPC]
    public void RPC_Remove(string UID)
    {
        bool cached = cache.ContainsKey(UID);
        Debug.Log("ReplicatedGameObjectManager.RPC_Remove: UID=" + UID + " cached=" + cached);
        if (cached)
        {
            ReplicatedGameObject rgo = cache[UID];
            cache.Remove(UID);
            Destroy(rgo.gameObject);
        }
    }

}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ReplicatedBulletManager : ReplicatedGameObjectManager
{
    public static ReplicatedBulletManager instance;

    private void Awake()
    {
        if (instance)
            throw new Exception("Attempt to create multiple instances of Singleton ReplicatedBulletManager");

        instance = this;
    }
}

 

0 REPLIES 0