c# - 在 Unity 中隐藏其他场景中的游戏对象
问题描述
我已经使用此代码来停止破坏特定的游戏对象。问题是当我将包含此对象的场景更改为另一个不同的场景时,即使在不同的场景中游戏对象仍然显示...如何仅在其场景中显示游戏对象。我希望我清楚地说明我的问题。
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
}
推荐阅读
- javascript - 谷歌图表调试
- python - 通过使用条件添加列来创建列
- android - 如何将 android MediaRecorder 输出转换为 MP3 文件
- ios - 如何仅在较小的设备上隐藏状态栏?
- c++ - Marshal 管理非托管的无符号整数数组
- java - 在 PrintWriter 格式错误中使用 PrintStream
- reactjs - 在 Docker 中使用 React 和 Nginx 授权 Spotify
- kubernetes - Kubernetes 检查“静默” pod 是否完成执行
- typescript - 在 TypeScript 中手动 curry 一个 add 函数
- here-api - 我如何从带有输入搜索的表单中获取 searchText 映射 javascript