摘要:本文原创,转载请注明出处http://www.cnblogs.com/AdvancePikachu/p/6742796.html
需求:
在游戏的任务编辑场景进行编辑的时候,摄像机需要在多个需要编辑的物体之间来回切换,如果只是用摄像机的移动旋转,对于相对位置较近的物体还好说,当相对位置过远的时候,就需要有一个聚焦的功能,可以很方便的自动把相机聚焦到需要编辑物体的一个相对可设置的位置。
如图:
如果有聚焦功能,就可以很方便的让摄像机在 Cube和Sphere之间聚焦。
1 public Transform _cube; 2 // Use this for initialization 3 void Start () { 4 _cube = GameObject.Find ("Cube").transform; 5 ViewSwitch.Instacne.GetRelativePosition (-10, 1, 0); 6 } 7 8 // Update is called once per frame 9 void Update () { 10 if(Input.GetKey(KeyCode.Space)) 11 ViewSwitch.Instacne.GetData(this.transform,_cube); 12 }
一个简单的调用即可达到下图的效果:
废话不多说,上代码:


1 //===== AdvancePikachu 2017 ======== 2 //文件功能描述;相机聚焦 3 //创建表示;AdvancePikachu 2017/4/20 4 //======================================================== 5 6 using UnityEngine; 7 using System.Collections; 8 9 public class ViewSwitch : MonoBehaviour { 10 11 private static ViewSwitch instance; 12 13 public static ViewSwitch Instacne { 14 get { 15 if (instance == null) { 16 GameObject go = new GameObject (); 17 go.name = "ViewSwitch"; 18 instance = go.AddComponent<ViewSwitch> (); 19 }