版权声明:转载请注明出处!不注明也无所谓,嘿嘿。 https://blog.csdn.net/qq_15020543/article/details/81674930
这次我们来实现地图以及玩家的创建。
首先,我们要先了解Network.Instantiate()。
网络实例化让你可以非常容易的在所有客户端实例化一个预制件。当一个玩家调用Network.Instantiate(),可以控制游戏对象上的任何输入命令。所有的安装和处理都被隐藏在实现中,因此你所需要做的只是在每个玩家所能控制的游戏对象上调用Network.Instantiate()使玩家能控制该对象。
可实例化预制件是Unity最重要的组成之一。Network Instantiate是对这个组成的扩展,让你可以通过使用一个函数在所有连接的机器上实例化一个游戏对象。(包括服务器)
Network.Instantiate()的参数如下:
static function Instantiate (prefab : Object, position : Vector3, rotation : Quaternion, group : int) : Object
如同实例化一样 ,头三个参数描述了被用于实例化的预制件和将放置预制件的位置和角度。组做为附加的参数也被作为参数传递。通常你只是默认传递0值。 组号码允许你过滤期望的网络信息并把它们组织在一起。
group也可以这样获取
NetworkPlayer player = Network.player;
group = int.Parse(player+"" );
其次我们要知道Network.Instantiate()与NetworkServer.Spawn()区别
在准备好的所有客户端上生成给定的游戏对象。
这将导致从注册的预制件或自定义的spawn函数实例化新对象。
总结一下,Network.Instantiate()是任何终端都可以调用的,而且不需要继承其他类。NetworkServer.Spawn()只能在服务器端调用,而且必须继承NetworkBehaviour,也就是说必须给游戏物体再添加一个 NetworkIdentity组件。再者如果挂载含有NetworkBehaviour的脚本的物体刚开始就在场景里,此游戏物体将失活。
好了,让我们进入正题。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class UMapCreater : MonoBehaviour {
//地图组件
//0.老家 1.墙 2.障碍 3.出生效果 4.河流 5.草 6.空气墙
public GameObject[] item;
public static List<GameObject> itemList = new List<GameObject>();
public List<GameObject> replaceList = new List<GameObject>();//用来储存钢铁家园
public static int _scene=1;
public Text sceneCount;
public GameObject Scene;
private void Update()
{
if (UManager.completed)
{
UManager.completed = false;
sceneCount.text = "Scene " + _scene.ToString();
Scene.SetActive(true);
Invoke("LoadComplate", 2.0f);
Invoke("CmdInitPlayer", 2.0f);
if (Network.isServer)
{
itemList.Clear();
Invoke("InitMap", 2.0f);
}
}
//if(UPlayerManager.Instance.vestigial<=0)
//{
// UPlayerManager.Instance.vestigial = 25;
// itemList.Clear();
// SceneManager.LoadScene(1);
// _scene++;
// sceneCount.text ="Scene "+ _scene.ToString();
//}
//IronHeart();
}
//void IronHeart()
//{
// if (UPlayerManager.Instance.isIronHeart==true)
// {
// for (int i = 0; i < 2; i++)
// {
// if (i == 1)
// for (int j = 0; j < 3; j++)
// {
// replaceList.Add(Instantiate(item[2], new Vector3(9+j, 1, 0), Quaternion.identity));
// }
// else if (i == 0)
// {
// replaceList.Add(Instantiate(item[2], new Vector3(9, 0, 0), Quaternion.identity));
// replaceList.Add(Instantiate(item[2], new Vector3(11, 0, 0), Quaternion.identity));
// }
// }
// HeartDel();
// for (int i = 0; i < replaceList.Count; i++)
// {
// Destroy(replaceList[i],5f);
// }
// Invoke("HeartLively", 5.0f);
// }
// UPlayerManager.Instance.isIronHeart = false;
//}
//private void HeartDel()
//{
// for (int i = 0; i < itemList.Count; i++)
// {
// if (itemList[i].transform.position == new Vector3(9, 1, 0) || itemList[i].transform.position == new Vector3(10, 1, 0) || itemList[i].transform.position == new Vector3(11, 1, 0)
// || itemList[i].transform.position == new Vector3(9, 0, 0) || itemList[i].transform.position == new Vector3(11, 1, 0))
// {
// itemList[i].GetComponent<BoxCollider2D>().enabled=false;
// }
// }
//}
//private void HeartLively()
//{
// for (int i = 0; i < itemList.Count; i++)
// {
// if (itemList[i].transform.position == new Vector3(9, 1, 0) || itemList[i].transform.position == new Vector3(10, 1, 0) || itemList[i].transform.position == new Vector3(11, 1, 0)
// || itemList[i].transform.position == new Vector3(9, 0, 0) || itemList[i].transform.position == new Vector3(11, 1, 0))
// {
// itemList[i].GetComponent<BoxCollider2D>().enabled = true;
// }
// }
//}
private GameObject CmdCreateItem(GameObject createGameObject,Vector3 createPosition,Quaternion createRotation)
{
GameObject itemGo =Network.Instantiate(createGameObject, createPosition, createRotation,UManager.group) as GameObject;
itemGo.transform.SetParent(gameObject.transform);
return itemGo;
}
//产生随机位置的方法
private Vector3 CreateRandomPosition()
{
//不生成x=0 20 y=0 16(场景边缘位置)
while(true)
{
Vector3 createPosition = new Vector3(Random.Range(1, 20), Random.Range(1, 16), 0);
//判定位置列表中是否有这个位置
int j = 0;
for(int i=0;i<itemList.Count;i++)
{
if (createPosition == itemList[i].transform.position)
j++;
}
if (j==0) return createPosition;
}
}
//产生敌人的方法
private void CreateEnemy()
{
int num = Random.Range(0, 3);
Vector3 EnemyPos = new Vector3();
if(num==0)
{
EnemyPos = new Vector3(0, 16, 0);
}
if (num == 1)
{
EnemyPos = new Vector3(10, 16, 0);
}
if (num == 2)
{
EnemyPos = new Vector3(20, 16, 0);
}
CmdCreateItem(item[3], EnemyPos, Quaternion.identity);
}
private void InitMap()
{
//实例化老家
CmdCreateItem(item[0], new Vector3(10, 0, 0), Quaternion.identity);
//用墙把老家围起来
for (int i = 0; i < 2; i++)
{
if (i == 1)
for (int j = 0; j < 3; j++)
{
itemList.Add(CmdCreateItem(item[1], new Vector3(10 + (j - 1) * 1, 1, 0), Quaternion.identity));
}
else if (i == 0)
{
itemList.Add(CmdCreateItem(item[1], new Vector3(9, 0, 0), Quaternion.identity));
itemList.Add(CmdCreateItem(item[1], new Vector3(11, 0, 0), Quaternion.identity));
}
}
//实例化外围墙
for (int j = 0; j < 23; j++)
{
CmdCreateItem(item[6], new Vector3(-1 + j * 1, -1, 0), Quaternion.identity);
}
for (int j = 0; j < 18; j++)
{
CmdCreateItem(item[6], new Vector3(21, j * 1, 0), Quaternion.identity);
}
for (int j = 0; j < 22; j++)
{
CmdCreateItem(item[6], new Vector3(j * 1, 17, 0), Quaternion.identity);
}
for (int j = 0; j < 18; j++)
{
CmdCreateItem(item[6], new Vector3(-1, j * 1, 0), Quaternion.identity);
}
//实例化地图
for (int i = 0; i <60; i++)
{
itemList.Add(CmdCreateItem(item[1], CreateRandomPosition(), Quaternion.identity));
}
for (int i = 0; i < 20; i++)
{
itemList.Add(CmdCreateItem(item[2], CreateRandomPosition(), Quaternion.identity));
}
for (int i = 0; i < 20; i++)
{
itemList.Add(CmdCreateItem(item[4], CreateRandomPosition(), Quaternion.identity));
}
for (int i = 0; i < 20; i++)
{
itemList.Add(CmdCreateItem(item[5], CreateRandomPosition(), Quaternion.identity));
}
InvokeRepeating("CreateEnemy", 5, 5);
}
private void CmdInitPlayer()
{
//初始化玩家,服务器端和客户端玩家位置不一样。
if(Network.isServer)
{
GameObject go = Network.Instantiate(item[3], new Vector3(8, 0, 0), Quaternion.identity, UManager.group) as GameObject;
}
if(Network.isClient)
{
GameObject go = Network.Instantiate(item[3], new Vector3(12, 0, 0), Quaternion.identity, UManager.group) as GameObject;
}
}
private void LoadComplate()
{
Scene.SetActive(false);
}
}
这是MapCreater的Inspector面板
这是Born1的Inspector面板(下面两个Creat Player请忽略- -)
然后是UBorn的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UBorn : MonoBehaviour {
public GameObject[] playerPrefab;
public GameObject[] enemyPrefabs;
// Use this for initialization
void Start () {
Invoke("BornTank", 0.8f);
Destroy(gameObject,0.8f);
}
private void BornTank()
{
if(transform.position==new Vector3(8,0,0))
{
GameObject go=Instantiate(playerPrefab[0], transform.position, Quaternion.identity);
}
else if (transform.position == new Vector3(12, 0, 0))
{
GameObject go= Instantiate(playerPrefab[1], transform.position, Quaternion.identity) ;
}
else
{
int num = Random.Range(0, 2);
GameObject go = Instantiate(enemyPrefabs[num], transform.position, Quaternion.identity);
}
}
}
我们来看看运行截图。