Unity功能代码插件 中只显示选中物体 隐藏其他物体

分享一个在Unity中控制代码,只显示选中物体和Camera还有挂载本脚步的物体
使用方法:在场景中添加一个空物体,挂载本脚本即可。

实现很简单,
1.获取本场景所有物体。

 public List<GameObject> list;
 public void Init()
    {
        list = new List<GameObject>();
        GameObject[] all = Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[];
        for (int i = 0; i < all.Length; i++)
        {
            var item = all[i];
            if (item.scene.isLoaded)
            {
                list.Add(all[i]);
            }
        }
    }

2.遍历所有list,通过isOtherShow来制定是否显示。

3.通过Selection.activeObject判断是否选中物体,定义GameOvject数组show接收Selection.gameObjects获取所有选中的物体。

4.显示对象所有父结点。

public void ShowParent(GameObject obj)
    {
        try
        {
          	while (obj != null)
            {
                obj.SetActive(true);
                obj = obj.transform.parent.gameObject;
            }
        }
        catch
        {
            obj.SetActive(true);

        }
    }

需要try catch一下 否则到根结点回抱空。

5.显示对象所有子节点,

  public void ShowChild(GameObject obj)
    {
        foreach (Transform g in obj.GetComponentsInChildren<Transform>(true))
        {
            //Debug.Log(obj.GetComponentsInChildren<Transform>(true).Length);
            g.gameObject.SetActive(true);
        }
    }

注意在GameObject.GetComponentsInChildren(true)括号内填true,可以获取未激活(显示)物体,包括本身
若为false则只获取激活(显示)的物体,包括本身,
不填写默认为false

下面为完整代码

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
//[ExecuteInEditMode]为编辑器模式下可运行
//[ExecuteInEditMode]
public class OnlyShowObj : MonoBehaviour
{
    public List<GameObject> list;
    public GameObject[] show;

    public bool isOtherShow = false;
    void Start()
    {
        Init();   
    }
    public void Init()
    {
        list = new List<GameObject>();
        GameObject[] all = Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[];
        for (int i = 0; i < all.Length; i++)
        {
            var item = all[i];
            if (item.scene.isLoaded)
            {
                list.Add(all[i]);
            }
        }
    }
    void Update()
    {
        Init();
        for (int i = 0; i < list.Count; i++)
        {
            if (!(list[i] == gameObject || list[i].GetComponent<Camera>() != null))
            {
                list[i].SetActive(isOtherShow);
            }
        }
        if (Selection.activeObject != null)
        {
            show = new GameObject[Selection.gameObjects.Length];
            show = Selection.gameObjects;
            for (int i = 0; i < show.Length; i++)
            {
                ShowParent(show[i]);

                ShowChild(show[i]);
            }
        }
    }

    public void ShowParent(GameObject obj)
    {
        try
        {
            while (obj != null)
            {
                obj.SetActive(true);
                obj = obj.transform.parent.gameObject;
            }
        }
        catch
        {
            obj.SetActive(true);

        }
    }

    public void ShowChild(GameObject obj)
    {
        foreach (Transform g in obj.GetComponentsInChildren<Transform>(true))
        {
            //Debug.Log(obj.GetComponentsInChildren<Transform>(true).Length);
            g.gameObject.SetActive(true);
        }
    }
}

小白一个,有说的不对的地方欢迎指出。
希望对大家有帮助。

如果文章对你有帮助,不妨关注我一下,点个赞。
我会一直分享Unity开发中解决的坑,分享学到的技术,也会分享读书心得和理财心得,谢谢大家。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Unity选中物体并高亮显示可以使用射线检测方法和Outline System插件来实现。首先,你可以在摄像机上添加Physics Raycaster组件,然后在想要高亮显示物体上添加一个标签。这样,当鼠标移入物体时,你可以通过射线检测到该物体,并在代码改变物体的层,使其被Outline System插件识别并显示高亮轮廓。这种方法的优点是方便管理,无需其他调用,代码清晰。[1] 另一种方法是使用IPointerEnterHandler和IPointerExitHandler方法。你需要在每个要高亮显示物体上挂载一个脚本,并实现这两个接口的方法。当鼠标移入物体时,会触发IPointerEnterHandler的方法,你可以在这个方法改变物体的层,使其被Outline System插件识别并显示高亮轮廓。当鼠标移出物体时,会触发IPointerExitHandler的方法,你可以在这个方法物体的层恢复为原来的层。这种方法的缺点是需要在每个物体上挂载脚本,但优点是方便管理,无需其他调用,代码清晰。[1] 总结一下,使用射线检测方法的优点是挂一个脚本在摄像机上,其余想要高亮显示物体统一添加一个标签就行,缺点是写起来较乱,方法调用麻烦。使用IPointerEnterHandler和IPointerExitHandler方法的缺点是需要在每个要高亮的物体上挂载一个,优点是方便管理,无需其他调用,代码清晰。[1] 希望这些信息对你有帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真鬼123

祝你节节高升岁岁平安越来越漂亮

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值