繁体   English   中英

如何检查/查找“变形”下存在的游戏对象是隐藏的还是活动的?

[英]How to check/find if a Gameobject present under a Transform is hidden or active?

我点击一个按钮,模型就会显示在场景中。当我再次单击同一按钮时,同一模型出现两次,如何停止它。 即如果模型/游戏对象已经加载,则无需再次加载。

我在这里使用Gameobject.find。当我单击一个按钮模型时,对应的模型就会出现,场景中存在的其他模型也会消失。由于其他模型被隐藏,此时Gameobject.find无法正常工作。任何更好的解决方案。请查看代码下面太多了。 :)

private string _assetname;

public void LoadAsstBundles(int choice)
{
    if (choice == 1)
    {
        _assetname = “Chair1”;
    }
    else if (choice == 2)
    {
        _assetname = “Chair2”;
    }
    else if (choice == 3)
    {
        _assetname = “Chair3”;

    }


    if (_assetsBundle == null)
    {
        Debug.Log("Could Not load AssetBundles");
    }
    else
    {


        if (GameObject.Find(_assetname + "(Clone)"))
        {
            Debug.Log("Already Loaded");
        }

        else
        {

            var asset = _assetsBundle.LoadAsset(_assetname);

            int childcounts = ParentTransform.childCount;

            Debug.Log("Asset name nw ==" + asset.name);


            Debug.Log("Asset Bundles Loaded");

            var go = (GameObject)Instantiate(asset, ParentTransform);

            int option = go.transform.GetSiblingIndex();


            int childcount = ParentTransform.childCount;

            for (int i = 0; i < childcount; i++)
            {

                if (option == i)
                {
                    ParentTransform.GetChild(i).gameObject.SetActive(true);
                    continue;
                }
                ParentTransform.GetChild(i).gameObject.SetActive(false);

            }

        }

    }

}

而不是完全使用Find而是使用变量,而是存储实例化对象时获得的引用,以后再使用它们,例如

// Here you store the reference from
// Assetbundle.LoadAsset
private object loadedAsset; 

// Here you store the reference to the assets instance itself
private GameObject assetInstance;

public void LoadAsstBundles(int choice)
{
    // Is _assetBundle available?
    if (!_assetsBundle)
    {
        Debug.Log("Could Not load AssetBundles", this);
        return;
    }  

    // Was the bundle loaded before?
    if (!loadedAsset)
    {
        loadedAsset = _assetsBundle.LoadAsset(_assetname);

        if(!loadedAsset)
        {
            Debug.LogError("unable to load asset", this);
            return;
        }

        Debug.Log("Asset Bundles Loaded", this);
    }

    // Is the object Instantiated in the scene?
    if(!assetInstance)
    {
        assetInstance = (GameObject)Instantiate(loadedAsset, ParentTransform);
        Debug.LogFormat(this, "Instantiated {0}", assetInstance.name);
    }

    // no need to go by names
    // simply get the correct child by index
    var selected = assetInstance.transform.GetChild(choice);

    // Enable or disable the childs
    // simply run through all childs no need to get their names etc
    foreach(Transform chair in assetInstance.transform)
    {
        chair.gameObject.SetActive(chair == selected);
    }
}

我使用Parenttransform.find来获取子隐藏对象。位长。欢迎任何更新的代码。

public void LoadAsstBundles(int choice)
        {
            if(choice==1)
            {
                _assetname = "Chair1";
            }
            else if(choice == 2)
            {
                _assetname = "Chair2";
            }
            else if(choice == 3)
            {
                _assetname = "Chair3";

            }

    if (_assetsBundle==null)
    {
        Debug.Log("Could Not load AssetBundles");
    }
    else
    {

       var asset= _assetsBundle.LoadAsset(_assetname);

        //if (GameObject.Find(_assetname + "(Clone)"))
        //{
        //    Debug.Log("Already Loaded");
        //}


        if(ParentTransform.Find(_assetname+ "(Clone)")== true)
        {
            Debug.Log("Already Loaded");

            int childcount = ParentTransform.childCount;
            for (int i = 0; i < childcount; i++)
            {

                if (choice == i)
                {
                    ParentTransform.GetChild(i).gameObject.SetActive(true);
                    continue;
                }
                ParentTransform.GetChild(i).gameObject.SetActive(false);

            }


        }

        else
        {


            Debug.Log("Asset Bundles Loaded");

            var go = (GameObject)Instantiate(asset, ParentTransform);

            int option = go.transform.GetSiblingIndex();
            _loadednames.Add(go.name);




            Debug.Log("Sibling index == " + go.transform.GetSiblingIndex());

            int childcount = ParentTransform.childCount;

            for (int i = 0; i < childcount; i++)
            {

                if (option == i)
                {
                    ParentTransform.GetChild(i).gameObject.SetActive(true);
                    continue;
                }
                ParentTransform.GetChild(i).gameObject.SetActive(false);

            }


        }

    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM