一个比较好的多人网络游戏菜单程序。
看完大神们的博客,作为一个初学者,同时第一次写博客,仅抄袭些简单的东西供大家参考。
关于U3D开发,我想说的是DO与WHY的问题。个人觉得程序开发,初学者需要的实际的操作,从实际案例中总结经验是去DO,而不是一味的追寻WHY,等到技术时机成熟了,自然知道了WHY。如果一直在原地问为什么?WHY?最后还是没有出发。所以不要管是不是有语言基础,只要想学,从简单的案例下手不断积累经验查漏补缺,我相信会越走越远。
/*一个比较好的多人网络游戏菜单程序
有兴趣尝试多人网络编程的话,大家可以试试下面的菜单代码,感觉还是不错的一段代码,功能比较全面,如果能应用到自己的工程中的话,相信会得到很不错的效果,不过在用之前最好能仔细的读一读,并且根据自己的工程进行稍微的调整,希望能对大家提供有用的帮助。*/
var mousesensitivity :float= 10.0;
//var mySkin : GUISkin;
var hostServerPort:String;
var joinServerIP:String;
var playerName:String;
var isinoptions = 0;
var infocastTextShow;
var connectStatus:String;
var level:String="Level 2";
var levelPrefix:int=2;
var netwPlayer1Name:String;
var netwPlayer2Name:String="Not Connected";
function Awake(){
if (PlayerPrefs.HasKey("Player Name")){
playerName=PlayerPrefs.GetString("Player Name");
isinoptions=0;
}
else if(PlayerPrefs.HasKey("Player Name")==false)
isinoptions=10;
}
function OnGUI () {
//GUI.skin=mySkin;
if(isinoptions==10 ){
GUI.Box (Rect (13,10,240,120), "Input your name");
GUI.Label(Rect(30,80,300,120),"Name shouldnt be over 40 characters");
playerName =GUI.TextField (Rect (30, 60, 150, 20),playerName);
GUI.Label(Rect(30,100,300,120),"Name Length:"+playerName.length);
if (GUI.Button (Rect (25,160,50,20), "OK")) {
if(playerName.length>40)
playerName="I abuse big names";
isinoptions=0;
PlayerPrefs.SetString("Player Name" ,playerName);
}
}
if(isinoptions==0 ){
//MAIN MENU
GUI.Box (Rect (100,10,120,190), "Main Menu");
if (GUI.Button (Rect (105,40,110,20), "New Game")) {
isinoptions=3;
}
if (GUI.Button (Rect (105,70,110,20), "Online Coop")) {
isinoptions=2;
}
if (GUI.Button (Rect (105,100,110,20), "Options")) {
isinoptions=1;
}
if (GUI.Button (Rect (105,130,110,20), "Change Name")) {
isinoptions=10;
}
if(GUI.Button (Rect (105,160,110,20), "Quit")) {
Application.Quit();
}
}
if(isinoptions == 1){
//OPTIONS MENU
GUI.Box (Rect (13,10,180,120), "Options");
if (GUI.Button (Rect (25,80,150,30), "Decrease Quality Level")) {
QualitySettings.DecreaseLevel();
}
if (GUI.Button (Rect (25,40,150,30), "Increase Quality Level")) {
QualitySettings.IncreaseLevel();
}
if (GUI.Button (Rect (25,160,50,20), "Back")) {
isinoptions=0;
}
}
if(isinoptions ==3){
//OPTIONS MENU
GUI.Box (Rect (13,10,180,120), "New Game");
if (GUI.Button (Rect (25,80,150,30), "Play the tutorial")) {
Application.LoadLevel("LvL 1");
}
if (GUI.Button (Rect (25,40,150,30), "Skip the tutorial")) {
Application.LoadLevel("Level 2");
}
if (GUI.Button (Rect (25,160,50,20), "Back")) {
isinoptions=0;
}
}
if(isinoptions == 2){
//big deeeal
GUI.Box (Rect (13,10,180,120), "Online Coop Menu");
if (GUI.Button (Rect (25,40,150,30), "Host Server")) {
isinoptions=6;
Network.useNat=true;
Network.InitializeServer(2,27015);
}
if (GUI.Button (Rect (25,80,150,30), "Join Server")) {
isinoptions = 7;
}
if (GUI.Button (Rect (25,160,50,20), "Back")) {
isinoptions=0;
}
}
if(isinoptions == 5){
GUI.Box (Rect (13,10,600,120), "Lobby");
GUI.Label(Rect (20,30,300,120),"Player 2: "+ netwPlayer1Name+ " (host)");
GUI.Label(Rect (20,50,300,120),"Player 1: "+ playerName);
GUI.Label(Rect (20,70,300,120),"IP Address: "+Network.natFacilitatorIP);
GUI.Label(Rect (20,90,300,120),"Map to Load: "+ level);
if (Network.peerType == NetworkPeerType.Disconnected){
isinoptions=7;
connectStatus="Disconnected";
}
if (GUI.Button (Rect (25,160,100,20), "Disconnect")) {
networkView.RPC("PlayerTwoNameSending", RPCMode.All," ");
Network.Disconnect();
connectStatus=" ";
isinoptions=0;
}
}
if(isinoptions == 6){
GUI.Box (Rect (13,10,300,120), "Online Coop Menu");
GUI.Label(Rect (20,30,300,120),"Player 1: "+ netwPlayer1Name);
GUI.Label(Rect (20,50,300,120),"Player 2: "+ netwPlayer2Name);
GUI.Label(Rect (20,70,300,120),"Map to Load: "+ level);
GUI.Label(Rect (20,90,300,120),"IP Address: "+Network.natFacilitatorIP);
GUI.Box (Rect (320,10,180,120), "Maps");
if (GUI.Button (Rect (335,50,100,20), "Example Level")) {
networkView.RPC("SendLevelName", RPCMode.OthersBuffered,"Example Level");
levelPrefix=1;
level="Example Level";
}
if (GUI.Button (Rect (335,70,100,20), "Level 1")) {
levelPrefix=2;
level="Level 1 Networked";
networkView.RPC("SendLevelName", RPCMode.OthersBuffered,"Level 1");
}
if (GUI.Button (Rect (30,180,140,20), "Start")) {
LoadLevelOf();
}
if (GUI.Button (Rect (30,200,140,20), "Shut Down Server")) {
Network.Disconnect();
connectStatus=" ";
isinoptions=0;
}
}
if(isinoptions == 7){
GUI.Label(Rect(25,130,300,300),connectStatus);
GUI.Box (Rect (13,10,180,120), "Join Server");
joinServerIP = GUI.TextField (Rect (30, 30, 100, 20), joinServerIP);
GUI.Label(Rect(30,50,150,120),"Type the ip of the server you want to connect here");
if (GUI.Button (Rect (25,100,100,20), "Connect")) {
connectStatus="Connecting";
Network.useNat=false;
Network.Connect(joinServerIP,27015);
Network.useNat=true;
}
//parseInt (myString)
if (GUI.Button (Rect (25,160,50,20), "Back")) {
connectStatus=" ";
isinoptions=0;
}
}
}
function Update (){
if (Input.GetKeyDown(KeyCode.P))
Application.CaptureScreenshot("Screenshot.png");
//if(PlayerPrefs.GetInt("funkach")==1337)
// funkTextTimer=0;
}
function OnFailedToConnect(error: NetworkConnectionError)
{
connectStatus="Could not connect to server: "+ error;
Debug.Log("Could not connect to server: "+ error);
}
function OnConnectedToServer(){
connectStatus="Connected , Wait for the host to select a map and start the game";
networkView.RPC("PlayerTwoNameSending", RPCMode.AllBuffered,playerName);
isinoptions=5;
}
@RPC
function LoadLevelOf(){
if (Network.isServer){
networkView.RPC("LoadLevel", RPCMode.All, level,levelPrefix);
}
}
@RPC
function LoadLevel (levelInt :String, levelPrefix :int)
{
lastLevelPrefix = levelPrefix;
// There is no reason to send any more data over the network on the default channel,
// because we are about to load the level, thus all those objects will get deleted anyway
//DebugConsole.LogMessage("loading a new level, sending disabled");
Network.SetSendingEnabled(0, false);
// We need to stop receiving because first the level must be loaded first.
// Once the level is loaded, rpc's and other state update attached to objects in the level are allowed to fire
Network.isMessageQueueRunning = false;
// All network views loaded from a level will get a prefix into their NetworkViewID.
// This will prevent old updates from clients leaking into a newly created scene.
Network.SetLevelPrefix(levelPrefix);
Application.LoadLevel(levelInt);
//yield;
//yield;
// Allow receiving data again
//Network.isMessageQueueRunning = true;
// Now the level has been loaded and we can start sending out data to clients
//Network.SetSendingEnabled(0, true);
//Debug.Log("Sending enabled again");
//for (var go in FindObjectsOfType(GameObject))
//go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
function OnLevelWasLoaded () {
// Allow receiving data again
Network.isMessageQueueRunning = true;
// Now the level has been loaded and we can start sending out data to clients
Network.SetSendingEnabled(0, true);
//DebugConsole.LogMessage("Level finished loading. Sending enabled again");
//for (var go in FindObjectsOfType(GameObject))
//go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
@RPC
function PlayerOneNameSending(name1:String){
netwPlayer1Name=name1;
//netwPlayer2Name=name2;
}
@RPC
function PlayerTwoNameSending(name2:String){
//netwPlayer1Name=name1;
netwPlayer2Name=name2;
}
function OnServerInitialized(){
Network.useNat = !Network.HavePublicAddress();
networkView.RPC("PlayerOneNameSending", RPCMode.AllBuffered, playerName);
}
@RPC
function SendLevelName(levelName:String){
level=levelName;
}

浙公网安备 33010602011771号