mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
修正SetSprite接口绑定资源引用关系,避免0引用导致AssetBundle被释放使图片丢失。
修正SetSprite接口绑定资源引用关系,避免0引用导致AssetBundle被释放使图片丢失。
This commit is contained in:
@@ -149,12 +149,14 @@ namespace TEngine
|
||||
{
|
||||
if (!isAsync)
|
||||
{
|
||||
image.sprite =
|
||||
GameModule.Resource.LoadAsset<Sprite>(spriteName, customPackageName: customPackageName);
|
||||
var operation = GameModule.Resource.LoadAssetGetOperation<Sprite>(spriteName, customPackageName: customPackageName);
|
||||
image.sprite = operation.AssetObject as Sprite;
|
||||
if (isSetNativeSize)
|
||||
{
|
||||
image.SetNativeSize();
|
||||
}
|
||||
|
||||
image.gameObject.GetOrAddComponent<AssetReference>().Reference(operation, spriteName);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -162,14 +164,16 @@ namespace TEngine
|
||||
{
|
||||
if (image == null)
|
||||
{
|
||||
operation.Dispose();
|
||||
operation = null;
|
||||
return;
|
||||
}
|
||||
|
||||
image.sprite = operation.AssetObject as Sprite;
|
||||
if (isSetNativeSize)
|
||||
{
|
||||
image.SetNativeSize();
|
||||
}
|
||||
image.gameObject.GetOrAddComponent<AssetReference>().Reference(operation, spriteName);
|
||||
}, customPackageName: customPackageName);
|
||||
}
|
||||
}
|
||||
@@ -198,8 +202,10 @@ namespace TEngine
|
||||
{
|
||||
if (!isAsync)
|
||||
{
|
||||
spriteRenderer.sprite =
|
||||
GameModule.Resource.LoadAsset<Sprite>(spriteName, customPackageName: customPackageName);
|
||||
var operation = GameModule.Resource.LoadAssetGetOperation<Sprite>(spriteName, customPackageName: customPackageName);
|
||||
spriteRenderer.sprite = operation.AssetObject as Sprite;
|
||||
|
||||
spriteRenderer.gameObject.GetOrAddComponent<AssetReference>().Reference(operation, spriteName);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -207,10 +213,13 @@ namespace TEngine
|
||||
{
|
||||
if (spriteRenderer == null)
|
||||
{
|
||||
operation.Dispose();
|
||||
operation = null;
|
||||
return;
|
||||
}
|
||||
|
||||
spriteRenderer.sprite = operation.AssetObject as Sprite;
|
||||
spriteRenderer.gameObject.GetOrAddComponent<AssetReference>().Reference(operation, spriteName);
|
||||
}, customPackageName: customPackageName);
|
||||
}
|
||||
}
|
||||
@@ -281,5 +290,15 @@ namespace TEngine
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static AssetReference GetAssetReference(this GameObject gameObject)
|
||||
{
|
||||
if (gameObject == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return gameObject.GetComponent<AssetReference>();
|
||||
}
|
||||
}
|
||||
}
|
@@ -59,6 +59,12 @@ namespace TEngine
|
||||
public void Bind(AssetOperationHandle operation, string assetLocation, AssetReference parent = null,
|
||||
string packageName = "")
|
||||
{
|
||||
if (_operationHandle != null)
|
||||
{
|
||||
Log.Warning($"rebind AssetReference gameObject.name:{gameObject.name} assetLocation:{assetLocation}");
|
||||
_operationHandle.Dispose();
|
||||
_operationHandle = null;
|
||||
}
|
||||
_operationHandle = operation;
|
||||
this._assetLocation = assetLocation;
|
||||
this._packageName = packageName;
|
||||
|
Reference in New Issue
Block a user