Skip to content

Instantly share code, notes, and snippets.

@tklee1975
Last active April 7, 2017 05:25
Show Gist options
  • Save tklee1975/d8c4d1ea671f238efd0a5c6902d07d8b to your computer and use it in GitHub Desktop.
Save tklee1975/d8c4d1ea671f238efd0a5c6902d07d8b to your computer and use it in GitHub Desktop.
Unity Editor Script to generate new Component and Add it to the selected object
using UnityEditor;
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Text;
// Reference
// http://answers.unity3d.com/questions/12599/editor-script-need-to-create-class-script-automati.html
// http://answers.unity3d.com/questions/1007004/generate-script-in-editor-script-and-gettype-retur.html
public class CreateNewTest {
const string NAME_CLASS_LABEL = "NewSimpleTDDTestCase";
[MenuItem("Assets/Create/New Custom Script")]
static void Create()
{
GameObject selected = Selection.activeObject as GameObject;
if (selected == null )
{
Debug.Log("Selected object not Valid");
return;
}
Debug.Log("Selected object=" + selected.name);
// Filename
string className = selected.name.Replace(" ","");
string filename = "Assets/"+className+".cs";
//
if( File.Exists(filename) ){
Debug.Log("Test Script already exists");
return;
}
string content = GetScriptContent(className);
//
using (StreamWriter outfile = new StreamWriter(filename))
{
outfile.WriteLine(content);
}
ImportAsset(className, filename);
}
static void ImportAsset(string className, string scriptPath)
{
// @BMayne’s amazing suggestion, set the name for later reference.
EditorPrefs.SetString (NAME_CLASS_LABEL, className);
// You probably don’t need to do both of these, but I’m just making sure.
// (Experiment with the ones you might or might not need)
AssetDatabase.ImportAsset (scriptPath);
AssetDatabase.Refresh (ImportAssetOptions.ForceUpdate );
}
[UnityEditor.Callbacks.DidReloadScripts]
private static void ScriptReloaded()
{
Debug.Log("ScriptReloaded!!");
// If the key doesn’t exist, don’t bother, as we’re not generating stuff.
if (!EditorPrefs.HasKey (NAME_CLASS_LABEL))
{
Debug.Log("ScriptReloaded: No key found");
return;
}
// If they key does exist and the object doesn’t, it’s just a left over key.
string name = EditorPrefs.GetString (NAME_CLASS_LABEL);
Debug.Log("ScriptReloaded: className=" + name);
GameObject go = GameObject.Find( name );
if (go == null)
{
Debug.Log("ScriptReloaded: No game object found");
return;
}
// Get the new type from the reloaded assembly!
// (It won’t work without assembly specification, because this
// is an editor script, so the default assembly is “Assembly-CSharp-editor”)
Type type = Type.GetType (name + ",Assembly-CSharp");
Debug.Log("ScriptReloaded: type=" + type);
go.AddComponent( type );
//Delete the key because we don’t need it anymore!
EditorPrefs.DeleteKey(NAME_CLASS_LABEL);
}
static string GetScriptContent(string name)
{
string template = GetScriptTemplate();
string content = template.Replace("##name##", name);
return content;
}
static string GetScriptTemplate()
{
return
@"using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ##name## : MonoBehaviour {
// TODO Script Template content
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment