以下幾個Unity提供的API是關鍵
Selection.selectionChanged //監聽Scene或者Hierarchy窗口中選中項的改變
Selection.activeGameObject //當前選中的GameObject
SceneView.lastActiveSceneView.LookAt //看向目標點的接口
完整代碼如下:
//在合理的地方加上監聽, 比如Editor的OnEnable函數之類的
Selection.selectionChanged += OnSelectionChange;
//...
void OnSelectionChange()
{
SceneView.lastActiveSceneView.LookAt(Selection.activeGameObject.transform.position);
}