protected override void OnInstanceInit() { mCam = GetComponentInChildren <M8.Camera2D>(); var unityCam = mCam.unityCamera; //setup view bounds var minExt = unityCam.ViewportToWorldPoint(Vector3.zero); var maxExt = unityCam.ViewportToWorldPoint(new Vector3(1f, 1f, 0f)); var mtxToLocal = transform.worldToLocalMatrix; var minExtL = mtxToLocal.MultiplyPoint3x4(minExt); var maxExtL = mtxToLocal.MultiplyPoint3x4(maxExt); mCamViewBounds = new Bounds( new Vector2(Mathf.Lerp(minExtL.x, maxExtL.x, 0.5f), Mathf.Lerp(minExtL.y, maxExtL.y, 0.5f)), new Vector2(Mathf.Abs(maxExtL.x - minExtL.x), Mathf.Abs(maxExtL.y - minExtL.y))); // }
public void Refresh() { if (_cameraAttach == null) { return; } #if UNITY_EDITOR mCamera2D = _cameraAttach.GetComponent <Camera2D>(); #endif Transform t = transform; Vector3 pos = t.position; float scale = 1.0f; Rect r; if (mCamera2D != null && _cameraAttach.orthographic) { r = useFixedBound ? mCamera2D.fixedScreenExtent : mCamera2D.screenExtent; scale = mCamera2D.getPixelSize(1.0f); } else { r = _cameraAttach.pixelRect; } Vector3 anchorPos = Vector3.zero; switch (anchor) { case Anchor.Left: anchorPos.x = r.xMin; anchorPos.y = r.center.y; break; case Anchor.Right: anchorPos.x = r.xMax; anchorPos.y = r.center.y; break; case Anchor.Top: anchorPos.x = r.center.x; anchorPos.y = r.yMax; break; case Anchor.Bottom: anchorPos.x = r.center.x; anchorPos.y = r.yMin; break; case Anchor.Center: Vector2 c = r.center; anchorPos.x = c.x; anchorPos.y = c.y; break; case Anchor.TopLeft: anchorPos.x = r.xMin; anchorPos.y = r.yMax; break; case Anchor.TopRight: anchorPos.x = r.xMax; anchorPos.y = r.yMax; break; case Anchor.BottomLeft: anchorPos.x = r.xMin; anchorPos.y = r.yMin; break; case Anchor.BottomRight: anchorPos.x = r.xMax; anchorPos.y = r.yMin; break; } anchorPos.x += offset.x * scale; anchorPos.y += offset.y * scale; Vector3 newPos = mCamera2D != null ? mCamera2D.transform.position + anchorPos : _cameraAttach.ScreenToWorldPoint(anchorPos); newPos.z = pos.z; newPos.z = pos.z; if (pos != newPos) { t.position = newPos; } }
public override void OnInspectorGUI() { Camera2D cam = target as Camera2D; Camera unityCam = cam.camera; //resolution GUILayout.BeginHorizontal(); Vector2 res = new Vector2(cam.fixedResolutionWidth, cam.fixedResolutionHeight); res = EditorGUILayout.Vector2Field("Resolution", res); cam.fixedResolutionWidth = Mathf.RoundToInt(res.x); cam.fixedResolutionHeight = Mathf.RoundToInt(res.y); GUILayout.EndHorizontal(); int camProjInd = unityCam.orthographic ? 0 : 1; camProjInd = EditorGUILayout.IntPopup("Projection", camProjInd, new string[] { "Orthographic", "Perspective" }, new int[] { 0, 1 }); switch (camProjInd) { case 0: unityCam.orthographic = true; cam.usePixelPerMeter = EditorGUILayout.Toggle("Use Pixel Per Meter", cam.usePixelPerMeter); if (cam.usePixelPerMeter) { float ppm = EditorGUILayout.FloatField("Pixel/Meter", cam.pixelPerMeter); cam.pixelPerMeter = Mathf.Max(0.001f, ppm); } else { float o = EditorGUILayout.FloatField("Size", cam.orthographicSize); cam.orthographicSize = Mathf.Max(0.001f, o); } break; case 1: unityCam.orthographic = false; cam.fov = EditorGUILayout.Slider("FOV", cam.fov, 1.0f, 179.0f); cam.transparencySortMode = (TransparencySortMode)EditorGUILayout.EnumPopup("Sort Mode", cam.transparencySortMode); break; } cam.viewRect = EditorGUILayout.RectField("Viewport Rect", cam.viewRect); if (unityCam.orthographic) { cam.scaleMode = (Camera2D.ScaleMode)EditorGUILayout.EnumPopup("Scale Mode", cam.scaleMode); if (cam.scaleMode == Camera2D.ScaleMode.None) { float ps = EditorGUILayout.FloatField("Pixel Scale", cam.pixelScale); cam.pixelScale = Mathf.Max(0.001f, ps); } cam.origin = (Camera2D.Origin)EditorGUILayout.EnumPopup("Origin", cam.origin); cam.usePixelOffset = EditorGUILayout.Toggle("Use Pixel Offset", cam.usePixelOffset); if (cam.usePixelOffset) { cam.pixelOffset = EditorGUILayout.Vector2Field("Pixel Offset", cam.pixelOffset); } cam.useClipping = EditorGUILayout.Toggle("Use Clipping", cam.useClipping); if (cam.useClipping) { cam.clipRegion = EditorGUILayout.RectField("Clip Region", cam.clipRegion); } } cam.zoom = EditorGUILayout.FloatField("Zoom", cam.zoom); }