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
1
Question by Sprakle · Aug 18, 2013 at 03:19 AM · debugassembly

Can't call custom Debug class (assembly issue?)

I'm working on an improved Debug console, but am unable to call any methods on it. Here is a simplified version of the class:

 using Assets.Editor;
 using UnityEditor;
 using UnityEngine;
 
 public class Logger : EditorWindow
 {
     [MenuItem("Window/Logger")]
     public static void Init()
     {
         Logger window = (Logger)GetWindow(typeof(Logger), false, "Logger");
         window.position = new Rect(window.position.xMin + 100f, window.position.yMin + 100f, 200f, 400f);
     }
 
     public static void Log(string msg)
     {
         
     }
 }

If I have a class that calls Logger.Log("something"), unity reports that 'Logger' does not exist in the current context. Visual Studio however does not find an error with any of my code. This code will only need to work when in the Unity editor.

So how can I access Logger from standard classes?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Aug 18, 2013 at 03:36 AM

Since you guess an assembly issue this class is compiled in a seperate assembly? right? Where do you have this assembly in your project? And where do you try to use it? You know that this is an editor script and therefore can only be used in the editor. The class won't exist at runtime since the assembly isn't included (since it's an editor assembly).

Comment
Add comment · Show 4 · 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 Sprakle · Aug 18, 2013 at 03:38 AM 0
Share

Then how does Unity's build in Debug class work? You can call it at runtime, and results are displayed in a editor window.

Is there any way to communicate between the editor and normal assemblies?

avatar image Bunny83 · Aug 18, 2013 at 03:58 AM 0
Share

The Debug class is a runtime class. Also all debug logs can be intercepted with Application.RegisterLogCallback or Application.RegisterLogCallbackThreaded at runtime. The editor probably just "hangs in". However keep in $$anonymous$$d that when testing in the editor the editor environment is the actual one.

I've also created a custom logging class (as part of my mobile debug system), but $$anonymous$$e has the exact opposite aim ;) It should work at runtime so i can see all Debug.Logs and my custom logs on screen.

There's no way to directly call / use an editor assembly from runtime. However all editor classes have full access to all runtime classes. You can see the runtime as a kind of subdomain of the editor domain.

Just to explain this in detail:
The editor assemblies are actually loaded in the same domain as the runtime classes when you're in the editor, however the editor assemblies are not included as references when compiling the runtime classes, therefore the compiling fails because the class you're try to use is missing.

In general: All code in runtime classes have to work with only the runtime available. In runtime classes you can use

 #if UNITY_EDITOR
 //Do some editor stuff
 #endif

To use built-it editor functions, but not custom assemblies.

avatar image Bunny83 · Aug 18, 2013 at 03:59 AM 0
Share

I've converted my comment into an answer ;) (and moved the comments)

avatar image Sprakle · Aug 18, 2013 at 04:09 AM 0
Share

I figured out a solution just before you posted, and wrote my own answer (still needs to be approved by a moderator).

It uses a surrogate class in the main assembly that an editor class can access and display the contents of. (Like your answer says can be done)

avatar image
1

Answer by Sprakle · Aug 18, 2013 at 04:06 AM

I was able to get this working using a surrogate class within the main assembly.

  • A LoggerUI class (Logger in the original question) is in the editor assembly, and cannot be accessed by any other classes.

  • A static Logger class (The surrogate) is in the main assembly. It has has logging methods, and saves all logs in a list.

  • The LoggerUI class then calls GetList() on the Logger class every frame (because the editor assembly is able to access the main one), to update and draw the logged lines.

The list is only updated once every frame. Although this will result in good performance (you can call Logger.Log() many times per frame), when the game crashes, the list might not be as up to date as it should be.

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 Bunny83 · Aug 18, 2013 at 04:18 AM 1
Share

I'm not sure what you're after but if you want a logger without any delays you can provide a callback event in your surrogate class to which your editor class can subscribe. Of course if the game and the editor crashes those lines won't be displayed since even Unity's console accumulates logs and displays them only at intervals.

However if you write out your own logfile from the callback it will be updated in realtime. Just make sure that this can be disabled for the release version.

avatar image Sprakle · Aug 18, 2013 at 04:40 AM 0
Share

@bunny83

Great idea, I'll probably do something along those lines.

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

16 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

Related Questions

How to view c# assembly code in Visual Studio 2019? 1 Answer

Is it possible to extend the playing and debugging process? 0 Answers

Random Blank Loaded Scene 1 Answer

Debug an occasional freeze 0 Answers

Hey !! i am taking a screenshot in one of my app that shot is saved in (android/data/app/shot) by default can i change its path directly to gallary? if yes thn how ? 0 Answers


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