I’ve realized that in my currect project and nearly all my other projects that I never want to see my UI when I’m in 3D mode and I never want to see my game objects when I’m 2D mode. So I want to make an editor script which turns off the UI layer when I’m in 3D mode and turns off a specified list of game objects when I’m in 2D mode. How do? ![]()
I’ve figured out through google and trial and error that this lets me know if I’ve clicked the 2D button or not:
But most of the time I get a null reference or nothing at all, but I assume that’s because I’m using the wrong callback to detect it.
Where should I put the script? Do I have to write a custom inspector or make a monobehaviour that I put on a game object? Or is it possible to just write a script that lies in my project folder and works from there? The latter is what I want.
SceneView for example has no scripting documentation and I’m really struggling with finding examples of something similar to what I want. Every example I find is for a custom inspector and I just want this script to work on it’s own by just existing in a project, if possible of course.
After some more trial and error this is what I have at the moment:
using UnityEngine;
using UnityEditor;
[ExecuteInEditMode]
public class SceneViewToggle : MonoBehaviour {
public GameObject[] objectsToHideIn2dMode;
bool lastModeWas2d;
void Update() {
if (SceneView.lastActiveSceneView == null) {
return;
}
if (SceneView.lastActiveSceneView.in2DMode && !lastModeWas2d) {
ShowUI();
}
else if (!SceneView.lastActiveSceneView.in2DMode && lastModeWas2d) {
HideUI();
}
}
void HideUI() {
lastModeWas2d = false;
foreach (GameObject obj in objectsToHideIn2dMode) {
obj.SetActive(true);
}
}
void ShowUI() {
lastModeWas2d = true;
foreach (GameObject obj in objectsToHideIn2dMode) {
obj.SetActive(false);
}
}
}
It has to be put on a game object which is not what I want and Update() is apparently only called in the editor when something is changed so I have to move or change a game object for this to work. I also don’t know how I can access the layers and hide/show the UI layer from script similar to clicking the eye-icon in the upper right corner of the Editor.
Anyone able to point me in the right direction?
Edit: Hmm, now that I think about it I guess I need to have it on a game object to be able to assign the objects I want to hide. But I guess that part could be solved better by hiding all other layers or something like that. This is just a rough first attempt at the problem.