- Home /
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.
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();
$$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 ...
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.
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?
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612043420im_/https://answers.unity.com/themes/thub/images/avi.jpg)