// Use this for initialization void Awake() { //实例化玩家 Player selectedPlayer = PlayerInfoModel.Instance.SelectedPlayer; GameObject player = Instantiate(PlayerInfoModel.Instance.playerBody[selectedPlayer.occupation, selectedPlayer.sex]); player.transform.position = transform.position; //添加组件和脚本 Rigidbody body = player.AddComponent <Rigidbody>(); body.constraints = RigidbodyConstraints.FreezeRotation; player.AddComponent <NavMeshAgent>().baseOffset = -0.15f; player.GetComponent <NavMeshAgent>().stoppingDistance = 0.5f; player.AddComponent <PlayerController>(); //设置数据 Transform headBar = player.transform.FindChild("PlayerHeadBar"); PlayerHeadBar headBarScript = headBar.GetComponent <PlayerHeadBar>(); headBarScript.SetData(selectedPlayer); //设置相机目标 GameObject mainCamera = Camera.main.gameObject; ThirdPersonCamera script = mainCamera.GetComponent <ThirdPersonCamera>(); script.target = player.transform; }
void IsOn(bool isOn) { for (int i = 0; i < headImgGrid.Length; i++) { if (headImgGrid[i].isOn) { index = i; //之前显示对象模型禁用 if (curBodyGo != null) { if (lastIndex == index) { return; } else { curBodyGo.SetActive(false); } } //在角色显示台显示目前选中的角色模型 //已经实例化,(激活) if (playerBodyGoList.ContainsKey(index)) { curBodyGo = playerBodyGoList[index]; curBodyGo.SetActive(true); } //还未实例化 else { GameObject playerBodyGo = Instantiate(playerBody[i / headImg.GetLength(1), i % headImg.GetLength(1)]); playerBodyGo.transform.position = stage.position + Vector3.up * 0.3f; playerBodyGo.transform.SetParent(allCharacters.transform); curBodyGo = playerBodyGo; //设置属性 Player player = PlayerFactory.GeneratePlayer((OccupationType)(i / headImg.GetLength(1)), ""); Transform headBar = curBodyGo.transform.FindChild("PlayerHeadBar"); PlayerHeadBar headBarScript = headBar.GetComponent <PlayerHeadBar>(); headBarScript.SetData(player); //加进容器 playerBodyGoList.Add(index, curBodyGo); } //更新摄像机目标 GameObject mainCamera = Camera.main.gameObject; ThirdPersonCamera script = mainCamera.GetComponent <ThirdPersonCamera>(); script.target = curBodyGo.transform; lastIndex = index; break; } } }
void Init() { characterCount = PlayerInfoModel.Instance.characterInfoList.Count; //实例化已创建角色 for (int i = 0; i < characterCount; i++) { Player info = PlayerInfoModel.Instance.characterInfoList[i]; GameObject playerBodyGo = Instantiate(playerBody[info.occupation, info.sex]); playerBodyGo.transform.position = stage.position + Vector3.up * 0.3f; playerBodyGo.transform.SetParent(stage); //设置数据 Transform headBar = playerBodyGo.transform.FindChild("PlayerHeadBar"); PlayerHeadBar script = headBar.GetComponent <PlayerHeadBar>(); script.SetData(info); playerBodyGoList.Add(playerBodyGo); playerBodyGo.SetActive(false); } }