fix: entity disable

This commit is contained in:
cptbtptpbcptdtptp
2026-03-27 15:12:36 +08:00
parent 1749077779
commit c662731f2f
3 changed files with 37 additions and 51 deletions

View File

@@ -36,8 +36,6 @@ export class ComponentsManager {
// Render
private _onUpdateRenderers = new DisorderedArray<Renderer>();
// Delay dispose active/inActive Pool
private _componentsContainerPool: Component[][] = [];
addCamera(camera: Camera) {
camera._cameraIndex = this._activeCameras.length;
@@ -270,14 +268,6 @@ export class ComponentsManager {
);
}
getActiveChangedTempList(): Component[] {
return this._componentsContainerPool.length ? this._componentsContainerPool.pop() : [];
}
putActiveChangedTempList(componentContainer: Component[]): void {
componentContainer.length = 0;
this._componentsContainerPool.push(componentContainer);
}
/**
* @internal

View File

@@ -122,7 +122,7 @@ export class Entity extends EngineObject {
private _transform: Transform;
private _templateResource: ReferResource;
private _parent: Entity = null;
private _activeChangedComponents: Component[];
private _isActiveChanging: boolean = false;
private _modifyFlagManager: UpdateFlagManager;
/**
@@ -565,24 +565,24 @@ export class Entity extends EngineObject {
* @internal
*/
_processActive(activeChangeFlag: ActiveChangeFlag): void {
if (this._activeChangedComponents) {
if (this._isActiveChanging) {
throw "Note: can't set the 'main inActive entity' active in hierarchy, if the operation is in main inActive entity or it's children script's onDisable Event.";
}
this._activeChangedComponents = this._scene._componentsManager.getActiveChangedTempList();
this._setActiveInHierarchy(this._activeChangedComponents, activeChangeFlag);
this._setActiveComponents(true, activeChangeFlag);
this._isActiveChanging = true;
this._setActiveInHierarchy(activeChangeFlag);
this._isActiveChanging = false;
}
/**
* @internal
*/
_processInActive(activeChangeFlag: ActiveChangeFlag): void {
if (this._activeChangedComponents) {
if (this._isActiveChanging) {
throw "Note: can't set the 'main active entity' inActive in hierarchy, if the operation is in main active entity or it's children script's onEnable Event.";
}
this._activeChangedComponents = this._scene._componentsManager.getActiveChangedTempList();
this._setInActiveInHierarchy(this._activeChangedComponents, activeChangeFlag);
this._setActiveComponents(false, activeChangeFlag);
this._isActiveChanging = true;
this._setInActiveInHierarchy(activeChangeFlag);
this._isActiveChanging = false;
}
/**
@@ -690,42 +690,33 @@ export class Entity extends EngineObject {
}
}
private _setActiveComponents(isActive: boolean, activeChangeFlag: ActiveChangeFlag): void {
const activeChangedComponents = this._activeChangedComponents;
for (let i = 0, length = activeChangedComponents.length; i < length; ++i) {
activeChangedComponents[i]._setActive(isActive, activeChangeFlag);
}
this._scene._componentsManager.putActiveChangedTempList(activeChangedComponents);
this._activeChangedComponents = null;
}
private _setActiveInHierarchy(activeChangedComponents: Component[], activeChangeFlag: ActiveChangeFlag): void {
private _setActiveInHierarchy(activeChangeFlag: ActiveChangeFlag): void {
activeChangeFlag & ActiveChangeFlag.Hierarchy && (this._isActiveInHierarchy = true);
activeChangeFlag & ActiveChangeFlag.Scene && (this._isActiveInScene = true);
const components = this._components;
for (let i = 0, n = components.length; i < n; i++) {
const component = components[i];
(component.enabled || !component._awoken) && activeChangedComponents.push(component);
(component.enabled || !component._awoken) && component._setActive(true, activeChangeFlag);
}
const children = this._children;
for (let i = 0, n = children.length; i < n; i++) {
for (let i = children.length - 1; i >= 0; i--) {
const child = children[i];
child.isActive && child._setActiveInHierarchy(activeChangedComponents, activeChangeFlag);
child.isActive && child._setActiveInHierarchy(activeChangeFlag);
}
}
private _setInActiveInHierarchy(activeChangedComponents: Component[], activeChangeFlag: ActiveChangeFlag): void {
private _setInActiveInHierarchy(activeChangeFlag: ActiveChangeFlag): void {
activeChangeFlag & ActiveChangeFlag.Hierarchy && (this._isActiveInHierarchy = false);
activeChangeFlag & ActiveChangeFlag.Scene && (this._isActiveInScene = false);
const components = this._components;
for (let i = 0, n = components.length; i < n; i++) {
const component = components[i];
component.enabled && activeChangedComponents.push(component);
component.enabled && component._setActive(false, activeChangeFlag);
}
const children = this._children;
for (let i = 0, n = children.length; i < n; i++) {
for (let i = children.length - 1; i >= 0; i--) {
const child = children[i];
child.isActive && child._setInActiveInHierarchy(activeChangedComponents, activeChangeFlag);
child.isActive && child._setInActiveInHierarchy(activeChangeFlag);
}
}

View File

@@ -58,7 +58,7 @@ export class ResourceManager {
* Create a ResourceManager.
* @param engine - Engine to which the current ResourceManager belongs
*/
constructor(public readonly engine: Engine) {}
constructor(public readonly engine: Engine) { }
/**
* Load the asset asynchronously by asset item information.
@@ -342,7 +342,12 @@ export class ResourceManager {
private _assignDefaultOptions(assetInfo: LoadItem): LoadItem {
assetInfo.type = assetInfo.type ?? ResourceManager._getTypeByUrl(assetInfo.url);
if (assetInfo.type === undefined) {
throw `asset type should be specified: ${assetInfo.url}`;
const remoteConfig = this._virtualPathResourceMap[assetInfo.url];
if (remoteConfig) {
assetInfo.type = remoteConfig.type;
} else {
throw `asset type should be specified: ${assetInfo.url}`;
}
}
assetInfo.retryCount = assetInfo.retryCount ?? this.retryCount;
assetInfo.timeout = assetInfo.timeout ?? this.timeout;
@@ -621,7 +626,7 @@ export class ResourceManager {
* @param extNames - Name of file extension
*/
export function resourceLoader(assetType: string, extNames: string[], useCache: boolean = true) {
return <T extends Loader<any>>(Target: { new (useCache: boolean): T }) => {
return <T extends Loader<any>>(Target: { new(useCache: boolean): T }) => {
const loader = new Target(useCache);
ResourceManager._addLoader(assetType, loader, extNames);
};
@@ -632,18 +637,18 @@ const reEscapeChar = /\\(\\)?/g;
const rePropName = RegExp(
// Match anything that isn't a dot or bracket.
"[^.[\\]]+" +
"|" +
// Or match property names within brackets.
"\\[(?:" +
// Match a non-string expression.
"([^\"'][^[]*)" +
"|" +
// Or match strings (supports escaping characters).
"([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2" +
")\\]" +
"|" +
// Or match "" as the space between consecutive dots or empty brackets.
"(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))",
"|" +
// Or match property names within brackets.
"\\[(?:" +
// Match a non-string expression.
"([^\"'][^[]*)" +
"|" +
// Or match strings (supports escaping characters).
"([\"'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2" +
")\\]" +
"|" +
// Or match "" as the space between consecutive dots or empty brackets.
"(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))",
"g"
);