Unity - Can I make a mirror in 2D?

限于喜欢 提交于 2019-12-11 15:16:41

问题


I am making a topdown tilebased 2D game. In this game, I want to make 1 of the walls into a mirror, like you can see on this video. Now, I know the game in the trailer is made in RPG Maker, but I want to make my game in Unity 3D.

I have tried to set a camera right next to the mirror, add a RenderTexture on the camera and set that texture on the Sprite, but of course it is not possible to convert a RenderTexture to a Sprite, so this did not end up working.

So my question is, is it possible to create a mirror like in the trailer?


回答1:


It is possible to get that effect. Just parent the second camera to your character and make it move with your character.

It is possible to convert RenderTexture to a Sprite. First of all, convert the RenderTexture to Texture2D then convert the Texture2D to Sprite with the Sprite.Create function.

It is better to disable the second or mirror camera then use mirrorCam.Render() to manually render it only when you need to. The script below should get you started. Attach it to an empty GameObject then assign the mirror camera and the target SpriteRenderer from the Editor and it should mirror the what the camera is seeing to the SpriteRenderer. Don't forget to plugin RenderTexture to the mirror camera.

public class CameraToSpriteMirror: MonoBehaviour
{
    public SpriteRenderer spriteToUpdate;
    public Camera mirrorCam;

    void Start()
    {
        StartCoroutine(waitForCam());
    }

    WaitForEndOfFrame endOfFrame = new WaitForEndOfFrame();

    IEnumerator waitForCam()
    {
        //Will run forever in this while loop
        while (true)
        {
            //Wait for end of frame
            yield return endOfFrame;

            //Get camera render texture
            RenderTexture rendText = RenderTexture.active;
            RenderTexture.active = mirrorCam.targetTexture;

            //Render that camera
            mirrorCam.Render();

            //Convert to Texture2D
            Texture2D text = renderTextureToTexture2D(mirrorCam.targetTexture);

            RenderTexture.active = rendText;

            //Convert to Sprite
            Sprite sprite = texture2DToSprite(text);

            //Apply to SpriteRenderer
            spriteToUpdate.sprite = sprite;

        }
    }

    Texture2D renderTextureToTexture2D(RenderTexture rTex)
    {
        Texture2D tex = new Texture2D(rTex.width, rTex.height, TextureFormat.RGB24, false);
        tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
        tex.Apply();
        return tex;
    }

    Sprite texture2DToSprite(Texture2D text2D)
    {
        Sprite sprite = Sprite.Create(text2D, new Rect(0, 0, text2D.width, text2D.height), Vector2.zero);
        return sprite;
    }
}



回答2:


You could do it the good old Super Mario 64 way and have the wall be a screen that shows another camera's perspective of another character.

Unity is pretty good at PIP (Picture In Picture) from what I've heard, so may be worth a shot.



来源:https://stackoverflow.com/questions/46687827/unity-can-i-make-a-mirror-in-2d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!