當前位置: 首頁>>代碼示例>>C#>>正文


C# Camera.ScreenToWorldPoint方法代碼示例

本文整理匯總了C#中Camera.ScreenToWorldPoint方法的典型用法代碼示例。如果您正苦於以下問題:C# Camera.ScreenToWorldPoint方法的具體用法?C# Camera.ScreenToWorldPoint怎麽用?C# Camera.ScreenToWorldPoint使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Camera的用法示例。


在下文中一共展示了Camera.ScreenToWorldPoint方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Awake

    float rightBoundary; //right boundary of screen

    #endregion Fields

    #region Methods

    void Awake()
    {
        playerRef = GameObject.FindGameObjectWithTag ("Player").transform;
        mainCam = Camera.main;
        leftBoundary = mainCam.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x - offset;
        rightBoundary = mainCam.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x + offset;
    }
開發者ID:mittal-himanshu,項目名稱:Infinity-Jumper,代碼行數:13,代碼來源:LeftToRight.cs

示例2: CreateFullScreenQuadMesh

    public static Mesh CreateFullScreenQuadMesh(Camera camera)
    {
        float z = 10;

        // 0 - 1
        // 2 - 3

        Vector3[] verts = new Vector3[4];
        verts [0] = camera.ScreenToWorldPoint (new Vector3 (0, 0, z));
        verts [1] = camera.ScreenToWorldPoint (new Vector3 (camera.pixelWidth, 0, z));
        verts [2] = camera.ScreenToWorldPoint (new Vector3 (0, camera.pixelHeight, z));
        verts [3] = camera.ScreenToWorldPoint (new Vector3 (camera.pixelWidth, camera.pixelHeight, z));

        Vector2[] uvs = new Vector2[4];
        uvs [0] = new Vector2 (0f, 0f);
        uvs [1] = new Vector2 (1f, 0f);
        uvs [2] = new Vector2 (0f, 1f);
        uvs [3] = new Vector2 (1f, 1f);

        int[] tris = new int[6] { 2, 1, 0, 3, 1, 2 };

        Mesh mesh = new Mesh ();
        mesh.vertices = verts;
        mesh.uv = uvs;
        mesh.triangles = tris;
        mesh.RecalculateNormals ();

        return mesh;
    }
開發者ID:gorecode,項目名稱:they-will-burn,代碼行數:29,代碼來源:ProceduralGeometry.cs

示例3: SetCameraBounds

	// This function is used by camBounds to set _camBounds and can also be
	// called directly
	public static void SetCameraBounds(Camera cam = null) {
		// If no Camera, use the main camera
		if (cam == null) cam = Camera.main;
		// This makes a couple of important assumptions about the camera
		// 1. Camera is orthographic
		// 2. Camera is at a rotation of R:[0,0,0]

		// Make Vector3s at the topLeft and bottomRight of the screen coords
		Vector3 topLeft = new Vector3(0,0,0);
		Vector3 bottomRight = new Vector3 (Screen.width, Screen.height, 0);

		// Convert these to world coordinates
		Vector3 boundTLN = cam.ScreenToWorldPoint(topLeft);
		Vector3 boundBRF = cam.ScreenToWorldPoint (bottomRight);

		// Adjust their zs to be at the near and far camera clipping planes
		boundTLN.z += cam.nearClipPlane;
		boundBRF.z += cam.farClipPlane;

		// Find the center of the Bounds
		Vector3 center = (boundTLN + boundBRF)/2f;
		_camBounds = new Bounds (center, Vector3.zero);
		// Expand _camBounds to encapsulate the extents.
		_camBounds.Encapsulate(boundTLN);
		_camBounds.Encapsulate (boundBRF);
	}
開發者ID:yjlintw,項目名稱:EECS494_Project1,代碼行數:28,代碼來源:Utils.cs

