【问题标题】:Finding a disabled component of a gameobject and enabling it again找到游戏对象的禁用组件并再次启用它
【发布时间】:2016-05-26 05:49:09
【问题描述】:

我有一个 Key 可以禁用当前 GameObject 的 MeshRenderer 和 BoxCollider 组件。在这些被禁用并且用户再次按下相同的键之后,我想再次启用这些相同的组件,但由于它们被禁用,它似乎无法找到这些组件。我怎样才能找到这两个组件并再次启用它们?提前致谢!
这是我正在使用的脚本的链接(我在函数内部的第 27+28 行禁用了组件,并且我也在尝试在函数内部的第 36+37 行再次启用它们):https://gist.github.com/anonymous/702cf5630bfc7236ba52f6ec8d7cd7a4

编辑:或者告诉我我犯了什么错误。

【问题讨论】:

  • 它应该找到组件。这种情况对禁用的游戏对象有效,除非您有参考,否则您无法找到它。向我们展示调用“ShowStrut”函数的代码。
  • 在代码中。
  • 哦,我错过了.. 你能用debug.log确认这个函数是通过RPC调用的吗?
  • 是否有理由不只是缓存对这些组件的引用并将它们保留为字段?

标签: c# unity3d gameobject


【解决方案1】:

为什么不只遍历所有组件并启用/禁用它?

var allComponents : Component[];
allComponents = gameObject.GetComponents (Component);
for (var component : Component in allComponents) {
    // check here if this component is of interest may be by checking
    // tag and if it is of interest do the following
    //enable disable 
    component.enabled = !component.enabled;
}

[编辑] 统一 v 5.3

public Component[] GetComponentsInChildren(Type t, bool includeInactive = false); 

如果 includeInactive 为真,将返回所有活动和非活动的游戏对象。 因此,您现在执行以下操作

Component [] compList = yourGameObj.GetComponentsInChildren(typeof(Component));
//or type of game object 
foreach(Component comp : compList){
  //do your magic
}

[编辑] 我们不是来照顾孩子的,我目前没有在我现在使用的 PC 上安装 Unity,但是代码看起来像这样(正确以满足您的需要):

public class Strut : Photon.MonoBehaviour
{
    public bool DestroyByRpc;


    private MeshRenderer meshRenderer;
    private BoxCollider boxCollider;

    void Start(){
        Component [] compList = yourGameObj.GetComponentsInChildren(typeof(MeshRenderer), true);
        if (compList != null && compList.Length > 0)
            meshRenderer = compList[0];
        compList = yourGameObj.GetComponentsInChildren(typeof(BoxCollider), true);
        if (compList != null && compList.Length > 0)
            boxCollider = compList[0];


    }
    void Update()
    {
        if (Input.GetButtonDown ("LandingStruts")) {
            if (this.gameObject.activeSelf == true) {
                this.photonView.RPC("HideStrut", PhotonTargets.AllBuffered);
            } 
            else {
                this.photonView.RPC("ShowStrut", PhotonTargets.AllBuffered);
            }
        }


    }

    [RPC]
    public IEnumerator HideStrut()
    {
        meshRenderer.enabled = false;
        boxCollider.enabled = false;
        yield return 0;
        PhotonNetwork.UnAllocateViewID(this.photonView.viewID);
    }

    [RPC]
    public IEnumerator ShowStrut()  
    {
        meshRenderer.enabled = true;
        boxCollider.enabled = true;

        yield return 0;
        PhotonNetwork.UnAllocateViewID(this.photonView.viewID);
    }
}

【讨论】:

  • 你能用 C# 写这个吗?
  • @alexo1001 唯一的“主要”区别是:foreach (Component comp in allComponents) { comp.enabled = !comp.enabled },这不会改变概念本身...
  • 脚本中的 comp 和 compList 用什么替换?
  • 它给了我一条错误消息:无法隐式转换类型UnityEngine.Component' to UnityEngine.MeshRenderer'。存在显式转换(您是否缺少演员表?)
  • 可以试试 meshRenderer = (MeshRenderer)compList[0];
【解决方案2】:

您可以在代码中保留对组件的引用,因此您不需要一直获取它们。 这是一个例子:

public class Strut : Photon.MonoBehaviour
{
    MeshRenderer _meshRenderer;
    BoxCollider _collider;

    void Start()
    {
        _meshRenderer = GetComponent<MeshRenderer> ();
        _collider= GetComponent<BoxCollider> ();

    }

    [RPC]
    public IEnumerator HideStrut()
    {
        _meshRenderer.enabled = false;
        _collider.enabled = false;
        yield return 0;
        PhotonNetwork.UnAllocateViewID(this.photonView.viewID);
    }

    [RPC]
    public IEnumerator ShowStrut()  
    {
        _meshRenderer.enabled = true;
        _collider.enabled = true;
        yield return 0;
        PhotonNetwork.UnAllocateViewID(this.photonView.viewID);
    }

【讨论】:

    猜你喜欢
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多