How to move camera in editor without acceleration & with custom speed (Feb 2017).
Just though I would thorw it into this forum in case his/her github repo gets down
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
[InitializeOnLoad]
public class EditorCameraSpeed
: EditorWindow
{
[MenuItem("Tools/Camera Speed &S")]
public static void CameraSpeed()
{
var window = GetWindow<EditorCameraSpeed>();
// TODO: gets cleared each frame?
SceneView.onSceneGUIDelegate += OnSceneDelegate;
}
static void CameraSpeedUpdate()
{
var e = Event.current;
// Tools.s_LockedViewTool is ViewTool.FPS when holding right-click down
// SceneView.OnGUI() calls SceneViewMotion.DoViewTool(self)
// SceneViewMotion.DoViewTool(): key down event: process WASD and add to s_Motion vector
// SceneViewMotion.DoViewTool(): layout event: view.pivot change by s_Motion * internal dt tracking
// solution 1: try and modify speed values during this update
// -> doesn't work because the timing of events and this call is different, and it is out of sync
// solution 2: get a callback in our code somewhere during the gui event handler so we can modify some values
// -> doesnt work because there seems to be no delegates or callbacks during SceneView.OnGUI that we can hook into
// solution 3: modify ilcode bytes of SceneViewMotion.GetMovementDirection() to return a modified value
// -> can modify ?
// solution 4: replace kFPSPref* input keys and move the main window pivot ourselves
// -> gotta implement a lot?
// solution 5: build custom permanent control and listen for OnGUI()
// -> will get events when not focused?
// -> will get events that are consumed by scene view?
}
static float cameraMoveSpeed = 10.0f;
static float cameraMoveSpeedCtrl = 1.0f;
public void OnGUI()
{
var event_ = Event.current;
var controlID = GUIUtility.GetControlID(FocusType.Passive);
var eventType = event_.GetTypeForControl(controlID);
cameraMoveSpeed = EditorGUILayout.Slider(cameraMoveSpeed, 0.0f, 10.0f);
cameraMoveSpeedCtrl = EditorGUILayout.Slider(cameraMoveSpeedCtrl, 0.1f, 1.0f);
SceneView.onSceneGUIDelegate -= OnSceneDelegate;
SceneView.onSceneGUIDelegate += OnSceneDelegate;
}
public static void OnSceneDelegate(SceneView sceneView)
{
if (Event.current.type != EventType.Layout)
return;
var tools_type = typeof(UnityEditor.Tools);
var locked_view_tool_field = (FieldInfo)tools_type.GetField("s_LockedViewTool", BindingFlags.NonPublic | BindingFlags.Static);
var locked_view_tool = (ViewTool)locked_view_tool_field.GetValue(null);
if (locked_view_tool != ViewTool.FPS)
return;
var scene_view_assembly = Assembly.GetAssembly(typeof(UnityEditor.SceneView));
var scene_view_motion_type = scene_view_assembly.GetType("UnityEditor.SceneViewMotion");
var scene_view_flyspeed_type = scene_view_assembly.GetType("UnityEditor.SceneViewMotion");
var flyspeed_field = (FieldInfo)scene_view_motion_type.GetField("s_FlySpeed", BindingFlags.NonPublic | BindingFlags.Static);
var flyspeed = flyspeed_field.GetValue(null);
var flyspeed_modified = (float)flyspeed;
flyspeed_modified = cameraMoveSpeed;
if (Event.current.control)
flyspeed_modified = cameraMoveSpeedCtrl;
flyspeed_field.SetValue(null, flyspeed_modified);
// we can stop input with this
//Event.current.Use();
}
}
The script does work enough to show an Editor window in the editor under the new Tab of Tools
If I hold down the Key ‘CTRL’ the scolling stops
Modifying the values for cameraMoveSpeed to extremes like 10,000 does not change behavior.
I don’t see a keyboard key I should be holding down in your code, so I assume it is supposed to be on all of the time.
Any advice on why this is not working?
Should the block of commented out code get un-commented?
As mentioned in my post, this code is not mine, but works for me on Windows 10 Unity 2017.2
I would try changing ‘Event.current.control’ to something like ‘Event.current.alt’ or some other button.
This way you can see if an issue is related to the entire script, or it’s just ctrl that doesn’t work
Great man… long time issue with Unity finally resolved… it’s amazing how many people work at Unity (and outside of it), that must really enjoy that garbage built in editor camera and it’s fly speed.
One day I was presenting my work (NG Remote Pro - https://assetstore.unity.com/packages/tools/utilities/ng-remote-scene-pro-96879) to several Unity guys at a Meetup, one was the head of something, I don’t remember what.
He said in front of me : “Interesting work, it will take what… 6 months to do it?”
I did not answered, but I smelled the bullshit. Unity has 4762105397 steps before releasing a feature.
6 months would have turn into 4 years…
yeah I can imagine, btw you should probably do some videos of your assets, gives potential customers a better idea of how things work or potential use cases for things.
They did… and there are options now… but the constant acceleration is STILL THERE. Meaning you cant set one value as a max, it says it will do that… but it totally doesnt.
Meaning if you have a large scene, and are trying to fly over it and find stuff, you will eventually start flying so fast you have to stop to reset it… rinse and repeat forever.
Does your script still fix this? Because good spagat… its so stupid.
Hi, Mikilo,
Just a quick update regarding the new Unity camera controls. First, though – many thanks for posting the camera control script!
Unfortunately, like GeekGonzo, I’ve found that Unity’s new camera/acceleration-control system doesn’t work. I can sort of adjust speeds on the fly with the scroll wheel, but the acceleration function is still there. I’ve been using Unity since 2009, and this has ALWAYS been an issue (the endless acceleration of the camera). I’m guessing that someone put a += loop into the first version of the Unity editor and they’ve never bothered to add an upper limit.
But regarding your script, I’ve got some bad news: I’m currently using Unity 2019.1.5, and your script doesn’t seem to be working. If you can find it in your amazingly generous heart I’ll be enormously grateful for a patch to your script. Either way, thanks for all the great stuff you do for the commUnity!
Thanks for any help you can provide – and safe travels!
For my part, I have an update to report regarding Unity’s scene view camera: I’ve been able to get around the acceleration problem (mostly) by unchecking the “ease” function AND by setting the speeds to a pretty high value (e.g. 20 - 30). Because the camera starts at a high speed, there’s far less need to hold it down for very long. Not perfect, but it helps!