Beispiel #1
0
        /// <summary>Creates a new Flat World UI with the given pixels of space and a given name.
        /// The gameobjects origin sits at the middle of the UI by default. See <see cref="PowerUI.WorldUI.SetOrigin"/>.
        /// By default, 100 pixels are 1 world unit. See <see cref="PowerUI.WorldUI.SetResolution"/>.</summary>
        /// <param name="name">The name for the UI's gameobject.</param>
        /// <param name="widthPX">The width in pixels of this UI.</param>
        /// <param name="heightPX">The height in pixels of this UI.</param>
        public FlatWorldUI(string name, int widthPX, int heightPX) : base(name, widthPX, heightPX)
        {
            // It's a flat UI:
            Flat = true;

            // Create camera gameobject:
            CameraObject = new GameObject();

            CameraObject.name = name + "-#camera";

            // Parent the camera to the root:
            CameraObject.transform.parent        = gameObject.transform;
            CameraObject.transform.localPosition = Vector3.zero;

            // Add a camera:
            SourceCamera = CameraObject.AddComponent <Camera>();

            // Put it right at the back:
            SourceCamera.depth = -9999;

            // Set the clear flags:
            SourceCamera.clearFlags      = CameraClearFlags.Color;
            SourceCamera.backgroundColor = new Color(1f, 1f, 1f, 0f);

            // Make it forward rendered (it deals with transparency):
            SourceCamera.renderingPath = RenderingPath.Forward;

            float zSpace = UI.GetCameraDistance();

            // Setup the cameras distance:
            SetCameraDistance(zSpace);

            // Call the camera creation method:
            UI.CameraGotCreated(SourceCamera);

            // Make it orthographic:
            SourceCamera.orthographic = true;

            // Set the orthographic size:
            SetOrthographicSize();

            // Start our handler:
            Handler = CameraObject.AddComponent <FlatWorldUIHandler>();

            // And the location too:
            Handler.Location = new Rect(0, 0, widthPX, heightPX);

            // Apply aspect:
            Handler.Aspect = (float)widthPX / (float)heightPX;

            // Hook up the camera:
            Handler.Camera = SourceCamera;

            // Next it's time for the texture itself! We're going to try using a RenderTexture first.

            RenderTexture renderTexture = null;

                        #if UNITY_5_5_OR_NEWER
            // Create a render texture:
            renderTexture = new RenderTexture(widthPX, heightPX, 16, RenderTextureFormat.ARGB32);

            // Apply it to the texture:
            Texture = renderTexture;

            // Hook it up:
            SourceCamera.targetTexture = renderTexture;
                        #else
            if (SystemInfo.supportsRenderTextures)
            {
                // Create a render texture:
                renderTexture = new RenderTexture(widthPX, heightPX, 16, RenderTextureFormat.ARGB32);

                // Apply it to the texture:
                Texture = renderTexture;

                // Hook it up:
                SourceCamera.targetTexture = renderTexture;
            }
            else
            {
                // No RT support. Time to use our workaround instead!

                // Create the texture:
                Texture2D texture = new Texture2D(widthPX, heightPX);

                // Hook it up now:
                Handler.Output = texture;

                // Apply it:
                Texture = texture;
            }
                        #endif

            // Change the layer of the gameobject and also the camera.

            // Set the culling mask:
            if (DefaultLayer == -1)
            {
                Layer = UI.Layer;
            }
            else
            {
                Layer = DefaultLayer;
            }

            gameObject.transform.position = new Vector3(0f, -150f, GlobalOffset);
            GlobalOffset += zSpace + 1f;
        }