Skip to content

Commit 7d3ab97

Browse files
committed
修复AvatarPreview在脚本编译或者Play的时候会导致的内存泄漏问题
1 parent a469c65 commit 7d3ab97

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

Assets/Editor/Examples/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public class Constants
1010
public const string GLOBAL_INPUT_ENEVT_ENABLE = "__GLOBAL_INPUT_ENEVT_ENABLE__";
1111

1212
public static readonly GUIStyle preBackgroundSolid = "PreBackgroundSolid";
13+
public const string PREVIRE_TAG = "PREVIRE_TAG";
1314
}
1415
}

Assets/Editor/Examples/Example_09_AvatarPreview/AvatarPreview.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ static GameObject CalculatePreviewGameObject(Animator selectedAnimator, Motion m
269269

270270
// Use selected preview
271271
GameObject selected = EditorHelper.InstantiateGoByPrefab(selectedAnimator.gameObject, null);
272+
selected.tag = Constants.PREVIRE_TAG;
272273
InitInstantiatedPreviewRecursive(selected);
273274
if (IsValidPreviewGameObject(selected, ModelImporterAnimationType.None))
274275
return selected;
@@ -315,6 +316,7 @@ void SetupBounds(GameObject go)
315316
if (go != null)
316317
{
317318
m_PreviewInstance = ReflectionHelper.Instance.EditorUtility.InstantiateForAnimatorPreview(go);
319+
m_PreviewInstance.tag = Constants.PREVIRE_TAG;
318320
previewUtility.AddSingleGO(m_PreviewInstance);
319321

320322
Bounds bounds = new Bounds(m_PreviewInstance.transform.position, Vector3.zero);
@@ -351,6 +353,7 @@ void InitInstance(Animator scenePreviewObject, Motion motion)
351353
{
352354
GameObject referenceGO = (GameObject) EditorGUIUtility.Load("Avatar/dial_flat.prefab");
353355
m_ReferenceInstance = (GameObject) Object.Instantiate(referenceGO, Vector3.zero, Quaternion.identity);
356+
m_ReferenceInstance.tag = Constants.PREVIRE_TAG;
354357
InitInstantiatedPreviewRecursive(m_ReferenceInstance);
355358
previewUtility.AddSingleGO(m_ReferenceInstance);
356359
}
@@ -359,6 +362,7 @@ void InitInstance(Animator scenePreviewObject, Motion motion)
359362
{
360363
GameObject directionGO = (GameObject) EditorGUIUtility.Load("Avatar/arrow.fbx");
361364
m_DirectionInstance = (GameObject) Object.Instantiate(directionGO, Vector3.zero, Quaternion.identity);
365+
m_DirectionInstance.tag = Constants.PREVIRE_TAG;
362366
InitInstantiatedPreviewRecursive(m_DirectionInstance);
363367
previewUtility.AddSingleGO(m_DirectionInstance);
364368
}
@@ -367,6 +371,7 @@ void InitInstance(Animator scenePreviewObject, Motion motion)
367371
{
368372
GameObject pivotGO = (GameObject) EditorGUIUtility.Load("Avatar/root.fbx");
369373
m_PivotInstance = (GameObject) Object.Instantiate(pivotGO, Vector3.zero, Quaternion.identity);
374+
m_PivotInstance.tag = Constants.PREVIRE_TAG;
370375
InitInstantiatedPreviewRecursive(m_PivotInstance);
371376
previewUtility.AddSingleGO(m_PivotInstance);
372377
}
@@ -375,6 +380,7 @@ void InitInstance(Animator scenePreviewObject, Motion motion)
375380
{
376381
GameObject rootGO = (GameObject) EditorGUIUtility.Load("Avatar/root.fbx");
377382
m_RootInstance = (GameObject) Object.Instantiate(rootGO, Vector3.zero, Quaternion.identity);
383+
m_RootInstance.tag = Constants.PREVIRE_TAG;
378384
InitInstantiatedPreviewRecursive(m_RootInstance);
379385
previewUtility.AddSingleGO(m_RootInstance);
380386
}
@@ -456,14 +462,7 @@ private void Init()
456462

457463
public void OnDisable()
458464
{
459-
if (m_PreviewUtility != null)
460-
{
461-
m_PreviewUtility.Cleanup();
462-
m_PreviewUtility = null;
463-
}
464-
465-
if (timeControl != null)
466-
timeControl.OnDisable();
465+
OnDestroy();
467466
}
468467

469468
public void OnDestroy()
@@ -1160,5 +1159,16 @@ internal static void SetEnabledRecursive(GameObject go, bool enabled)
11601159
renderer.enabled = enabled;
11611160
}
11621161
}
1162+
1163+
[UnityEditor.Callbacks.DidReloadScripts]
1164+
private static void OnScriptsReloaded()
1165+
{
1166+
var gameobjects = GameObject.FindGameObjectsWithTag(Constants.PREVIRE_TAG);
1167+
foreach (var item in gameobjects)
1168+
{
1169+
GameObject.DestroyImmediate(item);
1170+
}
1171+
1172+
}
11631173
}
11641174
}

Assets/Editor/Examples/Example_09_AvatarPreview/PreviewWindow.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ private void OnGUI()
4747
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(AVATAR_PATH);
4848
PreviewInstance = EditorHelper.InstantiateGoByPrefab(prefab, null);
4949
PreviewInstance.hideFlags = HideFlags.HideAndDontSave;
50+
PreviewInstance.tag = Constants.PREVIRE_TAG;
5051
_previewAnimator = AssetDatabase.LoadAssetAtPath<AnimatorController>(PREVIEW_ANIMCONTROLLER_PATH);
5152
_animationClip = AssetDatabase.LoadAssetAtPath<AnimationClip>(CLIP_PATH);
5253
var states = _previewAnimator.layers[0].stateMachine.states;
@@ -125,5 +126,13 @@ private void OnDestroy()
125126
_previewAnimator = null;
126127
_animatorState = null;
127128
}
129+
130+
private void OnDisable()
131+
{
132+
if (null != _avatarPreview)
133+
{
134+
_avatarPreview.OnDisable();
135+
}
136+
}
128137
}
129138
}

ProjectSettings/TagManager.asset

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
--- !u!78 &1
44
TagManager:
55
serializedVersion: 2
6-
tags: []
6+
tags:
7+
- PREVIRE_TAG
78
layers:
89
- Default
910
- TransparentFX

0 commit comments

Comments
 (0)