Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Alokdo · Apr 01, 2017 at 07:09 PM · unityeditorassetsscritping

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!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

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 Alokdo · Apr 01, 2017 at 11:21 PM 0
Share

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();
     }
 }
avatar image gameplay4all Alokdo · Apr 02, 2017 at 03:35 PM 0
Share

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? :)

avatar image Alokdo gameplay4all · Apr 03, 2017 at 12:08 AM 0
Share

You're right!

Thank you so much!

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

98 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

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


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