• 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
Question by ThomasODell · Apr 01, 2022 at 10:34 AM · editorinternalbridge

How to access useful Unity Editor/Engine internal methods?

I was trying to figure out how to manipulate a sprite image, so I looked at the code for the Sprite Editor. I noticed that it accessed some internal Unity Editor methods, including the one I really wanted: UnityEditor.SpriteUtility.CreateTemporaryDuplicate.

The Unity 2D methods seem to have no problem accessing these internal methods through an InternalBridge class. But when I copy over the code, including the assembly def's and assembly info's, I still get errors saying unable to access due to protection level.

Unity packages can do it, why can't I?

Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by sacredgeometry · Apr 01, 2022 at 10:01 PM

Decompile unity and recompile it with looser access modifiers?


Sounds like a bad idea? Yeah thats why they are internal. They arent meant to be used by you.

Comment
Bunny83

People who like this

1 Show 0 · 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

Answer by ThomasODell · Apr 09, 2022 at 06:43 AM

Okay, I figured out how Unity does it. If you look at the Visual Studio Assembly Browser for UnityEditor.CoreModule, you see that there are a number of [assembly: InternalsVisibleTo ()] directives. This includes Unity.InternalAPIEditorBridge001 up to Unity.InternalAPIEditorBridge024. (There are also Unity.InternalAPIEngineBridge001 up to (I think--it's harder to tell) Unity.InternalAPIEngineBridge024 for engine internals. )


The internal bridge class for plug-in com.unity.2d.common is in the DLL Unity.InternalAPIEditorBridge.001 according to its asmdef. So that's how Unity can access its internals from its plugins!


So, to access the Unity Internals (if you're desperate for them), you create an asmdef for an unused editor bridge DLL, and--presto!--you have the same access.


The problem would be in release code to figure out which internal bridges are already taken. Since I'm not making release code (only a plug-in for the editor that would be used in specific, controlled circumstances), that's not my concern.

Comment
forestrf

People who like this

1 Show 1 · 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 ThomasODell · Apr 09, 2022 at 06:46 AM 0
Share

(The better solution would be for Unity to move CreateTemporaryDuplicate() to the public API, hint, hint.)

avatar image

Answer by Bunny83 · Apr 09, 2022 at 08:02 AM

Uhm, you could always use reflection to access internal or private classes / methods / fields. There are only very few things you could not access through reflection. Of course you would create an unwanted dependency which may break in any future update since it's only meant for internal use.


Though the specific method you're talking about only uses public available APIs as far as I can tell. Note that the method goes through some detours to create a duplicate of the texture. If the texture is marked readable you should be able to simply instantiate the texture. You only would need to go though this convoluted process if the source texture is not readable. So it actually creates a temporary render texture, uses blit to copy the texture content on the GPU and then does a readback from the render texture into a new texture.

Comment

People who like this

0 Show 1 · 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 ThomasODell · Apr 09, 2022 at 08:08 AM 0
Share

Thanks @Bunny83. Yes, this method is only needed if the texture is not readable. Nice to find out how it works internally.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

182 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 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 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 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

SerializedPropertyTable in custom editor window 1 Answer

How can Internal variable get accessed from Editor script? 1 Answer

Can't sync vs project -1 Answers

Editor Window "Ghosts" appear on recompile 1 Answer

Scene changes not being reflected in standalone build 2 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