Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Ross_B · Nov 22, 2013 at 09:21 PM · renderingrendertextureeditorwindow

Rendering a Camera in an Editor window displays its RenderTexture automatically?

After a few hours of experimenting with ReadPixels() and GUI.DrawTexture() to render a Camera to an EditorWindow I was frustrated and not getting anywhere. There were 2 Textures being rendered! So I deleted all of the calls to the above mentioned functions and it works perfectly.

Now I'm no expert in magic, but this looks like magic to me. Here is the script:


 using UnityEditor;
 using UnityEngine;
 using System.Collections;
 
 public class CollisionPainter : EditorWindow 
 {
     //game object that holds the camera
     private GameObject gameObject;
 
     //direct reference to the game objects camera
     private Camera cam;
 
     static CollisionPainter window;
 
     [MenuItem("Custom Windows/Collision Painter")]
     static void ShowWindow() 
     {
         window = 
             (CollisionPainter)EditorWindow.GetWindow(typeof(CollisionPainter));
         window.init();
     }
 
     public void init()
     {
         //set game object's attribs
         //====================================================================
         gameObject = new GameObject("Collision Painter Camera");
         gameObject.transform.position = 
             new Vector3( 0.0f, 0.0f ,
                         GameObject.FindGameObjectWithTag("MainCamera")
                         .transform.position.z);
         gameObject.AddComponent<Camera>();
 
         //set camera's attribs
         //===================================================================
         cam = gameObject.camera;
         cam.backgroundColor = Color.green;
         cam.enabled = false;
         cam.orthographic = true;
         cam.orthographicSize = 
             GameObject.FindGameObjectWithTag("MainCamera")
                 .GetComponent<Camera>().orthographicSize;
     }
     
     void OnGUI() 
     {   
 
         cam.targetTexture = new RenderTexture((int)window.position.width,
                                               (int)window.position.height,24,
                                               RenderTextureFormat.ARGB32);
         cam.Render();
     }
     
     void OnDestroy()
     {
         DestroyImmediate(gameObject);
     }
 }


And Its resulting Window:

alt text

Is this normal behavior? How is the RenderTexture being displayed in the window?

thanks - Ross.

magic.jpg (48.4 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadowM8 · Feb 23, 2015 at 07:16 AM 0
Share

I saw this as well, don't quite understand why... were you able to figure it out?

avatar image Ross_B · Feb 23, 2015 at 04:25 PM 0
Share

Sorry... I wasnt. I Just ended up accepting it and moving on. I'm still curious however. So if anyone has figured it out I would love to know.

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by DStrike · Oct 28, 2015 at 01:20 PM

This is magic and it solved a problem I was stuck on for 6 hours =)

I did have to make some changes in the code for it to work on 5.1

I added

GUI.DrawTexture(new Rect(0, 0, position.width, position.height), cam.targetTexture);

and changed out the deprecated call to the camera with the current .GetComponent(); call

 using UnityEditor;
 using UnityEngine;
 using System.Collections;
 using Assets.Editor;
 
 public class CollisionPainter : EditorWindow
 {
     //game object that holds the camera
     private GameObject gameObject;
 
     //direct reference to the game objects camera
     private Camera cam;
 
     static CollisionPainter window;
 
     [MenuItem("Custom Windows/Collision Painter")]
     static void ShowWindow()
     {
         window =
             (CollisionPainter)EditorWindow.GetWindow(typeof(CollisionPainter));
         window.init();
     }
 
     public void init()
     {
         //set game object's attribs
         //====================================================================
         gameObject = new GameObject("Collision Painter Camera");
         gameObject.transform.position =
             new Vector3(0.0f, 0.0f,
                         GameObject.FindGameObjectWithTag("MainCamera")
                         .transform.position.z);
         gameObject.AddComponent<Camera>();
 
         //set camera's attribs
         //===================================================================
         cam = gameObject.GetComponent<Camera>();//.camera;
         cam.backgroundColor = Color.green;
         cam.enabled = false;
         cam.orthographic = true;
         cam.orthographicSize =
             GameObject.FindGameObjectWithTag("MainCamera")
                 .GetComponent<Camera>().orthographicSize;
     }
 
     void OnGUI()
     {
         if (window == null)
         {
             window =
             (CollisionPainter)EditorWindow.GetWindow(typeof(CollisionPainter));
         }
 
         cam.targetTexture = new RenderTexture((int)window.position.width,
                                               (int)window.position.height, 24,
                                               RenderTextureFormat.ARGB32);        
 
         cam.Render();
 
         GUI.DrawTexture(new Rect(0, 0, position.width, position.height), cam.targetTexture);        
     }
 
     void OnDestroy()
     {
         DestroyImmediate(gameObject);
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ross_B · Oct 28, 2015 at 05:39 PM 0
Share

I'm glad whatever was here helped :) Am I correct in saying that in 5.1 the editor window is no longer being drawn to without a call to GUI.DrawTexture()?. Can we assume it was unintended then?

avatar image DStrike Ross_B · Oct 28, 2015 at 06:21 PM 0
Share

Yeah the window didn't draw anything until I added that. It seems to imply what you inferred that it wasn't intentional. I was able to figure out that a texture was being recorded from the camera so then it was trivial to just draw it. None of the other things I tried even got the texture

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ClearFlags don't seem to work when I manually call Render() on my cameras. 0 Answers

Image-Based Lighting refreshing 0 Answers

Speeding up reading from a RenderTexture 3 Answers

Is there a way to write to Deferred Depth from a command buffer in CameraEvent.AfterImageEffects? 1 Answer

RenderTexture on RawImage does not render anything 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges