Edit a script from another script
Hi, i'm trying to make an editor script that inserts code into another script, but i couldn't find anything about changing internal assets (even in editor).
UPDATE:
The "gameplay4all" response is useful, but i have another problem:
It could be, but for that i need to know where is the script (inside what folder is). I was thinking in something like this:
using System.IO;
using UnityEngine;
public class ChangeScript : MonoBehaviour
{
public static string path = "Scripts/Output.cs";
public static string codeToWrite = "code that will be writed into the script";
public static void Save()
{
var out = File.CreateText("./" + path);
out.WriteLine(code);
out.Close();
}
}
The code works, but if i change the location of the script i had to change the variable "path" too, so...
There's a way to know the location of an atached script?
E.G.:
using System.IO;
using UnityEngine;
public class ChangeScript : MonoBehaviour
{
public originalScript originalScript;
public static string codeToWrite = "code that will be writed into the script";
public static void Save()
{
var out = File.CreateText("./" + originalScript.Path);
out.WriteLine(code);
out.Close();
}
}
Thanks!
Answer by gameplay4all · Apr 01, 2017 at 09:05 PM
You can achieve this simply with System.IO
.
This is an assembly that has useful classes for writing files to the disk. I used it once to make an enum script from the editor. Just save the file as a .cs file and you're good to go (after compiling/refreshing). Look up some examples of the StreamWriter
class usage, like here.
Good luck!
-Gameplay4all
Thanks @gameplay4all, but for that i need to know where is the script (inside what folder is).
I was thinking in something like this:
using System.IO;
using UnityEngine;
public class ChangeScript : $$anonymous$$onoBehaviour
{
public static string path = "Scripts/Output.cs";
public static string codeToWrite = "code that will be writed into the script";
public static void Save()
{
var out = File.CreateText("./" + path);
out.WriteLine(code);
out.Close();
}
}
The code works, but if i change the location of the script i had to change the variable "path" too, so...
There's a way to know the location of an atached script?
$$anonymous$$G.:
using System.IO;
using UnityEngine;
public class ChangeScript : $$anonymous$$onoBehaviour
{
public originalScript originalScript;
public static string codeToWrite = "code that will be writed into the script";
public static void Save()
{
var out = File.CreateText("./" + originalScript.Path);
out.WriteLine(code);
out.Close();
}
}
By now you've probably already found it trough google, but you can use AssetDatabase.GetAssetPath for this. Could you please mark my answer as correct? :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612114444im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
unity assets folder is empty 2 Answers
Why does Unity stop responding on pressing Play? 0 Answers
How do I hide the UI in the Game view (in editor)? 1 Answer
How can I use unity 2018 on DX9 GPU? 0 Answers
Unity IDE hides other programs 0 Answers