在Unity中隐藏其他场景中的游戏对象 [英] Hide game object in other scenes in Unity

查看:45
本文介绍了在Unity中隐藏其他场景中的游戏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用此代码停止销毁特定游戏对象.问题是当我将包含这个对象的场景更改为另一个不同的场景时,即使在不同的场景中,游戏对象仍然显示......如何仅在其场景中显示游戏对象.我希望我清楚地说明我的问题.

I've used this code to stop destroying a specific game object. The problem is when i change the scene that contain this object to another different scene ,the game object still showing even in different scene... How to only show the game object in its scene. I hope that I clearly specify my problem.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DontDestroy : MonoBehaviour {
 void Awake() {
     DontDestroyOnLoad(this.gameObject);
     if(FindObjectsOfType(GetType()).Length > 1) {
         Destroy(gameObject);
     }
   }
}

推荐答案

您可以尝试这样做:

m_Scene = SceneManager.GetActiveScene();
sceneName = m_Scene.name;
//Do logic by checking the specific scene you want
if (sceneName == YOUR_SCENE) {
    this.gameObject.SetActive(false);  //Set false to hide, true to show

}

这篇关于在Unity中隐藏其他场景中的游戏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