【UNET自学日志】Part 13 僵尸的ID

我们给僵尸设置ID的思路和在Part7讲的一样,老样子直接上代码

新建脚本Zombie_ID

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class Zombie_ID : NetworkBehaviour {

    [SyncVar]public string zombieID;
    private Transform myTransform;

	void Start () 
    {
        myTransform = transform;	
	}
	
	void Update () 
    {
        SetIdentity();
	}

    void SetIdentity()
    {
        if (myTransform.name == "" || myTransform.name == "Zombie(Clone)")
        {
            myTransform.name = zombieID;
        }
    }
}

另外在上一部分所留下的,GameManaer_ZombieSpawner脚本中SpawnZombie函数的部分

 void SpawnZombie()
    {
        counter++;
        GameObject go = GameObject.Instantiate(zombiePrefab, zombieSpawn.transform.position, Quaternion.identity) as GameObject;
        NetworkServer.Spawn(go);
        go.GetComponent<Zombie_ID>().zombieID = "Zombie " + counter;
    }
代码较简单我就不注释了,真的看不懂的可以在评论区提出,我就是这么懒你咬我啊

发布了43 篇原创文章 · 获赞 17 · 访问量 5万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章