4

我正在使用 Photon 在我的游戏中制作多人游戏,以确保一个玩家不会控制所有玩家,当您生成时,客户端会激活您的脚本/相机,以便您可以看到和移动。

虽然我想不出解决这个问题的方法,因为我不知道如何启用/禁用子组件或启用子组件。

我想通过脚本 http://imgur.com/ZntA8Qx启用它

这个 http://imgur.com/Nd0Ktoy

我的脚本是这样的:

using UnityEngine;
using System.Collections;

public class NetworkManager : MonoBehaviour {

public Camera standByCamera;
// Use this for initialization
void Start () {
Connect();
}

void Connect() {
Debug.Log("Attempting to connect to Master...");
PhotonNetwork.ConnectUsingSettings("0.0.1");
}

void OnGUI() {

GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
}

void OnConnectedToMaster() {
Debug.Log("Joined Master Successfully.");
Debug.Log("Attempting to connect to a random room...");
PhotonNetwork.JoinRandomRoom();
}

void OnPhotonRandomJoinFailed(){
Debug.Log("Join Failed: No Rooms.");
Debug.Log("Creating Room...");
PhotonNetwork.CreateRoom(null);
}

void OnJoinedRoom() {
Debug.Log("Joined Successfully.");
SpawnMyPlayer();
}
void SpawnMyPlayer() {
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("Body", Vector3.zero, Quaternion.identity, 0);
standByCamera.enabled = false;
((MonoBehaviour)myPlayerGO.GetComponent("Movement")).enabled = true;

}
}

monobehaivour 底部的位置是我想要启用它们的位置 正如你所看到的,我已经想出了如何激活我生成的游戏对象的一部分,我只需要上面所说的帮助,感谢您的帮助。

我通过一个预制件生成它,所以我希望它只编辑我生成的那个,而不是关卡中的所有其他的,因为我想使用 myPlayerGO 游戏对象启用这些组件,并且只编辑那个。

这就是我让我的游戏正常工作所需要的,所以请帮忙。

如果这是重复的,我很抱歉,因为我不知道如何写这个标题。

4

2 回答 2

1

我统一您可以使用gameObject.GetComponentInChildren启用和禁用子对象中的组件

ComponentYouNeed component = gameObject.GetComponentInChildren<ComponentYouNeed>();
component.enabled = false;

也可以使用gameObject.GetComponentsInChildren

于 2015-08-13T17:19:48.420 回答
1

两个图像链接都指向同一个地方,但我想我明白了。

我可能会建议您放置某种脚本,允许您在 Body 游戏对象中连接 HeadMovement 脚本。例如:

public class BodyController : MonoBehaviour
{
  public HeadMovement headMovement;
}

然后你可以在你的预制件中连接它,然后调用:

BodyController bc = myPlayerGo.GetComponent<BodyController>();
bc.headMovement.enabled = true;

另一个解决方法是使用 GetComponentsInChildren():

HeadMovement hm = myPlayerGo.GetComponentsInChildren<HeadMovement>(true)[0]; //the true is important because it will find disabled components.
hm.enabled = true;

我可能会说第一个是更好的选择,因为它更明确,也更快。如果您有多个 HeadMovements,那么您将遇到问题。它还需要爬取整个预制层次结构,才能找到在编译时已经知道位置的东西。

于 2015-08-13T17:24:14.597 回答