[Unity]유니티 스크립트_ Script _GAME CAMERA(VIEW) PANNING

2019. 8. 16. 16:50unity/unity script

in Unity serving two view mode game view and scene view
Sceneview is can possibly that control viewport as panning rotate zoom
but Gameview is only offered captured scene by camera

usually, look at Scene view when artist crafting art asset(effect, prefab..)
but some times needs to look at Gameview on working for a perfect match on gameplay

 

on this situation we need some controllable Gameview

that is very easy during gameplay, just manipulate camera's 'transform' from input key action

 

but on editor mode situation, 

 

designers need control able Gameview for their work

 

this script offered some camera moving action for game view
drag mouse on Gameview with pushing shift key is  zoom action, also dragging with control key is panning action

 

no more need this action

select prefab 

working

need view change

select camera

move transform

select effect prefab

working

need view change

select camera..

..

you just dragging on game view with correct key

 

 

 

 

//////////////////////////////////////////  code explane update later!!

we will think about how controll viewport

it's simple, zoom is using  ortho size, panning is moving positions

 

point is how to make it working at editormode?\

we find that answer at unity3d event category

 

 

 

using UnityEngine; 
using UnityEditor; 
using System.Collections; 

[ExecuteInEditMode] 

public class cameraControlTest : MonoBehaviour 


    public Vector3 FMousePosition = new Vector3(0, 0, 0); 
    public Vector3 CMousePosition = new Vector3(0, 0, 0); 
    public Vector3 FMousePosition2 = new Vector3(0, 0, 0); 
    public Vector3 CMousePosition2 = new Vector3(0, 0, 0); 
    private float range = 0.0f; 
    public Vector3 movevector = new Vector3(0, 0, 0); 
    public float panspeed = 1.0f; 
    private float vectorx = 0.0f; 
    private float vectorx2 = 0.0f; 
    public float wheelscale = 0.0f; 

//> vector3 variables are for calculating mouse dragging distance

The reason why all variables are public is that they are under development

also this script is excluded on build version

any variables are  not necessary to be 'public'

 

    void OnGUI() 
    { 

    Event e = Event.current; 
        range = range + Time.deltaTime; 
        if (e.control) //if (e.button == 1) 
//>check pushing 'control key'

(e.control = event.current.control )is checking keyboard event

some key has no reactions, that is don't know why is, so found a workable key of keyboard


        { 
            //---- 마우스 우클릭을 제외한 다른 클릭에대한 오류가 있음(좌클릭은 모든 클릭에 반영 
            if (e.button == 1) //if (e.control) 

//>under blocks code line is calculated when pushing 'controlkey' and pushing mouse button 1(right button) 
            { 
                if (range > 0.1f) 
                { 
                    CMousePosition = e.mousePosition; 
                    float vectorx = FMousePosition.x; 
                    if (vectorx != 0) 
                        {     } 
                    else 
                        FMousePosition = e.mousePosition; 

                range = 0.0f; 


        movevector = new Vector3((CMousePosition - FMousePosition).x, 0f, (CMousePosition - FMousePosition).y); 
        gameObject.transform.localPosition = gameObject.transform.position + movevector * (panspeed*0.001f); 

                } 
            } 
                  
//>all conditions are true is calculate mouse dragging distance and that append to the camera position
         
            else 
            { 
                FMousePosition = new Vector3(0, 0, 0);//마우스 버튼을 떼면 처음 위치는 초기화 
                FMousePosition2 = new Vector3(0, 0, 0);//마우스 버튼을 떼면 처음 위치는 초기화 
            } 

//>this block is reset function

position variables are applied zero when leaving the mouse button 

        } 

 

//> this block is exactly the same as above
//> The difference is that check the shift button


        else if (e.shift) 
            //---- 마우스 우클릭을 제외한 다른 클릭에대한 오류가 있음(좌클릭은 모든 클릭에 반영 
            if (e.button == 1) //if (e.control) 
            { 
               if (range > 0.1f) 
                { 

                    CMousePosition2 = e.mousePosition; 
                    float vectorx2 = FMousePosition2.x; 
                    if (vectorx2 != 0) 
                    {                     } 
                    else 
                        FMousePosition2 = e.mousePosition; 

                    range = 0.0f; 
                    wheelscale = (CMousePosition2 - FMousePosition2).y; 
                    Camera cam = gameObject.GetComponent(); 
                    cam.orthographicSize = cam.orthographicSize + wheelscale * (panspeed * 0.001f); 
                } 
            } 

            else 
            { 
                FMousePosition2 = new Vector3(0, 0, 0);//마우스 버튼을 떼면 처음 위치는 초기화 
            } 
    } 
}

 

 

