// 由于摄像机动态加载的时候名字会改变,所以需要重新设置ui需要的摄像机 [1/12/2012 Ivan] void Start() { Camera uiCamera = null; foreach (Camera camera in Camera.allCameras) { if (camera.cullingMask == LayerManager.UIMask) { uiCamera = camera; break; } } if (!uiCamera) { return; } GameObject goUi = GameObject.Find("UIManager"); if (goUi != null) { UIManager uiManager = goUi.GetComponent <UIManager>(); uiManager.rayCamera = uiCamera; EZCameraSettings[] ezCamera = new EZCameraSettings[1]; ezCamera[0] = new EZCameraSettings(); ezCamera[0].camera = uiCamera; ezCamera[0].mask = 1 << 10; uiManager.uiCameras = ezCamera; } CEventSystem.Instance.RegisterEventHandle(GAME_EVENT_ID.GE_APPLICATION_INITED, OnUIStart); }
/// <summary> /// Gets the screen position of a particular UI object whose transformation is given. /// </summary> /// <param name="trans">The transformation of the UI object to locate</param> /// <returns>The screen position of the UI object</returns> public static Vector3 GetPosition(Transform trans) { var uiCams = UIManager.instance.uiCameras; EZCameraSettings uiCam = null; for (int i = 0; i < uiCams.Length; i++) { if ((uiCams[i].mask & (1 << trans.gameObject.layer)) != 0) { uiCam = uiCams[i]; break; } } Vector3 screenPos = uiCam.camera.WorldToScreenPoint(trans.position); screenPos.y = Screen.height - screenPos.y; return(screenPos); }
/// <summary> /// Adds the specified camera to the UI Cameras list at the specified index. /// </summary> /// <param name="cam">The camera to be added.</param> /// <param name="mask">The layer mask for the camera.</param> /// <param name="depth">The depth into the scene the pointer should reach.</param> /// <param name="index">The index in the list where the camera should be added. /// Note that cameras higher on the list (at a lower index value) process input before /// cameras later in the list.</param> public void AddCamera(Camera cam, LayerMask mask, float depth, int index) { EZCameraSettings[] cams = new EZCameraSettings[uiCameras.Length + 1]; // Keep the index in a valid range: index = Mathf.Clamp(index, 0, uiCameras.Length + 1); for (int i = 0, src = 0; i < uiCameras.Length; ++i, ++src) { if (i == index) { cams[i] = new EZCameraSettings(); cams[i].camera = cam; cams[i].mask = mask; cams[i].rayDepth = depth; ++src; } else cams[i] = uiCameras[src]; } uiCameras = cams; SetupPointers(); }
/// <summary> /// Removes the camera at the specified index from the UI Cameras /// array. /// </summary> /// <param name="index">The index of the camera that should be removed.</param> public void RemoveCamera(int index) { EZCameraSettings[] cams = new EZCameraSettings[uiCameras.Length - 1]; // Keep the index in a valid range: index = Mathf.Clamp(index, 0, uiCameras.Length); for (int i = 0, src = 0; i < uiCameras.Length; ++i, ++src) { if (i == index) { // Skip this one ++src; } else cams[i] = uiCameras[src]; } uiCameras = cams; SetupPointers(); }
/// <summary> /// Removes the camera at the specified index from the UI Cameras /// array. /// </summary> /// <param name="index">The index of the camera that should be removed.</param> public void RemoveCamera(int index) { EZCameraSettings[] cams = new EZCameraSettings[uiCameras.Length - 1]; // Keep the index in a valid range: index = Mathf.Clamp(index, 0, uiCameras.Length); for (int dst = 0, src = 0; src < uiCameras.Length; src++) { if (src != index) { cams[dst] = uiCameras[src]; dst++; } } uiCameras = cams; SetupPointers(); }