i am looking for a handy (editor) script, before i create one. Someone know a script i that can be easely modified for my needs or do i need to create from scratch?
I just want to select a new imported or created gameobject and create a corresponding "class-" script by click.
The script should be named like the object e.g. myobject.cs and the class name inside should match the objects name.
So here is my script i made it so far.
But i have a refresh problem.
Only when the scene is saved, the new created component is added correctly.
//Author: APMIX
//Put this in Assets/Editor Folder
using UnityEditor;
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Text;
public class CreateClass
{
[MenuItem("Assets/Create/Add C# Class")]
static void Create()
{
GameObject selected = Selection.activeObject as GameObject;
if (selected == null || selected.name.Length <= 0 )
{
Debug.Log("Selected object not Valid");
return;
}
// remove whitespace and minus
string name = selected.name.Replace(" ","_");
name = name.Replace("-","_");
string copyPath = "Assets/"+name+".cs";
Debug.Log("Creating Classfile: " + copyPath);
if( File.Exists(copyPath) == false ){ // do not overwrite
using (StreamWriter outfile =
new StreamWriter(copyPath))
{
outfile.WriteLine("using UnityEngine;");
outfile.WriteLine("using System.Collections;");
outfile.WriteLine("");
outfile.WriteLine("public class "+name+" : MonoBehaviour {");
outfile.WriteLine(" ");
outfile.WriteLine(" ");
outfile.WriteLine(" // Use this for initialization");
outfile.WriteLine(" void Start () {");
outfile.WriteLine(" ");
outfile.WriteLine(" }");
outfile.WriteLine(" ");
outfile.WriteLine(" ");
outfile.WriteLine(" // Update is called once per frame");
outfile.WriteLine(" void Update () {");
outfile.WriteLine(" ");
outfile.WriteLine(" }");
outfile.WriteLine("}");
}//File written
}
AssetDatabase.Refresh();
selected.AddComponent(Type.GetType(name));
}
}