示例4: Start

    void Start()
    {
        mainCamera = GetComponent<Camera> ();
        //Create a Dictionary to contain all our Objects/Transforms
        System.Collections.Generic.Dictionary<string,Transform> colliders = new System.Collections.Generic.Dictionary<string,Transform>();
        //Create our GameObjects and add their Transform components to the Dictionary we created above
        colliders.Add("Top",new GameObject().transform);
        colliders.Add("Bottom",new GameObject().transform);
        colliders.Add("Right",new GameObject().transform);
        colliders.Add("Left",new GameObject().transform);
        //Generate world space point information for position and scale calculations
        Vector3 cameraPos = mainCamera.transform.position;
        screenSize.x = Vector3.Distance (mainCamera.ScreenToWorldPoint(new Vector3(0,0,-cameraPos.z)),mainCamera.ScreenToWorldPoint(new Vector3(Screen.width, 0,-cameraPos.z))) * 0.5F; //Grab the world-space position values of the start and end positions of the screen, then calculate the distance between them and store it as half, since we only need half that value for distance away from the camera to the edge
        screenSize.y = Vector3.Distance (mainCamera.ScreenToWorldPoint(new Vector3(0,0,-cameraPos.z)),mainCamera.ScreenToWorldPoint(new Vector3(0, Screen.height,-cameraPos.z))) * 0.5F;
        //For each Transform/Object in our Dictionary
        foreach(KeyValuePair<string,Transform> valPair in colliders){
            valPair.Value.gameObject.AddComponent<BoxCollider>().material = physicsMaterial; //Add our colliders. Remove the "2D", if you would like 3D colliders.
            valPair.Value.name = valPair.Key + "Collider"; //Set the object's name to it's "Key" name, and take on "Collider".  i.e: TopCollider
            valPair.Value.parent = transform; //Make the object a child of whatever object this script is on (preferably the camera)

            if(valPair.Key == "Left" || valPair.Key == "Right") //Scale the object to the width and height of the screen, using the world-space values calculated earlier
                valPair.Value.localScale = new Vector3(colThickness, screenSize.y * 2, 10*colThickness);
            else
                valPair.Value.localScale = new Vector3(screenSize.x * 2, 10*colThickness, colThickness);
        }
        //Change positions to align perfectly with outter-edge of screen, adding the world-space values of the screen we generated earlier, and adding/subtracting them with the current camera position, as well as add/subtracting half out objects size so it's not just half way off-screen
        colliders["Right"].position = new Vector3(cameraPos.x + screenSize.x + (colliders["Right"].localScale.x * 0.5f), cameraPos.y, zPosition);
        colliders["Left"].position = new Vector3(cameraPos.x - screenSize.x - (colliders["Left"].localScale.x * 0.5f), cameraPos.y, zPosition);
        colliders["Top"].position = new Vector3(cameraPos.x, cameraPos.y + screenSize.y + (colliders["Top"].localScale.y * 0.5f), zPosition);
        colliders["Bottom"].position = new Vector3(cameraPos.x, cameraPos.y - screenSize.y - (colliders["Bottom"].localScale.y * 0.5f), zPosition);
    }
開發者ID:cowstheory,項目名稱:CowsTheory,代碼行數:31,代碼來源:AddColliders.cs

示例5: Start

 //Runs as soon as the program starts
 void Start()
 {
     mainCamera = FindObjectOfType<Camera> ();
     gameManager = FindObjectOfType<GameManager> ();
     StartingLocation ();
     topWall = (new Vector3 (0f,mainCamera.ScreenToWorldPoint(new Vector3 (0f, Screen.height,0f)).y,0).y) - (transform.localScale.y);
     bottomWall =  (new Vector3 (0f,mainCamera.ScreenToWorldPoint(new Vector3 (0f, 0f ,0f)).y).y) + (transform.localScale.y);
 }
開發者ID:Solobolt,項目名稱:Pong,代碼行數:9,代碼來源:Paddle.cs

示例6: Start

 // Use this for initialization
 void Start()
 {
     target = GameMain.Instance.playerObject;
     transform.position = target.transform.position;
     camera = GetComponent<Camera>();
     cameraSize = new Vector3 (0,0,0);
     cameraSize.x = transform.position.x - camera.ScreenToWorldPoint(new Vector3(0,0,0)).x;
     cameraSize.y = transform.position.y - camera.ScreenToWorldPoint(new Vector3(0,0,0)).y;
 }
開發者ID:nxp040,項目名稱:Cat_Game,代碼行數:10,代碼來源:MoveWith.cs

