unity3D按下同一键切换不同的相机两种方法

如何按同一个键来回切换不同的摄像机呢?

首先建立两个相机放在不同的物体下用来分别摄到不同的物体。如图示

unity3D按下同一键切换不同的相机两种方法_第1张图片

在sphere下的图如下:在这个相机上绑定一个脚本如图sphere:

unity3D按下同一键切换不同的相机两种方法_第2张图片


而sphere的代码如下:

using UnityEngine;
using System.Collections;

public class sphere : MonoBehaviour {


	
	// Update is called once per frame
	void Update () {
	
	              }

    public void controlsphere()
    {

        this.gameObject.active = true;
    }

    public void cannotcontrolsphere()
    { 
     this.gameObject.active = false;
    }
    

}
没错就是这么简单。因为重点在下一张图:

这张图看到我们在CUBE上绑定的Testtwo这个脚本。而不是在Camera上啊。。。

unity3D按下同一键切换不同的相机两种方法_第3张图片
看这个Testtwo的代码:

按键盘的A键通过if  else 来决定打开哪个相机,,,和上面的sphere代价关联起来就是SO easy有木有哇!~~~~~~

using UnityEngine;
using System.Collections;

public class testtwo : MonoBehaviour {

    private bool control;
    private sphere Sphere;
    public GameObject gm;
	void Start () {
        Sphere = GameObject.Find("Camera2").GetComponent();
	}
	
	// Update is called once per frame
	void Update () {
        if (Input.GetKeyDown(KeyCode.A))
        {
            if (control)
            {

                visible();
            
            }
            else
            {

                unvisible();
                Sphere.controlsphere();
               
            }
        }
	}

            void visible()
           {
               gm.active = true;
               control = false;
            }

            void unvisible()
            {
                gm.active = false;
                control = true;
            }
}

这样就实现了按A键实现来回切换两个相机的功能了。。。


******************************************************************************************************

那么下面再来看一种没有上面那个方便的方法。。。

这次是把脚本都绑定到两个相机上的。然后互相调用对方的脚本!

如图:

unity3D按下同一键切换不同的相机两种方法_第4张图片

脚本Testchange


using UnityEngine;
using System.Collections;

public class testchange : MonoBehaviour {

    private tocube Tocube;
    private bool controlling=true;

    void Start()
    {
       
         Tocube =GameObject.Find("Camera2").GetComponent();
    }
	void Update () {

        if (Input.GetKeyDown(KeyCode.E) && controlling)
            {
                
                cubeunvisible();
                Tocube.spherevisible();
            }
	  }


    public  void cubeunvisible()
            {
                this.active = false;
            }

    public void cubevisible()
    {
        this.active = true ;
        controlling = true;
    }
}

然后另个,看下图;

unity3D按下同一键切换不同的相机两种方法_第5张图片

Tocube的代码;


using UnityEngine;
using System.Collections;

public class tocube : MonoBehaviour {

    private testchange test;
    private bool controll;

	void Start () {

        test = GameObject.Find("Camera").GetComponent();
	}

	// Update is called once per frame
	void Update () {
        if (Input.GetKeyDown(KeyCode.E)&&controll)
        {

            sphereunvisible();
            test.cubevisible();
        }


	}

    public void sphereunvisible()
    {
        this.active = false;
    }

    public void spherevisible()
    {
        this.active = true;
        StartCoroutine(kk());
    }

    IEnumerator kk()
    {  
        
        yield return new WaitForSeconds(2f);
        controll = true;
    }

    
}

这样也实现了这个功能,只是耗费资源而又没第一种方法简单。只有我这种菜鸟才会想到这种方法,写下这种方法是为了记录一下程序路上一路走来所走的弯路。。。

你可能感兴趣的:(unity3D)