Add Actor.HasScene and Script.HasScene

Useful in managed code to check the existence of the scene in hot paths
by avoiding expensive marshalling of the Scene object.
This commit is contained in:
2024-04-20 14:11:15 +03:00
parent 361e9a2929
commit 4dcdd8b5f7
7 changed files with 24 additions and 9 deletions

View File

@@ -89,7 +89,7 @@ namespace FlaxEditor.Content
// Cleanup it after usage // Cleanup it after usage
Object.Destroy(actor, 20.0f); Object.Destroy(actor, 20.0f);
} }
else if (actor.Scene != null) else if (actor.HasScene)
{ {
// Create prefab with identity transform so the actor instance on a level will have it customized // Create prefab with identity transform so the actor instance on a level will have it customized
resetTransform = true; resetTransform = true;

View File

@@ -291,7 +291,7 @@ namespace FlaxEditor.CustomEditors.Dedicated
if (editor.ChildrenEditors.Count == 0 || (isRefEdited && editor is CollectionEditor)) if (editor.ChildrenEditors.Count == 0 || (isRefEdited && editor is CollectionEditor))
result = CreateDiffNode(editor); result = CreateDiffNode(editor);
bool isScriptEditorWithRefValue = editor is ScriptsEditor && editor.Values.HasReferenceValue; bool isScriptEditorWithRefValue = editor is ScriptsEditor && editor.Values.HasReferenceValue;
bool isActorEditorInLevel = editor is ActorEditor && editor.Values[0] is Actor actor && actor.IsPrefabRoot && actor.Scene != null; bool isActorEditorInLevel = editor is ActorEditor && editor.Values[0] is Actor actor && actor.IsPrefabRoot && actor.HasScene;
for (int i = 0; i < editor.ChildrenEditors.Count; i++) for (int i = 0; i < editor.ChildrenEditors.Count; i++)
{ {
var childEditor = editor.ChildrenEditors[i]; var childEditor = editor.ChildrenEditors[i];

View File

@@ -227,7 +227,7 @@ namespace FlaxEditor.Modules
// When applying changes to prefab from actor in level ignore it's root transformation (see ActorEditor.ProcessDiff) // When applying changes to prefab from actor in level ignore it's root transformation (see ActorEditor.ProcessDiff)
var originalTransform = instance.LocalTransform; var originalTransform = instance.LocalTransform;
if (instance.IsPrefabRoot && instance.Scene != null) if (instance.IsPrefabRoot && instance.HasScene)
instance.LocalTransform = prefab.GetDefaultInstance().Transform; instance.LocalTransform = prefab.GetDefaultInstance().Transform;
// Call backend // Call backend

View File

@@ -68,7 +68,7 @@ namespace FlaxEditor.SceneGraph.GUI
Visible = (actor.HideFlags & HideFlags.HideInHierarchy) == 0; Visible = (actor.HideFlags & HideFlags.HideInHierarchy) == 0;
// Pick the correct id when inside a prefab window. // Pick the correct id when inside a prefab window.
var id = actor.HasPrefabLink && actor.Scene == null ? actor.PrefabObjectID : actor.ID; var id = actor.HasPrefabLink && !actor.HasScene ? actor.PrefabObjectID : actor.ID;
if (Editor.Instance.ProjectCache.IsExpandedActor(ref id)) if (Editor.Instance.ProjectCache.IsExpandedActor(ref id))
{ {
Expand(true); Expand(true);
@@ -291,7 +291,7 @@ namespace FlaxEditor.SceneGraph.GUI
return Style.Current.ForegroundGrey; return Style.Current.ForegroundGrey;
} }
if (actor.Scene != null && Editor.Instance.StateMachine.IsPlayMode && actor.IsStatic) if (actor.HasScene && Editor.Instance.StateMachine.IsPlayMode && actor.IsStatic)
{ {
// Static // Static
return color * 0.85f; return color * 0.85f;
@@ -366,7 +366,7 @@ namespace FlaxEditor.SceneGraph.GUI
if (!IsLayoutLocked && actor) if (!IsLayoutLocked && actor)
{ {
// Pick the correct id when inside a prefab window. // Pick the correct id when inside a prefab window.
var id = actor.HasPrefabLink && actor.Scene == null ? actor.PrefabObjectID : actor.ID; var id = actor.HasPrefabLink && !actor.HasScene ? actor.PrefabObjectID : actor.ID;
Editor.Instance.ProjectCache.SetExpandedActor(ref id, IsExpanded); Editor.Instance.ProjectCache.SetExpandedActor(ref id, IsExpanded);
} }
} }
@@ -640,8 +640,8 @@ namespace FlaxEditor.SceneGraph.GUI
private bool ValidateDragScript(Script script) private bool ValidateDragScript(Script script)
{ {
// Reject dragging scripts not linked to scene (eg. from prefab) or in the opposite way // Reject dragging scripts not linked to scene (eg. from prefab) or in the opposite way
var thisHasScene = Actor.Scene != null; var thisHasScene = Actor.HasScene;
var otherHasScene = script.Scene != null; var otherHasScene = script.HasScene;
if (thisHasScene != otherHasScene) if (thisHasScene != otherHasScene)
return false; return false;

View File

@@ -91,7 +91,7 @@ namespace FlaxEditor.SceneGraph
private void OnActorSpawned(Actor actor) private void OnActorSpawned(Actor actor)
{ {
// Skip actors from game // Skip actors from game
if (actor.Scene != null) if (actor.HasScene)
return; return;
// Check if it has parent // Check if it has parent

View File

@@ -382,6 +382,15 @@ public:
return _isActiveInHierarchy != 0; return _isActiveInHierarchy != 0;
} }
/// <summary>
/// Gets value indicating if actor is in a scene.
/// </summary>
API_PROPERTY(Attributes="HideInEditor, NoSerialize")
FORCE_INLINE bool HasScene() const
{
return _scene != nullptr;
}
/// <summary> /// <summary>
/// Returns true if object is fully static on the scene, otherwise false. /// Returns true if object is fully static on the scene, otherwise false.
/// </summary> /// </summary>

View File

@@ -17,6 +17,12 @@ namespace FlaxEngine
} }
} }
/// <summary>
/// Gets value indicating if the actor owning the script is in a scene.
/// </summary>
[HideInEditor, NoSerialize]
public bool HasScene => Actor?.HasScene ?? false;
/// <summary> /// <summary>
/// Gets or sets the world space transformation of the actors owning this script. /// Gets or sets the world space transformation of the actors owning this script.
/// </summary> /// </summary>