示例7: OnDrawGizmos

    void OnDrawGizmos()
    {
        focusedCamera = camera;

        topRight = focusedCamera.ScreenToWorldPoint(new Vector3(focusedCamera.pixelWidth, focusedCamera.pixelHeight, focusedCamera.nearClipPlane));
        bottomRight  = focusedCamera.ScreenToWorldPoint(new Vector3(focusedCamera.pixelWidth, 0, focusedCamera.nearClipPlane));
        topLeft = focusedCamera.ScreenToWorldPoint(new Vector3(0, focusedCamera.pixelHeight, focusedCamera.nearClipPlane));
        bottomLeft = focusedCamera.ScreenToWorldPoint(new Vector3(0, 0, focusedCamera.nearClipPlane)) ;
        Vector3 offset = (focusedCamera.transform.forward * screenSurfaceDepth);

        if(displayScreenInformation){
            Gizmos.color = Color.blue;

            if(displayTextInformation)
                Gizmos.DrawIcon((topLeft + bottomRight) * 0.5F, "CloseClippingPlane.png");

            Gizmos.DrawLine(topLeft, topRight);
            Gizmos.DrawLine(topRight, bottomRight);
            Gizmos.DrawLine(bottomRight, bottomLeft);
            Gizmos.DrawLine(bottomLeft, topLeft);

            Gizmos.color = Color.cyan;

            if(displayTextInformation)
                Gizmos.DrawIcon(((topLeft + offset) + (bottomRight + offset)) * 0.5F, "ScreenSpace.png");

            Gizmos.DrawLine(topLeft + offset, topRight + offset);
            Gizmos.DrawLine(topRight + offset, bottomRight + offset);
            Gizmos.DrawLine(bottomRight + offset, bottomLeft + offset);
            Gizmos.DrawLine(bottomLeft + offset, topLeft + offset);
        }

        if(displayGuideInformation){
            Gizmos.color = Color.yellow;
            drawFlatForwardLine(bottomLeft + offset, 1);
            drawFlatForwardLine(bottomRight + offset, 1);
            drawLineFromCameraPastPoint(topLeft, 1);
            drawLineFromCameraPastPoint(topRight, 1);
            drawLineFromCameraPastPoint(bottomLeft, 1);
            drawLineFromCameraPastPoint(bottomRight, 1);

            drawFlatForwardLine(slidePointDownCameraEdge(bottomLeft, floorDepth), 1);
            drawFlatForwardLine(slidePointDownCameraEdge(bottomRight, floorDepth), 1);
        }

        if(displayFloorInformation){
            Gizmos.color = FloorColor;
            drawFloorExample(slidePointDownCameraEdge(bottomRight, floorDepth), slidePointDownCameraEdge(bottomLeft, floorDepth), 1);
        }
    }
開發者ID:jacthoma,項目名稱:Project-Elephant,代碼行數:50,代碼來源:DebugBounds.cs

示例8: Start

	// Use this for initialization
	void Start () {
        Player = GameObject.FindWithTag("Player");
        rb = GetComponent<Rigidbody>();
        camera = Camera.main;
        Debug.Log("Yo Camera Bound");

        var bottomLeft = camera.ScreenToWorldPoint(Vector3.zero);
        var topRight = camera.ScreenToWorldPoint(new Vector3(
            camera.pixelWidth, camera.pixelHeight));
        cameraRect = new Rect(
            bottomLeft.x,
            bottomLeft.y,
            topRight.x - bottomLeft.x,
            topRight.y - bottomLeft.y);
    }
開發者ID:MillanAir,項目名稱:SimulationDesignA3,代碼行數:16,代碼來源:CameraBounds.cs

示例9: Update

 void Update()
 {
     cam = Camera.main;
     mousePosition = Input.mousePosition;
     mousePosition = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, Input.mousePosition.z - cam.transform.position.z));
     transform.eulerAngles = new Vector3(0,0,Mathf.Atan2((mousePosition.y - transform.position.y), (mousePosition.x - transform.position.x))*Mathf.Rad2Deg);
 }
開發者ID:ZujinD,項目名稱:TerrorForm,代碼行數:7,代碼來源:Attack.cs

示例10: Start

	void Start ()
	{
		mainCamera = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<Camera> ();
		upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
		upperCorner = mainCamera.ScreenToWorldPoint(upperCorner);
		//Debug.Log ("upper corner: " + upperCorner);
	}
開發者ID:SuperGameJammers,項目名稱:SuperColdRevenge,代碼行數:7,代碼來源:DestroyByBoundary.cs

示例11: splitByLine

    public GameObject[] splitByLine(GameObject target, Camera camera, Vector3 _start, Vector3 _end, bool destroyOriginal)
    {
        Vector3 targetPositionRelativeToCamera = camera.transform.worldToLocalMatrix.MultiplyPoint3x4( target.transform.position );

        _start.z = targetPositionRelativeToCamera.z;
        _end.z = targetPositionRelativeToCamera.z;

        Vector3 _middle = (_start + _end) / 2f;
        _middle.z *= 2f;

        Vector3 start = camera.ScreenToWorldPoint(_start);
        Vector3 middle = camera.ScreenToWorldPoint(_middle);
        Vector3 end = camera.ScreenToWorldPoint(_end);

        return splitByTriangle(target, new[] { start, middle, end }, destroyOriginal );
    }
開發者ID:VirsixInc,項目名稱:CatapultKing,代碼行數:16,代碼來源:PublicScriptInterface.cs