actually part of the calculation is very easy even not to explain
we need are how to make it on editor mode

 

 

 

////////////////////////////////////////// full code

 

using UnityEngine;
using UnityEditor;
using System.Collections;

[ExecuteInEditMode]



public class cameraControlTest : MonoBehaviour

{
    public Vector3 FMousePosition = new Vector3(0, 0, 0);
    public Vector3 CMousePosition = new Vector3(0, 0, 0);
    public Vector3 FMousePosition2 = new Vector3(0, 0, 0);
    public Vector3 CMousePosition2 = new Vector3(0, 0, 0);
    private float range = 0.0f;
    public Vector3 movevector = new Vector3(0, 0, 0);
    public float panspeed = 1.0f;
    private float vectorx = 0.0f;
    private float vectorx2 = 0.0f;
    public float wheelscale = 0.0f;

    void OnGUI()
    {

    Event e = Event.current;
        range = range + Time.deltaTime;
        if (e.control) //if (e.button == 1)

        {
            //---- 마우스 우클릭을 제외한 다른 클릭에대한 오류가 있음(좌클릭은 모든 클릭에 반영
            if (e.button == 1) //if (e.control)
            {
                Debug.Log("keycontrol");
                
                if (range > 0.1f)
                {
                    
                    CMousePosition = e.mousePosition;
                    //Debug.Log(CMousePosition);
                                        
                    float vectorx = FMousePosition.x;
                
                    if (vectorx != 0)
                        {
                        }
                    else
                        {
                        FMousePosition = e.mousePosition;
                        Debug.Log("Fmouse");
                        }


                range = 0.0f;
                movevector = new Vector3((CMousePosition - FMousePosition).x, 0f, (CMousePosition - FMousePosition).y);

                gameObject.transform.localPosition = gameObject.transform.position + movevector * (panspeed*0.001f);

                }
            }
                 

        
            else
            {
                FMousePosition = new Vector3(0, 0, 0);//마우스 버튼을 떼면 처음 위치는 초기화
                FMousePosition2 = new Vector3(0, 0, 0);//마우스 버튼을 떼면 처음 위치는 초기화
            }
         
        }

        else if (e.shift)
            //---- 마우스 우클릭을 제외한 다른 클릭에대한 오류가 있음(좌클릭은 모든 클릭에 반영
            if (e.button == 1) //if (e.control)
            {
                Debug.Log("keyshift");

                if (range > 0.1f)
                {

                    CMousePosition2 = e.mousePosition;
                    //Debug.Log(CMousePosition);

                    float vectorx2 = FMousePosition2.x;

                    if (vectorx2 != 0)
                    {
                    }
                    else
                    {
                        FMousePosition2 = e.mousePosition;
                        Debug.Log("Fmouse");
                    }

                    range = 0.0f;
                    wheelscale = (CMousePosition2 - FMousePosition2).y;
                    Camera cam = gameObject.GetComponent();
                    cam.orthographicSize = cam.orthographicSize + wheelscale * (panspeed * 0.001f);
                }
            }

            else
            {
                FMousePosition2 = new Vector3(0, 0, 0);//마우스 버튼을 떼면 처음 위치는 초기화
            }
    }
}