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
2
Question by $$anonymous$$ · Mar 27, 2013 at 09:00 PM · namespaceeditor-classes

Acces a script in the editor folder

Say for instance I need to access a function that is in a class in the editor folder like this:

 using UnityEngine;
 namespace MyNamepace //in Editor folder
 {
     public class myClass
     {
         public int Add(int int1, int int2)
         {
             return int1 + int2;
         }
     }
 }

script outside of editor folder:

 using UnityEngine;
 using MyNamespace;
 public class Test : MonoBehaviour
 {
     public void Awake()
     {
         int1 = 2;
         int2 = 2;
         int result = myClass.Add(int1, int2);
         print(result.ToString());
     }
 }

This gives a 'CS0246: The type or namespace name `MyNamespace' could not be found' but it works when MyNamespace is outside of the Editor folder, note that this is just an example script because I dont want to post a 10 line and a 600 line script here. Any ideas?

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
4
Best Answer

Answer by whydoidoit · Mar 27, 2013 at 09:03 PM

The editor files are compiled last and with a reference to everything else - however the non-editor files do not have a reference to the editor code which is in a separate (and unreferenced) assembly.

So you can't do that. Whatever needs to be referenced from both needs to be outside the Editor folder.

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 Unitraxx · Mar 27, 2013 at 09:29 PM 0
Share

See that's why I like to hang out on unity answers, $$anonymous$$ches me some things that might come in handy in the future. +1

avatar image astracat111 · Feb 16, 2019 at 12:56 AM 0
Share

The problem with this method is that Unity will undoubtedly have an update in where your names might change? I think....

It happened to me with using reflection at some point, although I can't recall where it was...

avatar image
3

Answer by a trained monkey · Nov 24, 2014 at 05:31 PM

This is possible with reflection. It obviously breaks encapsulation but I have required it for a resource manager as static constructors in the editor don't get re-ran on return from play mode.

 System.Reflection.Assembly editorAssembly = System.AppDomain.CurrentDomain.GetAssemblies().First( a => a.FullName.StartsWith( "Assembly-CSharp-Editor," ) ); // ',' included to ignore  Assembly-CSharp-Editor-FirstPass
 Type utilityType = editorAssembly.GetTypes().FirstOrDefault( t => t.FullName.Contains( "ResManEditorUtility" ) );
 utilityType.GetMethod( "InitialiseResMan", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static ).Invoke( obj: null, parameters: null );

 
 
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 Kiwasi · Nov 24, 2014 at 05:34 PM 0
Share

I feel I should clarify why breaking encapsulation like this is bad. Any scripts in the Editor folder will not end up in a build. So while this can be done, it won't work in the final build.

avatar image Jiraiyah Kiwasi · Oct 29, 2015 at 03:23 PM 0
Share

Although you are true, but sometimes (specially for custom editor stuff), when the only thing you need is to get access for a singleton of an editor window, or a scriptable object that will be dropped during the build (that holds stuff only for editor window), that reflection method is really helpful. but i totally agree, it should be used very carefully.

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

15 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

Related Questions

namespace support in unity 1 Answer

C# Unity 3D Using functions from other files. 1 Answer

Script in Scripts folder can't find namespace of file in Plugin folder 1 Answer

Why can't I use Texture2D? 1 Answer

Unable to access custom editor namespace from runtime assembly 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