Ejemplo n.º 1
0
        public static void AddView(ushort id, netvrkView view, GameObject go)
        {
            MonoBehaviour[] scripts = go.GetComponents <MonoBehaviour>();
            ObjData         data    = objList[id];

            data.netObj = view;

            foreach (MonoBehaviour script in scripts)
            {
                Type         type         = script.GetType();
                MethodInfo[] objectFields = type.GetMethods(BindingFlags.Instance | BindingFlags.Public);
                for (int i = 0; i < objectFields.Length; i++)
                {
                    netvrkRpc attribute = Attribute.GetCustomAttribute(objectFields[i], typeof(netvrkRpc)) as netvrkRpc;
                    if (attribute != null)
                    {
                        data.methods.Add(objectFields[i].Name);
                        data.scripts.Add(script);
                        data.rpcMethods.Add(objectFields[i]);
                    }
                }
                MethodInfo info = type.GetMethod("OnNetvrkReadSyncStream", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                if (info != null)
                {
                    data.syncMethod = info;
                    data.syncScript = script;
                }
            }
        }
Ejemplo n.º 2
0
        public static void RequestOwnership(netvrkView view)
        {
            object[] data  = { view.id };
            byte[]   bytes = netvrkSerialization.SerializeInternal((byte)InternalMethod.AskOwnership, data);

            SteamNetworking.SendP2PPacket(view.owner.SteamId, bytes, (uint)bytes.Length, EP2PSend.k_EP2PSendReliable, 0);
        }
Ejemplo n.º 3
0
        public static void Instantiate(string prefabName, Vector3 position, Quaternion rotation, int channel = 0, params object[] data)
        {
            if (!isConnected)
            {
                Debug.LogWarning("netVRk: Can not instantiate over the network when not connected!");
                return;
            }
            GameObject go   = (GameObject)Resources.Load(prefabName);
            netvrkView view = go.GetComponent <netvrkView>();

            if (view == null)
            {
                Debug.LogError("netVRk: Can not instantiate object '" + prefabName + "' because its missing a netvtkView component!");
                return;
            }

            object[] internalData = new object[data.Length + 3];
            internalData[0] = position;
            internalData[1] = rotation.eulerAngles;
            internalData[2] = prefabName;
            Array.Copy(data, 0, internalData, 3, data.Length);
            byte[] bytes = netvrkSerialization.SerializeInternal((byte)InternalMethod.InstantiatePrefab, internalData);

            for (int i = 0; i < playerList.Count; i++)
            {
                SteamNetworking.SendP2PPacket(playerList[i].SteamId, bytes, (uint)bytes.Length, EP2PSend.k_EP2PSendReliable, channel);
            }

            GameObject instanceGo = Instantiate(go, position, rotation);
            netvrkView netView    = instanceGo.GetComponent <netvrkView>();

            netView.owner           = localClient;
            netView.instantiateData = data;
        }
Ejemplo n.º 4
0
        private void OnGrab(object sender, ObjectInteractEventArgs e)
        {
            netvrkSyncedBase syncScript = e.target.GetComponent <netvrkSyncedBase>();

            if (syncScript != null)
            {
                netvrkView ioView = e.target.GetComponent <netvrkView>();
                ioView.RequestOwnership();
                syncScript.enabled = false;
                netView.Rpc("GrabRpc", netvrkTargets.Other, 0, true, ioView.id);
            }
        }
Ejemplo n.º 5
0
        private IEnumerator SetOwnership(InternalData internalData)
        {
            ushort       viewId  = (ushort)internalData.data[0];
            netvrkPlayer player  = IsInPlayerList(new CSteamID((ulong)internalData.data[1]));
            netvrkView   netView = objList[viewId].netObj;

            if (player.Equals(localClient))
            {
                netView.isMine = true;
                netView.owner  = localClient;
            }
            else
            {
                netView.owner = player;
            }
            yield return(null);
        }
Ejemplo n.º 6
0
        private IEnumerator InstantiatePrefab(InternalData internalData)
        {
            Vector3      position   = (Vector3)internalData.data[0];
            Quaternion   rotation   = Quaternion.Euler((Vector3)internalData.data[1]);
            string       prefabName = (string)internalData.data[2];
            netvrkPlayer owner      = IsInPlayerList(internalData.remoteId);

            GameObject go         = (GameObject)Resources.Load(prefabName);
            GameObject instanceGo = Instantiate(go, position, rotation);
            netvrkView netView    = instanceGo.GetComponent <netvrkView>();

            netView.isMine      = false;
            netView.isSceneView = false;
            netView.owner       = owner;
            int len = internalData.data.Length - 3;

            if (len > 0)
            {
                netView.instantiateData = new object[len];
                Array.Copy(internalData.data, 3, netView.instantiateData, 0, len);
            }
            yield return(null);
        }
Ejemplo n.º 7
0
 protected virtual void OnEnable()
 {
     netView = GetComponent <netvrkView>();
     StartCoroutine("SyncLoop");
 }