3秒切换 unity3d
2012-06-14 13:39:21 wangxfvc 阅读数 4217

Unity3D管理多相机很是繁琐,"Camera Preview"着实解决了相机的切换,然而窗口小,比例不能调整,并且在多个相机中禁用启用设置相机视角也很不方便,所以也不是很实用。

Security Camera就简化了这个过程。以下是Security Camera脚本的使用说明:

1、将SecurityCameraEditor放在Editor中,其脚本为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
using UnityEngine;
using UnityEditor;
using System.Collections;
 
[CustomEditor(typeof(SecurityCamera))]
public class SecurityCameraEditor : Editor
{
    SecurityCamera _target = null;
     
    void OnEnable(){
        if(_target == null && !Application.isPlaying){
            _target = (SecurityCamera)target;
            _target.camera.depth = 999999;
        }
    }
     
    void OnDisable(){
        if (!Application.isPlaying && _target != null) {
            _target.camera.depth = -999999;
        }
    }
}

 2、将SecurityCamera脚本和CameraSwap脚本放在Camera Script文件夹下,其中SecurityCamera脚本要加在每个相机上(主相机MainCamera的Tag为MainCamera,其他相机如Camera1,Camera2,Camera3),CameraSwap脚本加在GameObject(或者其他的物体上)。

SecurityCamera脚本:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
using UnityEngine;
using System.Collections.Generic;
 
public class SecurityCamera : MonoBehaviour{
    public static Dictionary<string, GameObject> Cameras = new Dictionary<string, GameObject>();
    public static GameObject CurrentCamera;
    static int conflictCount = 0;
     
    void Start(){
        if (Cameras.ContainsKey(name)) {
            Debug.LogWarning("SecurityCamera: Warning! All cameras used with SecurityCamera should have unique names. Conflict has been automatically renamed."); 
            name = name + "_" + ++conflictCount;
        }
         
        if (tag == "MainCamera" && name != "Main Camera") {
            Debug.LogWarning("SecurityCamera: Warning! A camera was found with a tag of 'MainCamera' but wasn't named 'Main Camera' this might be the result duplicating the default Main Camera. This camera's tag has been set to 'Untagged' to ensure proper operation of Unity's Camera.main.");
            tag = "Untagged";
        }
         
        Cameras.Add(name,gameObject);
        if (Camera.main.gameObject != gameObject) {
            gameObject.active = false;
        }else{
            CurrentCamera = Camera.main.gameObject;
        }
    }
     
    public static void ChangeCamera(string newCamera){
        CurrentCamera.active = false;
        Cameras[newCamera].active = true;
        CurrentCamera = Cameras[newCamera];
        CurrentCamera.camera.depth=999999;
    }
}

CameraSwap脚本为:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;
using System.Collections;
 
public class CameraSwap : MonoBehaviour
{
    void Update ()
    {
        //pressing 0, 1, 2, and 3 will swap before the cameras named "Main Camera",
        //"Camera 1", "Camera 2", and "Camera 3" which have Security Camera added:
        if (Input.GetKeyDown(KeyCode.Alpha0)) {
            SecurityCamera.ChangeCamera("Main Camera");
        }
        if (Input.GetKeyDown(KeyCode.Alpha1)) {
            SecurityCamera.ChangeCamera("Camera1");
        }
        if (Input.GetKeyDown(KeyCode.Alpha2)) {
            SecurityCamera.ChangeCamera("Camera2");
        }
        if (Input.GetKeyDown(KeyCode.Alpha3)) {
            SecurityCamera.ChangeCamera("Camera3");
        }
    }
}

其文件组织格式如下:

参考网站:http://pixelplacement.com/2011/02/14/security-camera/

2014-02-28 16:14:28 paopaohuiiz 阅读数 5294

把脚本拖到物体上,前提是要有Mesh Renderer,然后给Texture2D 数组赋值,选择Texture2D数组的size填入数字,直接拖动图片就行。如图所示:


//换贴图,直接把这段代码加在要换贴图的模型上即可;
var textures : Texture2D[]; //声明一个数组型的图片库;
private var i : float = 0; //声明i为浮点数0;

