private static void createCameraAndRendertextureFor(AnyUiMesh tTarget)
        {
            //configure canvas
            Canvas canvasToProject = tTarget.CanvasToProject;

            canvasToProject.renderMode = RenderMode.WorldSpace;
            RectTransform canvasRectangle = canvasToProject.GetComponent <RectTransform>();
            //replace Graphic raycaster
            AnyUiCanvas currentReceiver = canvasToProject.GetComponent <AnyUiCanvas>();

            if (currentReceiver == null)
            {
                GraphicRaycaster oldReceiver          = canvasToProject.GetComponent <GraphicRaycaster>();
                AnyUiCanvas      newReceiverRayCaster = canvasToProject.gameObject.AddComponent <AnyUiCanvas>();
                newReceiverRayCaster.ignoreReversedGraphics = oldReceiver.ignoreReversedGraphics;
                newReceiverRayCaster.blockingObjects        = oldReceiver.blockingObjects;
                DestroyImmediate(oldReceiver);
            }

            RenderTexture canvasToTexture = null;
            Camera        canvasCam       = null;

            //create camera and render texture if necessary
            if (canvasToProject.worldCamera == null)
            {
                //create camera looking at canvas
                canvasCam = new GameObject().AddComponent <Camera>();
                //editor: make camera invisible
                canvasCam.transform.parent = canvasToProject.transform;
                //if canvasToProject is scaled, adapt!
                canvasCam.transform.localScale = Vector3.one;

                canvasCam.name            = "AnyUI_CanvasCam" + canvasToProject.gameObject.name + System.Guid.NewGuid().ToString();
                canvasCam.clearFlags      = CameraClearFlags.SolidColor;
                canvasCam.backgroundColor = Color.clear;
                canvasCam.cullingMask     = 1 << canvasToProject.gameObject.layer;
                canvasCam.depth           = -1;

                canvasCam.orthographic     = true;
                canvasCam.orthographicSize = canvasRectangle.rect.height * canvasRectangle.localScale.y / 2.0f; //vertical
                canvasCam.nearClipPlane    = 0.9f;
                canvasCam.farClipPlane     = 1.1f;
                canvasCam.projectionMatrix = Matrix4x4.Ortho(-canvasRectangle.rect.width * canvasRectangle.localScale.x / 2.0f,
                                                             canvasRectangle.rect.width * canvasRectangle.localScale.x / 2.0f,
                                                             -canvasRectangle.rect.height * canvasRectangle.localScale.y / 2.0f,
                                                             canvasRectangle.rect.height * canvasRectangle.localScale.y / 2.0f,
                                                             0.5f, 1.5f);

                canvasCam.transform.localPosition = Vector3.zero;
                canvasCam.transform.Translate(-canvasRectangle.forward, Space.World);
                canvasCam.transform.rotation = canvasRectangle.rotation;

                canvasToProject.worldCamera = canvasCam;
            }
            else
            {
                canvasCam = canvasToProject.worldCamera;
            }

            if (canvasToProject.worldCamera.targetTexture == null)
            {
                //new RenderTexture
                canvasToTexture      = new RenderTexture((int)tTarget.ProjectionResolution, (int)((float)tTarget.ProjectionResolution * (canvasRectangle.rect.height / canvasRectangle.rect.width)), 0);
                canvasToTexture.name = "AnyUI_RenderTexture" + canvasToProject.gameObject.name + System.Guid.NewGuid().ToString();
                if (!AssetDatabase.IsValidFolder("Assets/AnyUI/AnyUIRenderTexturesAndMaterials"))
                {
                    AssetDatabase.CreateFolder("Assets/AnyUI", "AnyUIRenderTexturesAndMaterials");
                }
                AssetDatabase.CreateAsset(canvasToTexture, "Assets/AnyUI/AnyUIRenderTexturesAndMaterials/" + canvasToTexture.name + ".renderTexture");
                canvasCam.targetTexture = canvasToTexture;
            }
            else
            {
                canvasToTexture = canvasCam.targetTexture;
            }
        }
示例#2
0
 protected override void Start()
 {
     base.Start();
     receiver = CanvasToProject != null?CanvasToProject.GetComponent <AnyUiCanvas>() : null;
 }