示例12: GeneratePlanes

    private void GeneratePlanes()
    {
        cameraComponent = sceneCamera.GetComponent<Camera>();

        screenHeightInWorldUnits = cameraComponent.orthographicSize * 2;
        screenWidthInWorldUnits = screenHeightInWorldUnits * cameraComponent.aspect;

        for (int i = 0; i < numberOfHorizontalRectangles; i++)
            for (int j = 0; j < numberOfVerticalRectangles; j++)
            {
                Transform plane = GameObject.Instantiate(planePrefab);
                plane.GetComponent<Renderer>().material.color = new Color(0, 0, 0);

                //Set rotation
                plane.rotation = Quaternion.Euler(-90, 0, 0);

                //Set position
                float horizontalSpacing = cameraComponent.pixelWidth / numberOfHorizontalRectangles;
                float verticalSpacing = cameraComponent.pixelHeight / numberOfVerticalRectangles;
                plane.position = cameraComponent.ScreenToWorldPoint(new Vector3(horizontalSpacing * 0.5f + horizontalSpacing * i,
                                                                                          verticalSpacing * 0.5f + verticalSpacing * j,
                                                                                          1));

                //Set scale
                plane.localScale = new Vector3(1 * cameraComponent.aspect / numberOfHorizontalRectangles - spacingBetweenRectangles / 2,
                                                         1,
                                                         1f / numberOfVerticalRectangles - spacingBetweenRectangles / 2);

                planes.Add(plane);
            }
    }
開發者ID:RockyMaxStudios,項目名稱:TileGameUnity,代碼行數:31,代碼來源:Grid.cs

示例13: Draw

        public override int Draw(Microsoft.Xna.Framework.Graphics.GraphicsDevice device, Camera cam)
        {
            if (ApplicationSettings.ShowDebugLines)
            {
                Debug.DrawLines(device, cam);
            }
            if (ApplicationSettings.ShowDebugPhysics)
            {
                Matrix proj = cam.projectionMatrix;
                Matrix view = cam.view;
                if (rayCastPlane.HasValue)
                {
                    if (rayCastPlane.Value.normal == Vector3.up)
                    {
                        view = Matrix.CreateRotationX(MathHelper.ToRadians(90)) * view;
                    }
                }

                physicsDebugView.RenderDebugData(ref proj, ref view, b => dontDrawColliders.Contains(b.UserData));

                // NOTE: This only works for XY plane stuff
                PressPlay.FFWD.Vector3 inPos = Input.mousePosition;
                inPos.z = cam.nearClipPlane;
                PressPlay.FFWD.Vector2 castPos = cam.ScreenToWorldPoint(inPos);
                if (rayCastPlane.HasValue)
                {
                    float dist;
                    Ray ray = Camera.main.ScreenPointToRay(inPos);
                    if (rayCastPlane.Value.Raycast(ray, out dist))
                    {
                        Vector3 pt = ray.GetPoint(dist);
                        castPos = new Vector2(pt.x, pt.z);
                    }
                    else
                    {
                        castPos = Vector2.zero;
                    }
                }
                Debug.Display("Mouse / Physics", inPos + " / " + castPos);

                RaycastHit[] hits = Physics.PointcastAll(castPos, cam.cullingMask);
                if (hits.Length > 0)
                {
                    Debug.Display("Over", String.Join("\n", hits.Select(h => h.collider.ToString()).OrderBy(s => s).ToArray()));
                }
                else
                {
                    Debug.Display("Over", "");
                }
            }

            if(ApplicationSettings.ShowDebugPhysicsCustom)
            {
                Physics.DrawDebug();
            }
            return 0;
        }
開發者ID:CodeHelix,項目名稱:FFWD,代碼行數:57,代碼來源:DebugCamera.cs

示例14: SetCameraBounds

    public static void SetCameraBounds(Camera cam = null)
    {
        if (cam == null) cam = Camera.main;

        Vector3 topLeft = new Vector3(0, 0, 0);
        Vector3 bottomRight = new Vector3(Screen.width, Screen.height, 0);

        Vector3 boundTLN = cam.ScreenToWorldPoint(topLeft);
        Vector3 boundBRF = cam.ScreenToWorldPoint(bottomRight);

        boundTLN.z += cam.nearClipPlane;
        boundBRF.z += cam.farClipPlane;

        Vector3 center = (boundTLN + boundBRF) / 2f;
        _camBounds = new Bounds(center, Vector3.zero);
        _camBounds.Encapsulate(boundBRF);
        _camBounds.Encapsulate(boundTLN);
    }
開發者ID:jeja,項目名稱:Project_1_CrashBandicoot,代碼行數:18,代碼來源:Utils.cs

示例15: GetRelativeMousePosition

    /// <summary>
    ///   get relative mouse position under parent node
    ///   the camera must be orthometric
    /// </summary>
    static public Vector3 GetRelativeMousePosition(Camera camera, Transform parent)
    {
        Vector3 v = camera.ScreenToWorldPoint(Input.mousePosition);//screen position to world position
        if (parent!=null)
            v = parent.InverseTransformPoint(v);//world position to local position

        v.z = 0;//flat z 
        return v;
    }
開發者ID:noa99kee,項目名稱:UniUIEffect,代碼行數:13,代碼來源:TransformUtility.cs


注:本文中的Camera.ScreenToWorldPoint方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。