Skip to content

Instantly share code, notes, and snippets.

@bengsfort
Last active July 31, 2017 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bengsfort/3bea3a353562eeef667130d4ad98d222 to your computer and use it in GitHub Desktop.
Save bengsfort/3bea3a353562eeef667130d4ad98d222 to your computer and use it in GitHub Desktop.
Entity-Component-System Helper Window for easy creation of files http://bengsfort.github.io/dev-logs/down-the-ecs-rabbit-hole/
// {0}
// Created by {1} @ {2:d}
using System;
using FlyingIsHard.Assets.Bengsfort.Components.Core;
namespace FlyingIsHard.Assets.Bengsfort.Components
{{
[Serializable]
public class {0} : BaseComponent
{{
}}
}}
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
namespace FlyingIsHard.Assets.Bengsfort.EditorTools
{
public enum ScaffoldDialogType
{
Component,
System,
Entity
}
public class ECSScaffoldingWindow : EditorWindow
{
// Default menu base
const string c_MenuPath = "ECS Scaffold";
// What sort of file does the user want to scaffold?
public ScaffoldDialogType type = ScaffoldDialogType.Component;
string[] m_DialogTitles = new string[]
{
"Component",
"System",
"Entity"
};
// The user-input class name
string m_ClassName = "";
// The user-input template file
string m_TemplateFile = "";
// Store the users name so they dont have to constantly re-type it
public string DevName
{
get
{
return EditorPrefs.GetString("FIH_ECS_Name", "");
}
set
{
EditorPrefs.SetString("FIH_ECS_Name", value);
}
}
[MenuItem(c_MenuPath + "/New Entity")]
static void CreateEntity()
{
// Create and position the window
var window = ScriptableObject.CreateInstance<ECSScaffoldingWindow>();
window.position = new Rect(
Screen.width / 2.0f, Screen.height / 2.0f,
250.0f, 150.0f
);
window.type = ScaffoldDialogType.Entity;
window.ShowPopup();
}
[MenuItem(c_MenuPath + "/New Component")]
static void CreateComponent()
{
// Create and position the window
var window = ScriptableObject.CreateInstance<ECSScaffoldingWindow>();
window.position = new Rect(
Screen.width / 2.0f, Screen.height / 2.0f,
250.0f, 150.0f
);
window.type = ScaffoldDialogType.Component;
window.ShowPopup();
}
[MenuItem(c_MenuPath + "/New System")]
static void CreateSystem()
{
// Create and position the window
var window = ScriptableObject.CreateInstance<ECSScaffoldingWindow>();
window.position = new Rect(
Screen.width / 2.0f, Screen.height / 2.0f,
250.0f, 150.0f
);
window.type = ScaffoldDialogType.System;
window.ShowPopup();
}
void CreateClassFromTemplate()
{
// Get the template path
var tmplPathPrefix = Application.dataPath + "/Bengsfort/Editor/Scaffolding/Templates";
var tmplFile = (tmplPathPrefix + "/" + m_DialogTitles[(int)type] + "Template.cs.tmpl")
.Replace('/', Path.DirectorySeparatorChar);
// Check for the existence of the template
if (!File.Exists(tmplFile))
{
Debug.LogError("Missing the template file!");
return;
}
// Try to create the new template
try
{
// Read the file to a variable than format it with the new data
var template = File.ReadAllText(tmplFile, System.Text.Encoding.UTF8);
m_TemplateFile = string.Format(template,
m_ClassName,
DevName,
DateTime.Now
);
// Determine the correct output file
var targetFile = Application.dataPath + "/Bengsfort";
switch (type)
{
case ScaffoldDialogType.Component:
targetFile += "/Components/" + m_ClassName + ".cs";
break;
case ScaffoldDialogType.Entity:
targetFile += "/Entities/" + m_ClassName + ".cs";
break;
case ScaffoldDialogType.System:
targetFile += "/Systems/" + m_ClassName + ".cs";
break;
}
// Reformat the path to the output file to be platform agnostic
var newClass = targetFile.Replace('/', Path.DirectorySeparatorChar);
// Write the shiny new file to disk
File.WriteAllText(
newClass,
m_TemplateFile
);
// Tell Unity to import the asset
AssetDatabase.ImportAsset(
newClass.Substring(newClass.IndexOf("Assets" + Path.DirectorySeparatorChar + "Bengsfort")),
ImportAssetOptions.ForceUpdate
);
}
catch (FormatException e)
{
// Oh noes! D: It failed!
Debug.LogError("There was an error creating the new class!");
Debug.LogError(e.Message);
}
}
void OnGUI()
{
// Title
EditorGUILayout.LabelField(
"Create new " + m_DialogTitles[(int)type],
EditorStyles.boldLabel
);
// Name field
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Creator Name");
EditorGUILayout.BeginVertical();
DevName = EditorGUILayout.TextField(DevName);
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
// Title
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("Class name");
EditorGUILayout.BeginVertical();
m_ClassName = EditorGUILayout.TextField(m_ClassName);
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
// Create!
if (GUILayout.Button("Create"))
{
if (m_ClassName == "")
{
this.Close();
}
Debug.Log("Creating class " + m_ClassName);
CreateClassFromTemplate();
this.Close();
}
// Cancel
if (GUILayout.Button("Cancel"))
{
this.Close();
}
}
}
}
// {0}
// Created by {1} @ {2:d}
using System;
using UnityEngine;
using FlyingIsHard.Assets.Bengsfort.Entities.Core;
using FlyingIsHard.Assets.Bengsfort.Components;
using FlyingIsHard.Assets.Bengsfort.Systems;
namespace FlyingIsHard.Assets.Bengsfort.Entities
{{
public class {0} : BaseEntity
{{
// public MyComponent component;
// private MySystem m_System;
void Start()
{{
// m_System = new MySystem(component);
}}
void Update()
{{
// m_System.Tick();
}}
}}
}}
// {0}
// Created by {1} @ {2:d}
using FlyingIsHard.Assets.Bengsfort.Systems.Core;
using FlyingIsHard.Assets.Bengsfort.Components;
namespace FlyingIsHard.Assets.Bengsfort.Systems
{{
public class {0} : BaseSystem
{{
// private MyComponent m_Component;
// Loop Tick
public override void Tick()
{{
}}
// Constructor
public {0}()
{{
// m_Component = providedComponent;
}}
}}
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment