예제 #1
0
        public void Destroy(GameObject instance)
        {
            if (!instance) //如果没有游戏物体
            {
                return;
            }
            NetworkIdentity identity = instance.GetComponent <NetworkIdentity>();

            if (!identity || !identity.Active) //如果没有该组件或者不活跃
            {
                return;
            }
            //只允许删除自己的游戏物体
            if (!selfIntances.ContainsKey(identity.Id))
            {
                return;
            }

            //从列表中删除
            instances[identity.TypeId].Remove(identity.Id);
            //从场景中删除
            global::Destroy.Object.Destroy(instance);
            //从自己物体中删除
            selfIntances.Remove(identity.Id);

            C2S_Destroy cmd = new C2S_Destroy();

            cmd.Frame  = frame;
            cmd.TypeId = identity.TypeId;
            cmd.Id     = identity.Id;
            //发送指令
            Send((ushort)NetworkRole.Client, (ushort)GameCmd.Destroy, cmd);
        }
예제 #2
0
        private void OnDestroy(Socket socket, byte[] data)
        {
            C2S_Destroy clientCmd = NetworkSerializer.Deserialize <C2S_Destroy>(data);

            //移除对象数据
            instances[clientCmd.TypeId].Remove(clientCmd.Id);

            S2C_Destroy cmd = new S2C_Destroy();

            cmd.Frame  = clientCmd.Frame;
            cmd.TypeId = clientCmd.TypeId;
            cmd.Id     = clientCmd.Id;
            //广播除了发送者之外的人
            foreach (var client in clients.Keys)
            {
                if (client == socket)
                {
                    continue;
                }
                Send(client, (ushort)NetworkRole.Server, (ushort)GameCmd.Destroy, cmd);
            }
        }