评分次数31
感谢分享^_^
威锋有你更给力:)
感谢分享^_^
支持!继续更新!
威锋有你更精彩:)
感谢分享^_^
威锋有你更精彩:)
威锋有你更给力:)
威锋有你更给力:)
威锋有你更精彩:)
这贴猛啊&&有说蓝牙的游戏&&支持了!!
感谢分享^_^
威锋有你更精彩:)
感谢分享^_^
终于下到地牢猎人、真实赛车了,谢谢~!
good 扣分的s13
扣分的想什么呢啊??换做你费心发个帖子没人回。你心里舒服啊?
还没看,先加分
大过年的 不要什么死不死的~!!!
跑跑挺好,就是后来的版本都不能联机了
威锋有你更精彩:)
威锋有你更精彩:)
注册时间 最后登录
在线时间149 小时 UID
主题帖子人气
老师问我长大想做什么
提示: 作者被禁止或删除 内容自动屏蔽
签名被屏蔽
注册时间 最后登录
在线时间76 小时 UID
主题帖子人气
Sonic & SEGA All-Stars Racing
索尼克世嘉全明星赛车
注册时间 最后登录
在线时间242 小时 UID
主题帖子人气
对幻影残痕于 19:24在楼主发表的主题评分:人气:+1;
看很多人在找,自己写一个,游戏本人没有全玩过,
可能仅仅是复制粘贴,其中的错误欢迎大家指正,更欢迎大家补充,我会把推荐者加到这个帖子里的
大家联机前游戏版本要相同哦
一蓝牙和wifi联机均可用1四国军棋
.......拳皇
注册时间 最后登录
在线时间353 小时 UID
主题帖子人气
对幻影残痕于 19:25在楼主发表的主题评分:人气:+1;
看很多人在找,自己写一个,游戏本人没有全玩过,
可能仅仅是复制粘贴,其中的错误欢迎大家指正,更欢迎大家补充,我会把推荐者加到这个帖子里的
大家联机前游戏版本要相同哦
一蓝牙和wifi联机均可用1四国军棋
.......先顶再看
注册时间 最后登录
在线时间552 小时 UID
主题帖子人气
提示: 作者被禁止或删除 内容自动屏蔽
注册时间 最后登录
在线时间136 小时 UID
主题帖子人气
注册时间 最后登录
在线时间176 小时 UID
主题帖子人气
这个绝对要顶,LZ快点放出地址和图片
注册时间 最后登录
在线时间7230 小时 UID
主题帖子人气
加油 王建民
对幻影残痕于 22:26在楼主发表的主题评分:人气:+1;
看很多人在找,自己写一个,游戏本人没有全玩过,
可能仅仅是复制粘贴,其中的错误欢迎大家指正,更欢迎大家补充,我会把推荐者加到这个帖子里的
大家联机前游戏版本要相同哦
一蓝牙和wifi联机均可用1四国军棋
.......威锋有你更精彩:)
注册时间 最后登录
在线时间3301 小时 UID
主题帖子人气
正义的闷骚客
对幻影残痕于 22:51在楼主发表的主题评分:人气:+10;
看很多人在找,自己写一个,游戏本人没有全玩过,
可能仅仅是复制粘贴,其中的错误欢迎大家指正,更欢迎大家补充,我会把推荐者加到这个帖子里的
大家联机前游戏版本要相同哦
一蓝牙和wifi联机均可用1四国军棋
.......跑跑挺好,就是后来的版本都不能联机了
威锋旗下产品
Hi~我是威威!
沪公网安备 29号 | 沪ICP备号-1
新三板上市公司威锋科技(836555)
增值电信业务经营许可证:
Powered by Discuz!Unity3d使用蓝牙(bluetooth)开发IOS点对点网络游戏
Unity3d使用蓝牙(bluetooth)开发IOS点对点网络游戏
发布时间: 7:20:43
编辑:www.fx114.net
本篇文章主要介绍了"Unity3d使用蓝牙(bluetooth)开发IOS点对点网络游戏 ",主要涉及到Unity3d使用蓝牙(bluetooth)开发IOS点对点网络游戏 方面的内容,对于Unity3d使用蓝牙(bluetooth)开发IOS点对点网络游戏 感兴趣的同学可以参考一下。
著作权声明:本文由 原创,欢迎转载分享。转载时请保留该声明和作者博客链接,谢谢!
最近使用Unity3d制作的IOS游戏需要加入联网对战功能功能,因此使用ObjC语言利用IOS SDK的GameKit.framework的Peer-to-peer Connectivity实现了网络连接,在此分享。
啥话都不说,先上代码。,
类NetWorkP2P,继承自NSObject。提供GKSessionDelegate和GKPeerPickerControllerDelegate的实现。并且利用单例模式实现一个类方法+( NetWorkP2P *) sharedNetWorkP2P;此方法返回一个NetWorkP2P的实例。
NetWorkP2P.h//
P2PTapWar////
Created by
on 11-9-14.//
Copyright 2011年 __MyCompanyName__. All rights reserved.//#import &Foundation/Foundation.himport &GameKit/GameKit.hdefine AMIPHD_P2P_SESSION_ID @"amiphd-p2p"#define START_GAME_KEY @"startgame"#define TIME_KEY @"time"#define END_GAME_KEY @"endgame"#define TAP_COUNT_KEY @"taps"#define TIMES_KEY @"times"@interface NetWorkP2P : NSObject&GKSessionDelegate,GKPeerPickerControllerDelegate&{
UInt32 playerS
UInt32 opponentS
UInt32 playerT
UInt32 opponentT
NSString *opponentID;
BOOL actingAsH
GKSession *gkS }+( NetWorkP2P *) sharedNetWorkP2P;-(void)showPeerPickerC-(void)addTheScore: (UInt32)-(void)addT-(UInt32)getOpponentS-(UInt32)getPlayerS-(UInt32)getPlayerT-(UInt32)getOpponentT-(NSString *)getOpponentID;@end
类NetWorkP2P的实现文件,fileName:NetWorkP2P.m
NetWorkP2P.m//
P2PTapWar////
Created by
on 11-9-14.//
Copyright 2011年 __MyCompanyName__. All rights reserved.//#import "NetWorkP2P.h"@implementation NetWorkP2P+(NetWorkP2P *) sharedNetWorkP2P{
static NetWorkP2P *sharedNetWorkO
if(!sharedNetWorkObject)
sharedNetWorkObject=[[NetWorkP2P alloc] init];
return sharedNetWorkO}- (id)init{
self = [super init];
if (self) {
// Initialization code here.
return}-(UInt32)getPlayerScore{
return playerS}-(UInt32)getOpponentScore{
return opponentS}-(UInt32)getOpponentTimes{
return opponentT}-(UInt32)getPlayerTimes{
return playerT}-(NSString *)getOpponentID{
return opponentID;}-(void) showPeerPickerController{
if(!opponentID)
actingAsHost=YES;
GKPeerPickerController *peerPickerContrller=[[GKPeerPickerController alloc] init];
peerPickerContrller.delegate=
peerPickerContrller.connectionTypesMask=GKPeerPickerConnectionTypeN
[peerPickerContrller show];
}}-(void)addTheScore:(UInt32)score{
playerScore+=
NSMutableData *message=[[NSMutableData alloc]init];
NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:message];
[archiver encodeInt:playerScore forKey:TAP_COUNT_KEY];
[archiver finishEncoding];
GKSendDataMode sendMode=GKSendDataU
[gkSession sendDataToAllPeers:message withDataMode:sendMode error:NULL];
[archiver release];
[message release];
}-(void)addTimes{
playerTimes++;
NSMutableData *message=[[NSMutableData alloc]init];
NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:message];
[archiver encodeInt:playerTimes forKey:TIMES_KEY];
[archiver finishEncoding];
GKSendDataMode sendMode=GKSendDataU
[gkSession sendDataToAllPeers:message withDataMode:sendMode error:NULL];
[archiver release];
[message release];}#pragma mark game logic-(void) initGame{
playerScore=0;
opponentScore=0;}-(void) hostGame{
[self initGame];
NSMutableData *message=[[NSMutableData alloc] init];
NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:message];
[archiver encodeBool:YES forKey:START_GAME_KEY];
[archiver finishEncoding];
NSError *sendErr=
[gkSession sendDataToAllPeers:message withDataMode:GKSendDataReliable error:&sendErr];
if (sendErr) {
NSLog(@"send greeting failed : %@",sendErr);
[message release];
[archiver release];}-(void) joinGame{
[self initGame];}-(void) showEndGameAlert{}-(void) endGame{
opponentID=
[gkSession disconnectFromAllPeers];
[self showEndGameAlert];}#pragma mark GKPeerPickerControllerDelegate methods-(GKSession *) peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type{
if(!gkSession)
gkSession=[[GKSession alloc] initWithSessionID:AMIPHD_P2P_SESSION_ID displayName:nil sessionMode:GKSessionModePeer];
gkSession.delegate=
return gkS}-(void) peerPickerController:(GKPeerPickerController *) picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{
NSLog ( @"connected to peer %@", peerID);
[session retain];
// TODO: who releases this?
[picker dismiss];
[picker release];}- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker {
NSLog ( @"peer picker cancelled");
[picker release];}#pragma mark GKSessionDelegate methods//START:code.P2PTapWarViewController.peerdidchangestate- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
switch (state)
case GKPeerStateConnected:
[session setDataReceiveHandler: self withContext: nil];
opponentID = peerID;
actingAsHost ? [self hostGame] : [self joinGame];
} }//END:code.P2PTapWarViewController.peerdidchangestate//START:code.P2PTapWarViewController.didreceiveconnectionrequestfrompeer- (void)session:(GKSession *)sessiondidReceiveConnectionRequestFromPeer:(NSString *)peerID {
actingAsHost = NO;}//END:code.P2PTapWarViewController.didreceiveconnectionrequestfrompeer- (void)session:(GKSession *)session connectionWithPeerFailed:(NSString *)peerID withError:(NSError *)error {
NSLog (@"session:connectionWithPeerFailed:withError:");
}- (void)session:(GKSession *)session didFailWithError:(NSError *)error {
NSLog (@"session:didFailWithError:");
}#pragma mark receive data from session-(void) receiveData: (NSData *) data fromPeer : (NSString *) peerID inSession: (GKSession *) session context:(void *) context{
NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:data];
if([unarchiver containsValueForKey:TAP_COUNT_KEY])
opponentScore=[unarchiver decodeIntForKey:TAP_COUNT_KEY];
if([unarchiver containsValueForKey:TIMES_KEY])
opponentTimes=[unarchiver decodeIntForKey:TIMES_KEY];
if([unarchiver containsValueForKey:END_GAME_KEY])
[self endGame];
if([unarchiver containsValueForKey:START_GAME_KEY])
[self joinGame];
[unarchiver release];}@end
fileName:NetWorkP2PBinding.m在此文件中实现C语言接口,以方便在UnityScript中调用。
//NetWorkP2PBinding.mm
#import "NetWorkP2P.h"extern "C"{
void _showPeerPicker()
[[NetWorkP2P sharedNetWorkP2P] showPeerPickerController];
NSLog(@"call the mothed _showPeerPicker");
const char* _getName()
return [@"fyn" UTF8String];
int _getOpponentScore()
return [[NetWorkP2P sharedNetWorkP2P]getOpponentScore];
int _getPlayerScore()
return [[NetWorkP2P sharedNetWorkP2P]getPlayerScore];
void _addTheScore( UInt32 score)
[[NetWorkP2P sharedNetWorkP2P] addTheScore:score];
void _addTheTimes()
[[NetWorkP2P sharedNetWorkP2P] addTimes];
int _getOpponentTimes()
return [[NetWorkP2P sharedNetWorkP2P] getOpponentTimes];
int _getPlayerTimes()
return [[NetWorkP2P sharedNetWorkP2P] getPlayerTimes];
const char * _getOpponentID()
return [[[NetWorkP2P sharedNetWorkP2P ]getOpponentID] UTF8String];
FileName:P2PBinding.cs 这是在Unity3d中C#脚本,在这里面调用NetWorkP2PBinding.mm实现的C语言方法。
//P2PBinding.csusing UnityEusing Susing System.Cusing System.Collections.Gusing System.Runtime.InteropSpublic class P2PBinding{
[DllImport("__Internal")]
private static extern void _showPeerPicker();
public static void showPeerPicker()
if(Application.platform==RuntimePlatform.IPhonePlayer)
_showPeerPicker();
[DllImport("__Internal")]
private static extern int _getOpponentScore();
[DllImport("__Internal")]
private static extern int _getOpponentTimes();
[DllImport("__Internal")]
private static extern int _getPlayerScore();
[DllImport("__Internal")]
private static extern int _getPlayerTimes();
[DllImport("__Internal")]
private static extern void _addTheTimes();
[DllImport("__Internal")]
private static extern void _addTheScore(int score);
public static int getOpponentScore()
if(Application.platform==RuntimePlatform.IPhonePlayer)
return _getOpponentScore();
return -1;
public static int getOpponentTimes()
if(Application.platform==RuntimePlatform.IPhonePlayer)
return _getOpponentTimes();
return -1;
public static int getPlayerScore()
if(Application.platform==RuntimePlatform.IPhonePlayer)
return _getPlayerScore();
return -1;
public static int getPlayerTimes()
if(Application.platform==RuntimePlatform.IPhonePlayer)
return _getPlayerTimes();
return -1;
public static void addTheScore(int score)
if(Application.platform==RuntimePlatform.IPhonePlayer)
_addTheScore(score);
public static void addTheTimes()
_addTheTimes();
FileName:GameDate.cs 此文件提供游戏的数据,并保持和NetWorkP2P文件中数据同步,将此文件附加到一个GameObject上。
using UnityEusing System.Cpublic class GameDate : MonoBehaviour {
public string opponentID;
public int playerT
public int opponentT
public int playerS
public int opponentS
// Use this for initialization
void Start () {
P2PBinding.showPeerPicker();
// Update is called once per frame
void Update () {
opponentTimes=P2PBinding.getOpponentTimes();
opponentScore=P2PBinding.getOpponentScore();
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!
二、互相尊重,对自己的言论和行为负责。
本文标题:
本页链接: