private void UpdateBoomIconPos() { Vector3 hitPoint = Vector3.zero; RaycastHit raycastHit; Vector3 pos = this.gunTransform.position; Ray ray = new Ray(pos, this.gunTransform.forward); hitPoint = Physics.Raycast(ray, out raycastHit, 3000f)? raycastHit.point : ray.GetPoint(3000f); Debug.DrawLine(ray.origin, hitPoint, Color.red);//划出射线,在scene视图中能看到由摄像机发射出的射线 Vector3 screenPoint = Camera.main.WorldToScreenPoint(hitPoint); UIContentScaler scaler = Stage.inst.gameObject.GetComponent <UIContentScaler>(); screenPoint.x = (scaler.designResolutionX * 1.0f) / Screen.width * screenPoint.x; screenPoint.y = (scaler.designResolutionY * 1.0f) / Screen.height * screenPoint.y; UpdatePos?.Invoke(screenPoint.x, screenPoint.y); }
public static GameObject CreatePanel() { GameObject panelObj = new GameObject("QuestionMainPanel"); panelObj.layer = LayerMask.NameToLayer("UI"); UIPanel panel = panelObj.AddComponent <UIPanel>(); panel.packageName = "QuesPart"; panel.componentName = "Component1"; panel.fitScreen = FitScreen.FitSize; panel.SetSortingOrder(10, true); panel.CreateUI(); UIContentScaler contentScaler = panelObj.AddComponent <UIContentScaler>(); contentScaler.designResolutionX = 1136; contentScaler.designResolutionY = 640; contentScaler.scaleMode = UIContentScaler.ScaleMode.ScaleWithScreenSize; contentScaler.screenMatchMode = UIContentScaler.ScreenMatchMode.MatchWidthOrHeight; QuestionMainPanel wp = panelObj.AddComponent <QuestionMainPanel>(); return(panelObj); }
/// <summary> /// 初始化 /// </summary> protected override void OnCreate() { if (uiPanelGo == null) { uiPanelGo = new GameObject("UIPanel"); uiPanelGo.layer = LayerMask.NameToLayer(StageCamera.LayerName); DontDestroyOnLoad(uiPanelGo); //GRoot.inst.SetContentScaleFactor(WIDTH, HEIGHT); scaler = uiPanelGo.AddComponent <UIContentScaler>(); scaler.designResolutionX = WIDTH; scaler.designResolutionY = HEIGHT; scaler.scaleMode = UIContentScaler.ScaleMode.ScaleWithScreenSize; // ConstantPixelSize;// //在 ScaleMode.ScaleWithScreenSize 时使用-------------------------------------------------------- int dx = scaler.designResolutionX; int dy = scaler.designResolutionY; if (Screen.width > Screen.height && dx < dy || Screen.width < Screen.height && dx > dy) { int tmp = dx; dx = dy; dy = tmp; } contentScaleX = (float)Screen.width / dx; contentScaleY = (float)Screen.height / dy; //--------------------------------------------------------------------------------------------- UIPanel panel = uiPanelGo.AddComponent <UIPanel>(); panel.fitScreen = FitScreen.FitSize; } }
void OnScreenSizeChanged(int newWidth, int newHeight) { if (newWidth == 0 || newHeight == 0) { return; } screenWidth = newWidth; screenHeight = newHeight; float upp; if (constantSize) { cachedCamera.orthographicSize = DefaultCameraSize; upp = cachedCamera.orthographicSize * 2 / screenHeight; } else { upp = 0.02f; cachedCamera.orthographicSize = screenHeight / 2 * UnitsPerPixel; } cachedTransform.localPosition = new Vector3(cachedCamera.orthographicSize * screenWidth / screenHeight, -cachedCamera.orthographicSize); if (isMain) { UnitsPerPixel = upp; screenSizeVer++; if (Application.isPlaying) { Stage.inst.HandleScreenSizeChanged(); } else { UIContentScaler scaler = CentorPivot.This.GetComponent <UIContentScaler>(); if (scaler != null) { scaler.ApplyChange(); } else { UIContentScaler.scaleFactor = 1; } } } }