in unity, is it possible to edit objects' different variables in inspector using edtor…
- User
in unity, is it possible to edit objects' different variables in inspector using edtor scripts without requiring for scene to be opened by user?
- Assistant
in unity, is it possible to edit objects' different variables in inspector using edtor scripts without requiring for scene to be opened by user?
Yes, in Unity it is possible to edit an object's variables in the inspector via editor scripts without opening the scene in the editor's scene view. However, how you do this depends on what exactly you want to modify and how the assets are structured.
Editing Serialized Objects or Prefabs stored as assets:
You can modify serialized data on assets like prefab files or ScriptableObjects directly through editor scripts without opening the scene that contains them.
Editing Scene objects without opening scene:
Modifying objects instantiated in a scene asset on disk (e.g. inside .unity scene files) without loading that scene in the editor UI is more involved but possible by opening the scene additively and working in code without prompting the user.
You can access prefab assets or ScriptableObjects from the Project window using AssetDatabase APIs and modify their serialized properties:
using UnityEngine;
using UnityEditor;
public class EditPrefabExample
{
[MenuItem("Tools/Edit Prefab")]
static void EditPrefab()
{
// Load prefab asset
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/MyPrefab.prefab");
// Get SerializedObject
SerializedObject so = new SerializedObject(prefab);
// Assume it has a component MyComponent with a variable 'someValue'
var comp = prefab.GetComponent<MyComponent>();
if (comp != null)
{
SerializedProperty prop = so.FindProperty("someValue");
if (prop != null)
{
prop.intValue = 42; // Change someValue to 42
so.ApplyModifiedProperties();
// Mark asset dirty and save
EditorUtility.SetDirty(prefab);
AssetDatabase.SaveAssets();
Debug.Log("Prefab updated");
}
}
}
}
Scene files (.unity) are assets on disk. To modify a scene object’s variables without opening the scene for the user, you can:
EditorSceneManager.OpenScene(path, OpenSceneMode.Additive) to open the scene additively but keep your editor focused on another scene or no scene.Example snippet:
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EditSceneExample
{
[MenuItem("Tools/Edit Scene Object")]
static void EditSceneObject()
{
string scenePath = "Assets/Scenes/MyScene.unity";
Scene scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);
foreach (GameObject go in scene.GetRootGameObjects())
{
var myComp = go.GetComponent<MyComponent>();
if (myComp != null)
{
myComp.someValue = 100;
EditorUtility.SetDirty(myComp);
}
}
EditorSceneManager.SaveScene(scene);
EditorSceneManager.CloseScene(scene, true);
Debug.Log("Scene edited without user manually opening it.");
}
}
This way, you programmatically load the scene for editing but don't require the user to open it manually or have it visible.
| Scenario | Possible Without Opening Scene in Editor? | Notes |
|---|---|---|
| Edit prefab or ScriptableObject variables | Yes | Use AssetDatabase and SerializedObject |
Edit scene objects by modifying .unity file | Yes, programmatically load scene additively | Use EditorSceneManager.OpenScene with OpenSceneMode.Additive |
If you want to edit variables for objects in a scene without the user opening the scene, writing an editor script that loads the scene additively, modifies objects, saves, and closes the scene is the way to go.
If you want, I can provide a full example script tailored to your particular use case!
Not yet included in a published release.