- Home /
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();
}
}
}
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