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
0
Question by chrisall76 · Apr 30, 2014 at 12:26 AM · editortextasset

Editing Script In Code?

Hi all, I'm working on an editor extension for my own AI solution, and I'm now working on linking the base class to the state classes the user connects. I need help on two problems I can't seem to figure out.

1.How do I go about changing the actual code in a TextAsset? For example, let's say i wanted to change the title in the script:

 public class StateBase : EnemyStateMachine {    

How would I go about changing that to put the actual name I give it in-code? I would guess I would have to use something other than TextAsset but no clue on that.

2.How would I add things in the code in general? For example, if I wanted to add code to the update function, how would I go about actually doing that?

Here's my code:

 using UnityEngine;
 using UnityEditor;
 using System.IO;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CreateStateEditor : EditorWindow {
 
     //Info We Give
     private TextAsset ScriptBase;
     private string BaseClassSetup;
     private string BaseChaseSetup;
 
     //Information to gather
     private List<TextAsset> States = new List<TextAsset>();
     private string BaseScriptString;
     private TextAsset BaseScript;
 
     //GUI
     private List<GameObject> _selectedObjs = new List<GameObject>();
     private List<Rect> windowRects = new List<Rect> ();
     private Vector2 _methodScrollPos;
     private Vector2 _StateScrollPos;
     private Vector2 _scrollPos;
     private Rect BaseClassRect;
 
     [MenuItem ("EasyAI/Create AI...")]
     public static void showEditor () {
         EditorWindow.GetWindow<CreateStateEditor>(false, "Easy AI");
     }
 
     void OnSelectionChange(){
         Repaint ();
     }
 
     void MainWindow (int windowID) {
         GUI.DragWindow ();
         if (BaseScript != null) {
             _StateScrollPos = EditorGUILayout.BeginScrollView (_StateScrollPos);
             //GUILayout.TextArea (BaseScript.text);
             GUILayout.Label("Script Name: " + BaseScript.name);
             EditorGUILayout.EndScrollView ();
         }
     }
 
     void StateWindow (int windowID) {
         GUI.DragWindow ();
         GUILayout.Label (States[windowID].name);
         if (BaseScript != null) {
             _StateScrollPos = EditorGUILayout.BeginScrollView (_StateScrollPos);
             GUILayout.TextArea (BaseScript.text);
             EditorGUILayout.EndScrollView ();
         }
     }
 
     void OnGUI () {
         GUILayout.Label ("Base Class: ");
         BaseScript = EditorGUILayout.ObjectField(BaseScript, typeof(TextAsset)) as TextAsset;
 
         EditorGUILayout.BeginHorizontal ();
 
         BeginWindows ();
         _scrollPos = GUILayout.BeginScrollView (_scrollPos, GUILayout.Width(position.width-500));
 
         //Shows Base Class
         BaseClassRect = GUILayout.Window (100, BaseClassRect, MainWindow, "Base Class", GUILayout.Width (250), GUILayout.Height (150));
         if (windowRects.Capacity > 0) {
             for (int i = 0; i< windowRects.Count; i++) {
                 //Shows States
                 windowRects [i] = GUILayout.Window (i, windowRects [i], StateWindow, "Enemy State", GUILayout.Width (250), GUILayout.Height (150));    
             }
         }
         GUILayout.EndScrollView ();
         EndWindows ();
 
 
         _methodScrollPos = GUILayout.BeginScrollView (_methodScrollPos);
         if (GUILayout.Button ("Setup Base Class")) {
             LoadBase();
             AssetDatabase.Refresh();
         }
         if (GUILayout.Button ("Add Chase State")) {
             CreateState("ChaseState", "Hello");
             AssetDatabase.Refresh();
         }
         GUILayout.EndScrollView ();
 
         EditorGUILayout.EndHorizontal ();
     }
 
     void CreateState(string FileName, string BaseToReference){
         StreamWriter writer; 
         FileInfo t = new FileInfo(Application.dataPath+"\\"+"Resources"+"\\"+"AI"+"\\"+FileName+".cs");
         writer = t.CreateText();
         writer.Write(BaseToReference); 
         writer.Close();
 
         windowRects.Add(new Rect(0, 0, 250, 250));
     }
 
     void LoadBase(){
         //Get the base class information
         StreamReader bc = File.OpenText (Application.dataPath + "\\" +"AISolution"+"\\"+"BaseScript" + ".cs");
         string _infob = bc.ReadToEnd (); 
         bc.Close ();
         BaseClassSetup = _infob;
         
         //Set base class to the base information
         if (BaseScript != null) {
             //If file exist, exit it.
             StreamReader b = File.OpenText (Application.dataPath+"\\"+"Resources"+"\\"+"AI" + "\\" + BaseScript.name + ".cs");
             string _info = b.ReadToEnd (); 
             b.Close ();
             BaseScriptString = _info;
         } else {
             //Else, create a file
             StreamWriter writer; 
             FileInfo t = new FileInfo(Application.dataPath+"\\"+"Resources"+"\\"+"AI"+"\\"+ "StateBase.cs");
             writer = t.CreateText();
             writer.Write(BaseClassSetup); 
             writer.Close(); 
         }
     }
 }
Comment
Add comment · Show 1
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 getyour411 · Apr 30, 2014 at 02:42 AM 0
Share

I'm not sure you can do #1 (dont' know that a runtime recompile of existing/modified class wouldn't send things into a tailspin, or if such an option even exists)

For number 2, from the perspective of 'add code to update' that feels like #1; maybe you could use AddComponent to extend behaviours based on game state

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

having problems creating a textAsset - unity editor scripting 1 Answer

What is a good Javascript editor, with Syntax highlighting and checking? 5 Answers

JavaScript issue: apparently invisible variables 1 Answer

Using custom external tools for custom assets? 0 Answers

How can I execute code in the editor? 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