坐标转换
已知云端坐标点A(10,10),需要转换到本地坐标轴下的位置 B?
可能的问题
参考
Unity中内置转换
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| using UnityEngine;
public static class CTUtils { public static Vector2 World2Screen(Vector3 wp, Camera camera = null) { if (camera == null) { camera = Camera.main; } return camera.WorldToScreenPoint(wp); } public static Vector3 Screen2World(Vector3 sp, Camera camera = null) { if (camera == null) { camera = Camera.main; } return camera.ScreenToWorldPoint(sp); } public static Vector2 World2Viewport(Vector3 wp, Camera camera = null) { if (camera == null) { camera = Camera.main; } return camera.WorldToViewportPoint(wp); } public static Vector3 Viewport2World(Vector3 vp, Camera camera = null) { if (camera == null) { camera = Camera.main; } return camera.ViewportToWorldPoint(vp); } public static Vector2 Screen2Viewport(Vector2 sp, Camera camera = null) { if (camera == null) { camera = Camera.main; } return camera.ScreenToViewportPoint(sp); } public static Vector2 Viewport2Screen(Vector2 vp, Camera camera = null) { if (camera == null) { camera = Camera.main; } return camera.ViewportToScreenPoint(vp); } public static Vector2 Screen2UI(Vector2 sp, RectTransform rect, Camera camera = null) { Vector2 uiLocalPos; RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, sp, camera, out uiLocalPos); return uiLocalPos; } public static Vector2 World2UI(bool isUIObj, Vector3 wp, RectTransform rect, Camera uiCamera, Camera worldCamera = null) { if (worldCamera == null) { worldCamera = Camera.main; } Vector2 screenPos = World2Screen(wp, isUIObj ? uiCamera : worldCamera); return Screen2UI(screenPos, rect, uiCamera); } }
|