Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Addyarb · Oct 31, 2015 at 10:56 AM · scripting problemstring

Convert Script To String - Not your average typecasting

Good morning!

I'm working on a seemingly convoluted editor utility where I find myself needing to convert a whole script into a string in order to search for a certain term using string.Contains.

I'm looking to do something like this : (C#-Like Pseudo-code incoming)

 string entireScriptToString = string.GetContents("MyClass.cs");
 
 or
 
 GameObject.Find("MyClass").GetComponent<MyOtherClass>().Contents.ToString();


Any ideas? I have writing a script from code down, but reading it is a bit more difficult it seems.

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

Answer by Bunny83 · Oct 31, 2015 at 12:17 PM

A script inside the Unity editor is represented by a MonoScript instance. The MonoScript class itself is derived from TextAsset and actually represents the source code of your script. You can obtain a specific MonoScript instance by using MonoScript.FromMonoBehaviour for MonoBehaviours and MonoScript.FromScriptableObject for ScriptableObjects.

You need to have an instance of the MonoBehaviour / ScriptableObject for those methods. Unfortunately there's no overload / version that takes a System.Type but it looks like that's not what you want.

So if you have an instance of a MonoBehaviour, just pass it to MonoScript.FromMonoBehaviour to get the MonoScript instance.

 #if UNITY_EDITOR
 using UnityEditor;
 using UnityEngine;
 
 public static class MonoBehaviourEditorExtension
 {
     public static string GetScriptContent(this MonoBehaviour aInstance)
     {
         var script = MonoScript.FromMonoBehaviour(aInstance);
         if (script != null)
             return script.text;
         return "";
     }
 }
 #endif

That extension method allows you to directly get the string content of the script file that contains the class. Keep in mind that MonoBehaviours can be inside an already compiled assembly in which case there is no source file.

 string scriptContent = someGameObject.GetComponent<MyMonoBehaviour>().GetScriptContent();



Comment
Add comment · Show 3 · 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 · Oct 31, 2015 at 12:19 PM 0
Share

$$anonymous$$eep in $$anonymous$$d that $$anonymous$$onoScript is an editor class. It can only be used inside the editor. At runtime all scripts only exists in their compiled form.

edit
Just as additional warning:

A script file can contain multiple classes. Of course the script itself can only represent one class (with the matching filename), however it can also contain other classes. So a pure text based search might not be the best solution depending on what you actually want to do / search for.

If you want to find a specific class it might be better to use attributes. If you want to find all classes which are derived from a certain base class or implement a certain interface, use reflection. You didn't mention your usecase so we can't suggest a better approach ...

avatar image Addyarb · Nov 01, 2015 at 05:17 AM 0
Share

Thanks so much for taking the time to reply. I'm going to give this a go now.

Since you seem to know so much about extra-Unity3D C#, perhaps I can give you a rundown of the scenario, and you can tell me if my approach is an efficient one?

I'm checking a script to see if an Enum contains a certain value. For instance

 public enum listOfColors {magenta,cyan,yellow}

and I'm offering the user a utility remove one of the enum values based on an EnumPopUp.

$$anonymous$$y approach is to get entire script into the string and rewrite it sans the enum they have chosen, and achieve this by searching string.Contains("," + enumName + ",");

Since the class is simply holding the enum values, I'm guessing the string won't be more than a couple hundred letters total. I also can't imagine a conflict with returning the wrong enum, as long as I set up a decent na$$anonymous$$g convention for them.

Thanks for your input! I'll let you know how it turns out.

avatar image Addyarb · Nov 01, 2015 at 05:36 AM 0
Share

I feel like a dummy now. I was approaching this backwards. I'm definitely going to use this script/class to string method in the future though.

Since the enums are also stored in a list, I just removed the enum from said list, generated a new string with the existing enum names, and then used a streamWriter to rewrite/update the script sans that enum's name.

Just a total oversight on my part, but hey - hours of editor scripting will do that right?

avatar image
0

Answer by hexagonius · Oct 31, 2015 at 12:00 PM

With AssetDatabase.GetAssetPath you should be able to get the datapath to your script asset.

With C# TextReader class at least I think you should be able to read that in. The example on that page might help.

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

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

33 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

Related Questions

Localization via script works in editor but not in build, why? 1 Answer

Best way to emulate Swift String Interpolation in UnityScript? 1 Answer

Outputting multi line strings to console? 2 Answers

Problem changing string value. 2 Answers

Text component change through script not working 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