feat: 添加 UpdateManager 的 JobBehaviour 的Editor逻辑

main 1.0.1
NoahLan 10 months ago
parent 6dd5a9f496
commit d8b9f7e628

@ -1,6 +1,6 @@
{
"name": "LanLib.Singleton.Editor",
"rootNamespace": "",
"rootNamespace": "LanLib.Singleton.EditorUtilities",
"references": [
"LanLib.Singleton"
],

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ddd256eeb3444789b4c8a242eb4ccb95
timeCreated: 1706154518

@ -0,0 +1,39 @@
using System.Reflection;
using LanLib.UpdateManager.Jobs;
using UnityEditor;
using UnityEngine;
namespace LanLib.UpdateManager.EditorUtilities
{
[CustomEditor(typeof(AJobBehaviour<>), true)]
[CanEditMultipleObjects]
public class AJobBehaviourEditor : Editor
{
private GUIStyle TopAnchoredLabel => _topAnchoredLabel != null
? _topAnchoredLabel
: (_topAnchoredLabel = new GUIStyle(EditorStyles.label)
{
alignment = TextAnchor.UpperLeft
});
private GUIStyle _topAnchoredLabel;
public override bool HasPreviewGUI()
{
return Application.isPlaying
&& !serializedObject.isEditingMultipleObjects
&& ((MonoBehaviour)target).isActiveAndEnabled;
}
public override GUIContent GetPreviewTitle()
{
return new GUIContent($"{target.GetType().Name} Job Data");
}
public override void OnInteractivePreviewGUI(Rect r, GUIStyle background)
{
PropertyInfo prop = target.GetType().GetProperty("JobData");
GUI.Label(r, JsonUtility.ToJson(prop.GetValue(target), true), TopAnchoredLabel);
}
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e128a8c16c784b51aab1dddfb8b8329a
timeCreated: 1706154581

@ -0,0 +1,18 @@
{
"name": "LanLib.UpdateManager.Editor",
"rootNamespace": "LanLib.UpdateManager.EditorUtilities",
"references": [
"LanLib.UpdateManager"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 83d7ca5b7a6e4b21980fc187b907d443
timeCreated: 1706154533

@ -0,0 +1,91 @@
using System;
using LanLib.UpdateManager.Jobs.Internal;
using UnityEngine;
namespace LanLib.UpdateManager.Jobs
{
/// <summary>
/// <see cref="MonoBehaviour"/> abstract class with automatic registration in <see cref="UpdateTransformJobManager{}"/>.
/// </summary>
/// <remarks>
/// Instances will register themselves for job scheduling in the <c>OnEnable</c> message and unregister in the <c>OnDisable</c> message.
/// </remarks>
/// <seealso cref="UpdateTransformJobManager{}"/>
public abstract class AJobBehaviour<TData> : MonoBehaviour, ITransformJobUpdatable<TData>
where TData : struct, IUpdateTransformJob
{
/// <summary>
/// Whether job data should be synchronized every frame.
/// By default, returns false.
/// </summary>
/// <remarks>
/// Override in subclasses implementing <see cref="IJobDataSynchronizer{}"/> and return true
/// to automatically register objects for job data synchronization every frame.
/// </remarks>
public virtual bool SynchronizeJobDataEveryFrame => false;
protected virtual void OnEnable()
{
this.RegisterInManager(SynchronizeJobDataEveryFrame);
}
protected virtual void OnDisable()
{
this.UnregisterInManager();
}
public Transform Transform => transform;
/// <summary>
/// Returns the job data for the first job run.
/// </summary>
/// <remarks>
/// Override this in child classes to use data other than <see langword="default"/> in the first scheduled job run.
/// </remarks>
/// <seealso cref="ITransformJobUpdatable{}.InitialJobData"/>
public virtual TData InitialJobData => default;
/// <summary>
/// Shortcut for <c>UpdateTransformJobManager&lt;TData&gt;.Instance.GetData(this)</c>.
/// </summary>
/// <seealso cref="UpdateTransformJobManager{}.GetData"/>
public TData JobData => this.GetJobData();
#if UNITY_EDITOR
/// <summary>
/// Whether job data should be synchronized in the <c>OnValidate</c> message.
/// By default, returns true.
/// </summary>
/// <remarks>
/// Override in subclasses implementing <see cref="IJobDataSynchronizer{}"/> and return false
/// to avoid automatic job data synchronization during <c>OnValidate</c>.
/// </remarks>
protected virtual bool SynchronizeJobDataOnValidate => true;
/// <summary>
/// Synchronizes job data on Play mode if <see cref="SynchronizeJobDataOnValidate"/> returns true.
/// </summary>
protected virtual void OnValidate()
{
if (Application.isPlaying && SynchronizeJobDataOnValidate)
{
this.SynchronizeJobDataOnce();
}
}
#endif
}
/// <summary>
/// Alias for <see cref="AJobBehaviour{}"/>.
/// </summary>
/// <remarks>
/// Deprecated: use <see cref="AJobBehaviour{}"/> instead and implement
/// <see cref="IBurstUpdateTransformJob{}"/> in job definition to compile jobs with Burst.
/// </remarks>
[Obsolete("Use AJobBehaviour<> and implement IBurstUpdateTransformJob<> in job definition instead.")]
public abstract class AJobBehaviour<TData, TJob> : AJobBehaviour<TData>
where TData : struct, IUpdateTransformJob
where TJob : struct, IInternalUpdateTransformJob<TData>
{
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 72e7d96662e042d29ae18768a2ea8600
timeCreated: 1706154817
Loading…
Cancel
Save