function OnGUI ()
{
	GUI.Button(Rect(0,0,100,100),"切换贴图");
}

function Update()
{
	//注解一:
	//Input.GetMouseButton(0)就是点击左键切换
	if(Input.GetMouseButtonDown(0))
	{
		if(i<textures.Length) //判断数组下标是否越界
		{		
			renderer.material.mainTexture = textures[i];
			++i;
			Debug.Log(i);
		}
	}	
}

注解一:Input.GetMouseButtonDown 获取鼠标按钮按下。

你需要在Update方法中调用这个方法,此后每一帧重置状态时,它将不会返回true除非用户释放这个鼠标按钮然后重新按下它。按钮值设定为 0对应左键,1对应右键,2对应中键。

运行结果,如图所示:



2015-07-02 16:29:06 gongfu_li 阅读数 1107

1,我已经创建好两个场景,放到Scene目录下


2,然后分别运行其场景,在运行的过程中,点击“File”->“Build Setting”;

如图所示,点击“Add Current”,场景就会自动加进去了


3,给LoginSence中的button加一个点击事件,代码如下

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.EventSystems;


public class LoginMgr : MonoBehaviour {


// Use this for initialization
void Start () {
        Debug.Log("game is start");


        var LoginBtn = gameObject.GetComponent<Button>();


        if (LoginBtn.name == "Btn_Login")
        {
            LoginBtn.ButtonClickEvent = LoginMethod;
        }
}


    public void LoginMethod(BaseEventData eventData)
    {
        Debug.Log("开始跳转场景");
        Application.LoadLevelAsync("SelectLevelSence");


        Debug.LogWarning("我成功啦");


    }




    void Awake() {
        Debug.Log(" this is LoginMgr's Awake()");
    }

// Update is called once per frame
void Update () {
        Debug.Log(" this is LoginMgr's Update()");
}




 
}

Application.LoadLevelAsync("SelectLevelSence");和Application.LoadLevelAsync(1);这两种方法都可以.。一个是加载名“SelectLevelSence”的关卡,一个是加载索引为1的关卡


4,运行游戏,点击按钮,就会进入下一个场景了

2013-02-26 16:42:35 fzhlee 阅读数 2663
using UnityEngine;
using System.Collections;

public class CameraSwap : MonoBehaviour
{
 void Update ()
 {
  //pressing 0, 1, 2, and 3 will swap before the cameras named "Main Camera",
  //"Camera 1", "Camera 2", and "Camera 3" which have Security Camera added:
  if (Input.GetKeyDown(KeyCode.Alpha0)) {
   SecurityCamera.ChangeCamera("Main Camera");
  }
  if (Input.GetKeyDown(KeyCode.Alpha1)) {
   SecurityCamera.ChangeCamera("Camera 1");
  }
  if (Input.GetKeyDown(KeyCode.Alpha2)) {
   SecurityCamera.ChangeCamera("Camera 2");
  }
  if (Input.GetKeyDown(KeyCode.Alpha3)) {
   SecurityCamera.ChangeCamera("Camera 3");
  }
 }
}

2013-01-09 15:16:00 iteye_21202 阅读数 44

#pragma strict

private var Camera0:GameObject;
private var Camera1:GameObject;
private var Camera2:GameObject;

function Start () {
Camera0=GameObject.Find("Main Camera");
Camera1=GameObject.Find("Camera1");
Camera2=GameObject.Find("Camera2");
}

function OnGUI () {
if(GUILayout.Button("侧面",GUILayout.Height(50))){
Camera0.active=false;
Camera1.active=false;
Camera2.active=true;
}

if(GUILayout.Button("正面",GUILayout.Height(50))){
Camera0.active=false;
Camera1.active=true;
Camera2.active=false;
}

if(GUILayout.Button("阿面",GUILayout.Height(50))){
Camera0.active=true;
Camera1.active=false;
Camera2.active=false;
}
}

Unity3D相机切换效果

阅读数 1443

unity3d中切换武器

阅读数 119

没有更多推荐了,返回首页