mirror of
https://github.com/DCFApixels/DragonECS-Unity.git
synced 2025-09-18 18:14:35 +08:00
add templates
This commit is contained in:
parent
e34b2a8f0d
commit
9b3379d060
8
src/Templates.meta
Normal file
8
src/Templates.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7675bea928846c54d9eaefb0905778b9
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
10
src/Templates/Component.cs.txt
Normal file
10
src/Templates/Component.cs.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using DCFApixels.DragonECS;
|
||||||
|
|
||||||
|
namespace #NAMESPACE#
|
||||||
|
{
|
||||||
|
public struct #SCRIPTNAME#
|
||||||
|
{
|
||||||
|
// add your data here
|
||||||
|
}
|
||||||
|
}
|
7
src/Templates/Component.cs.txt.meta
Normal file
7
src/Templates/Component.cs.txt.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d8dcd63282111b24a94372328500c37a
|
||||||
|
TextScriptImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
20
src/Templates/ComponentExtended.cs.txt
Normal file
20
src/Templates/ComponentExtended.cs.txt
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using DCFApixels.DragonECS;
|
||||||
|
|
||||||
|
namespace #NAMESPACE#
|
||||||
|
{
|
||||||
|
// setting the color of the visualization in the debug window
|
||||||
|
//[DebugHide]
|
||||||
|
[DebugColor(DebugColor.Red)]
|
||||||
|
public struct #SCRIPTNAME# : IEcsComponentReset<#SCRIPTNAME#>
|
||||||
|
{
|
||||||
|
// add your data here
|
||||||
|
public float value;
|
||||||
|
|
||||||
|
public void Reset(ref #SCRIPTNAME# component)
|
||||||
|
{
|
||||||
|
// setting custom default values
|
||||||
|
value = 5f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
src/Templates/ComponentExtended.cs.txt.meta
Normal file
7
src/Templates/ComponentExtended.cs.txt.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 3dd75a8f9967d9440bbe2dcc30168b2f
|
||||||
|
TextScriptImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
20
src/Templates/Runner.cs.txt
Normal file
20
src/Templates/Runner.cs.txt
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
using DCFApixels.DragonECS;
|
||||||
|
|
||||||
|
namespace #NAMESPACE#
|
||||||
|
{
|
||||||
|
public interface I#SCRIPTNAME# : IEcsSystem
|
||||||
|
{
|
||||||
|
public void Do();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class #SCRIPTNAME#Runner : EcsRunner<I#SCRIPTNAME#>, I#SCRIPTNAME#
|
||||||
|
{
|
||||||
|
public void Do()
|
||||||
|
{
|
||||||
|
foreach (var target in targets)
|
||||||
|
{
|
||||||
|
target.Do();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
src/Templates/Runner.cs.txt.meta
Normal file
7
src/Templates/Runner.cs.txt.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 67fb4cee9fff4bc48a1a6009a762959c
|
||||||
|
TextScriptImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
48
src/Templates/RunnerExtended.cs.txt
Normal file
48
src/Templates/RunnerExtended.cs.txt
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using DCFApixels.DragonECS;
|
||||||
|
#if DEBUG
|
||||||
|
using Unity.Profiling;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace #NAMESPACE#
|
||||||
|
{
|
||||||
|
public interface I#SCRIPTNAME# : IEcsSystem
|
||||||
|
{
|
||||||
|
public void Do();
|
||||||
|
}
|
||||||
|
|
||||||
|
// setting the color of the visualization in the debug window
|
||||||
|
//[DebugHide]
|
||||||
|
[DebugColor(DebugColor.Red)]
|
||||||
|
public class #SCRIPTNAME#Runner : EcsRunner<I#SCRIPTNAME#>, I#SCRIPTNAME#
|
||||||
|
{
|
||||||
|
private ProfilerMarker[] _profilerMarkers;
|
||||||
|
|
||||||
|
public void Do()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < targets.Length; i++)
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
_profilerMarkers[i].Begin();
|
||||||
|
#endif
|
||||||
|
targets[i].Do();
|
||||||
|
#if DEBUG
|
||||||
|
_profilerMarkers[i].End();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
// will be called after changing "targets"
|
||||||
|
protected override void OnSetup()
|
||||||
|
{
|
||||||
|
// create an array of profiler markers for each system in "targets"
|
||||||
|
_profilerMarkers = new ProfilerMarker[targets.Length];
|
||||||
|
for (int i = 0; i < targets.Length; i++)
|
||||||
|
{
|
||||||
|
_profilerMarkers[i] = new ProfilerMarker(ProfilerCategory.Scripts, $"EcsRunner.{targets[i].GetType().Name}.{nameof(Do)}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
7
src/Templates/RunnerExtended.cs.txt.meta
Normal file
7
src/Templates/RunnerExtended.cs.txt.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 3c196b8082fad1f4ea6038a881414ce4
|
||||||
|
TextScriptImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
66
src/Templates/Startup.cs.txt
Normal file
66
src/Templates/Startup.cs.txt
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using DCFApixels.DragonECS;
|
||||||
|
|
||||||
|
namespace #NAMESPACE#
|
||||||
|
{
|
||||||
|
sealed class #SCRIPTNAME# : MonoBehaviour
|
||||||
|
{
|
||||||
|
private EcsWorld<DefaultWorld> _world;
|
||||||
|
private EcsSystems _systems;
|
||||||
|
|
||||||
|
private void Start()
|
||||||
|
{
|
||||||
|
// is needed to integrate the internal debugging tool with the unity environment
|
||||||
|
UnityDebugService.Init();
|
||||||
|
|
||||||
|
_world = new EcsWorld<DefaultWorld>();
|
||||||
|
_systems = EcsSystems.New()
|
||||||
|
// register your systems here, for example:
|
||||||
|
// .Add (new TestSystem1 ())
|
||||||
|
// .Add (new TestSystem2 ())
|
||||||
|
|
||||||
|
// inject worlds here, for example:
|
||||||
|
.Inject(_world)
|
||||||
|
//.Inject(new EcsWorld<EventWorld>())
|
||||||
|
|
||||||
|
// with Inject you can also inject other data, for example:
|
||||||
|
//.Inject(new SharedData())
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
// add debug systems for this EcsSystems here
|
||||||
|
.Add(new SystemsDebugSystem())
|
||||||
|
#endif
|
||||||
|
.BuildAndInit();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Update()
|
||||||
|
{
|
||||||
|
_systems?.Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void LateUpdate()
|
||||||
|
{
|
||||||
|
_systems?.LateRun();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void FixedUpdate()
|
||||||
|
{
|
||||||
|
_systems?.FixedRun();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDestroy()
|
||||||
|
{
|
||||||
|
// don't forget to clear data
|
||||||
|
if (_systems != null)
|
||||||
|
{
|
||||||
|
_systems.Destroy();
|
||||||
|
_systems = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_world != null)
|
||||||
|
{
|
||||||
|
_world.Destroy();
|
||||||
|
_world = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
src/Templates/Startup.cs.txt.meta
Normal file
7
src/Templates/Startup.cs.txt.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 5af8205794231544eafe9b43c30c5d83
|
||||||
|
TextScriptImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
18
src/Templates/System.cs.txt
Normal file
18
src/Templates/System.cs.txt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using DCFApixels.DragonECS;
|
||||||
|
|
||||||
|
namespace #NAMESPACE#
|
||||||
|
{
|
||||||
|
public class #SCRIPTNAME# : IEcsRunSystem, IEcsInject<EcsWorld<DefaultWorld>>
|
||||||
|
{
|
||||||
|
private EcsWorld<DefaultWorld> _world;
|
||||||
|
public void Inject(EcsWorld<DefaultWorld> obj) => _world = obj;
|
||||||
|
|
||||||
|
public void Run(EcsSystems systems)
|
||||||
|
{
|
||||||
|
// will be called on each EcsSystems.Run() call
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
src/Templates/System.cs.txt.meta
Normal file
7
src/Templates/System.cs.txt.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 3bbff7d1071aadf4f93df0a0fb6078e9
|
||||||
|
TextScriptImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
55
src/Templates/SystemExtended.cs.txt
Normal file
55
src/Templates/SystemExtended.cs.txt
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using DCFApixels.DragonECS;
|
||||||
|
|
||||||
|
namespace #NAMESPACE#
|
||||||
|
{
|
||||||
|
// setting the color of the visualization in the debug window
|
||||||
|
//[DebugHide]
|
||||||
|
[DebugColor(DebugColor.Red)]
|
||||||
|
public class #SCRIPTNAME# :
|
||||||
|
IEcsPreInitSystem,
|
||||||
|
IEcsInitSystem,
|
||||||
|
IEcsRunSystem,
|
||||||
|
IEcsLateRunSystem,
|
||||||
|
IEcsFixedRunSystem,
|
||||||
|
IEcsDestroySystem,
|
||||||
|
IEcsInject<EcsWorld<DefaultWorld>>
|
||||||
|
{
|
||||||
|
private EcsWorld<DefaultWorld> _world;
|
||||||
|
public void Inject(EcsWorld<DefaultWorld> obj) => _world = obj;
|
||||||
|
|
||||||
|
public void PreInit(EcsSystems systems)
|
||||||
|
{
|
||||||
|
// will be called once during EcsSystems.Init() call and before Init(EcsSystems systems).
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Init(EcsSystems systems)
|
||||||
|
{
|
||||||
|
// will be called once during EcsSystems.Init() call and after PreInit(EcsSystems systems).
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Run(EcsSystems systems)
|
||||||
|
{
|
||||||
|
// will be called on each EcsSystems.Run() call
|
||||||
|
}
|
||||||
|
|
||||||
|
public void LateRun(EcsSystems systems)
|
||||||
|
{
|
||||||
|
// will be called on each EcsSystems.LateRun() call
|
||||||
|
}
|
||||||
|
|
||||||
|
public void FixedRun(EcsSystems systems)
|
||||||
|
{
|
||||||
|
// will be called on each EcsSystems.FixedRun() call
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Destroy(EcsSystems systems)
|
||||||
|
{
|
||||||
|
// will be called once during EcsSystems.Destroy() call
|
||||||
|
}
|
||||||
|
|
||||||
|
//Use Runners to implement additional messages
|
||||||
|
}
|
||||||
|
}
|
7
src/Templates/SystemExtended.cs.txt.meta
Normal file
7
src/Templates/SystemExtended.cs.txt.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: fab30d6906127c040a3cbb3bd17e05c6
|
||||||
|
TextScriptImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
157
src/Templates/TemplateGenerator.cs
Normal file
157
src/Templates/TemplateGenerator.cs
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
#if UNITY_EDITOR
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
using UnityEditor;
|
||||||
|
using UnityEditor.ProjectWindowCallback;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace DCFApixels.DragonECS.Editors
|
||||||
|
{
|
||||||
|
public sealed class TemplateGenerator
|
||||||
|
{
|
||||||
|
private const int MENU_ITEM_PRIORITY = -198;
|
||||||
|
|
||||||
|
private const string TITLE = "DragonECS Template Generator";
|
||||||
|
|
||||||
|
private const string MENU_ITEM_PATH = "Assets/Create/DragonECS/";
|
||||||
|
|
||||||
|
private const string NAMESPACE_TAG = "#NAMESPACE#";
|
||||||
|
private const string SCRIPTANAME_TAG = "#SCRIPTNAME#";
|
||||||
|
|
||||||
|
#region Properties
|
||||||
|
private static Texture2D ScriptIcon => EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D;
|
||||||
|
private static string GetCurrentFilePath([System.Runtime.CompilerServices.CallerFilePath] string fileName = null)
|
||||||
|
{
|
||||||
|
return Path.GetFullPath(Path.Combine(fileName, "../"));
|
||||||
|
}
|
||||||
|
private static string TemplatesPath => GetCurrentFilePath();
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region GenerateMethods
|
||||||
|
[MenuItem(MENU_ITEM_PATH + "[Template]Startup", false, MENU_ITEM_PRIORITY)]
|
||||||
|
public static void CreateSturtupScript() => CreateScript("Startup");
|
||||||
|
|
||||||
|
[MenuItem(MENU_ITEM_PATH + "[Template]System", false, MENU_ITEM_PRIORITY)]
|
||||||
|
public static void CreateSystemSimpleScript() => CreateScript("System");
|
||||||
|
|
||||||
|
[MenuItem(MENU_ITEM_PATH + "[Template]Component", false, MENU_ITEM_PRIORITY)]
|
||||||
|
public static void CreateComponentSimpleScript() => CreateScript("Component");
|
||||||
|
|
||||||
|
[MenuItem(MENU_ITEM_PATH + "[Template]Runner", false, MENU_ITEM_PRIORITY)]
|
||||||
|
public static void CreateRunnerSimpleScript() => CreateScript("Runner");
|
||||||
|
|
||||||
|
[MenuItem(MENU_ITEM_PATH + "[Template]System Extended", false, MENU_ITEM_PRIORITY)]
|
||||||
|
public static void CreateSystemScript() => CreateScript("SystemExtended");
|
||||||
|
|
||||||
|
[MenuItem(MENU_ITEM_PATH + "[Template]Component Extended", false, MENU_ITEM_PRIORITY)]
|
||||||
|
public static void CreateComponentScript() => CreateScript("ComponentExtended");
|
||||||
|
|
||||||
|
[MenuItem(MENU_ITEM_PATH + "[Template]Runner Extended", false, MENU_ITEM_PRIORITY)]
|
||||||
|
public static void CreateRunnerScript() => CreateScript("RunnerExtended");
|
||||||
|
|
||||||
|
|
||||||
|
private static void CreateScript(string templateName)
|
||||||
|
{
|
||||||
|
CreateAndRenameAsset($"{GetAssetPath()}/Ecs{templateName}.cs", name => GenerateEndWrtieScript($"{templateName}.cs.txt", name));
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
private static void GenerateEndWrtieScript(string templateFileName, string generatedFileName)
|
||||||
|
{
|
||||||
|
string script;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
script = File.ReadAllText(Path.Combine(TemplatesPath, templateFileName));
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
EditorUtility.DisplayDialog(TITLE, $"[ERROR] Template {templateFileName} cannot be read.\r\n{exception.Message}", "Close");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var ns = EditorSettings.projectGenerationRootNamespace.Trim();
|
||||||
|
if (string.IsNullOrEmpty(ns))
|
||||||
|
{
|
||||||
|
ns = "Client";
|
||||||
|
}
|
||||||
|
|
||||||
|
script = script.Replace(NAMESPACE_TAG, ns);
|
||||||
|
script = script.Replace(SCRIPTANAME_TAG, NormalizeClassName(Path.GetFileNameWithoutExtension(generatedFileName)));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
File.WriteAllText(AssetDatabase.GenerateUniqueAssetPath(generatedFileName), script);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
EditorUtility.DisplayDialog(TITLE, $"[ERROR] The result was not written to the file.\r\n{exception.Message}", "Close");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (EditorPrefs.GetBool("kAutoRefresh")) AssetDatabase.Refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string NormalizeClassName(string className)
|
||||||
|
{
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
bool isUpper = true;
|
||||||
|
foreach (var c in className)
|
||||||
|
{
|
||||||
|
if (char.IsLetterOrDigit(c))
|
||||||
|
{
|
||||||
|
result.Append(isUpper ? char.ToUpperInvariant(c) : c);
|
||||||
|
isUpper = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
isUpper = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetAssetPath()
|
||||||
|
{
|
||||||
|
var path = AssetDatabase.GetAssetPath(Selection.activeObject);
|
||||||
|
if (!string.IsNullOrEmpty(path) && AssetDatabase.Contains(Selection.activeObject))
|
||||||
|
{
|
||||||
|
if (!AssetDatabase.IsValidFolder(path))
|
||||||
|
{
|
||||||
|
path = Path.GetDirectoryName(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
path = "Assets";
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CreateAndRenameAsset(string pathName, Action<string> onSuccess)
|
||||||
|
{
|
||||||
|
var action = ScriptableObject.CreateInstance<CustomEndNameAction>();
|
||||||
|
action.Callback = onSuccess;
|
||||||
|
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, action, pathName, ScriptIcon, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Utils
|
||||||
|
private sealed class CustomEndNameAction : EndNameEditAction
|
||||||
|
{
|
||||||
|
[NonSerialized]
|
||||||
|
public Action<string> Callback;
|
||||||
|
|
||||||
|
public override void Action(int instanceId, string pathName, string resourceFile)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(pathName))
|
||||||
|
{
|
||||||
|
EditorUtility.DisplayDialog(TITLE, "Invalid filename", "Close");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Callback?.Invoke(pathName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
11
src/Templates/TemplateGenerator.cs.meta
Normal file
11
src/Templates/TemplateGenerator.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 98b537892f3da62409c7f6322323af10
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user