I need to set position of my Player at center screen of the Editor Viewport camera when my game starts.
I know i should use SceneView.lastActiveSceneView
to get current viewport camera but the problem is i can’t access Editor codes when im in MonoBehaviour
script in Runtime mode (when im testing the game) so it returns me Null error and it can’t find SceneView
.
So i need access Editor codes in Runtime. I don’t know is it possible or not but i think almost there is a way for every issue.
UnityEditor.SceneView sceneView = UnityEditor.SceneView.lastActiveSceneView;
if (sceneView != null)
{
// Get the center of the SceneView camera
Vector3 cameraCenter = sceneView.camera.transform.position;
// Set the player position to the camera center
transform.position = cameraCenter;
}