替换玩家模型素材,添加 ChildrenMap 配置挂点
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SepCore.AsyncTask;
|
||||
using SepCore.CustomUtility;
|
||||
using SepCore.Definition;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace SepCore.Components
|
||||
{
|
||||
public class SkinComponent : MonoBehaviour
|
||||
{
|
||||
private static readonly int BaseMapPropertyId = Shader.PropertyToID("_BaseMap");
|
||||
|
||||
private MeshRenderer[] _renderers;
|
||||
private Material _instanceMaterial;
|
||||
private Texture2D _loadedTexture;
|
||||
private int _generation;
|
||||
|
||||
public async UniTask OnInitAsync(RoleConfig role, CancellationToken cancellationToken = default)
|
||||
{
|
||||
int generation = ++_generation;
|
||||
|
||||
EnsureRenderersAndMaterial();
|
||||
SetRenderersVisible(false);
|
||||
_instanceMaterial.SetTexture(BaseMapPropertyId, null);
|
||||
ReleaseLoadedTexture();
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
string textureAssetName = AssetUtility.GetSpriteAsset(role.Texture_Ref.AtlasPath);
|
||||
Texture2D texture;
|
||||
try
|
||||
{
|
||||
texture = await GameEntry.Resource.LoadAssetAsync<Texture2D>(textureAssetName);
|
||||
}
|
||||
catch (Exception exception) when (!(exception is OperationCanceledException))
|
||||
{
|
||||
Log.Error("SkinComponent.OnInitAsync: Failed to load skin texture '{0}'. {1}", textureAssetName,
|
||||
exception.Message);
|
||||
throw;
|
||||
}
|
||||
|
||||
if (generation != _generation || cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
GameEntry.Resource.UnloadAsset(texture);
|
||||
throw new OperationCanceledException(cancellationToken);
|
||||
}
|
||||
|
||||
_loadedTexture = texture;
|
||||
_instanceMaterial.SetTexture(BaseMapPropertyId, _loadedTexture);
|
||||
SetRenderersVisible(true);
|
||||
}
|
||||
|
||||
public void OnReset()
|
||||
{
|
||||
_generation++;
|
||||
|
||||
if (_renderers != null)
|
||||
{
|
||||
SetRenderersVisible(false);
|
||||
}
|
||||
|
||||
if (_instanceMaterial != null)
|
||||
{
|
||||
_instanceMaterial.SetTexture(BaseMapPropertyId, null);
|
||||
}
|
||||
|
||||
ReleaseLoadedTexture();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
OnReset();
|
||||
|
||||
if (_instanceMaterial == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
Destroy(_instanceMaterial);
|
||||
}
|
||||
else
|
||||
{
|
||||
DestroyImmediate(_instanceMaterial);
|
||||
}
|
||||
|
||||
_instanceMaterial = null;
|
||||
}
|
||||
|
||||
private void EnsureRenderersAndMaterial()
|
||||
{
|
||||
if (_renderers != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_renderers = GetComponentsInChildren<MeshRenderer>(true);
|
||||
_instanceMaterial = new Material(_renderers[0].sharedMaterial);
|
||||
for (int i = 0; i < _renderers.Length; i++)
|
||||
{
|
||||
_renderers[i].sharedMaterial = _instanceMaterial;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetRenderersVisible(bool visible)
|
||||
{
|
||||
for (int i = 0; i < _renderers.Length; i++)
|
||||
{
|
||||
_renderers[i].enabled = visible;
|
||||
}
|
||||
}
|
||||
|
||||
private void ReleaseLoadedTexture()
|
||||
{
|
||||
if (_loadedTexture == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.Resource.UnloadAsset(_loadedTexture);
|
||||
_loadedTexture = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user