fix: the changes made to the material used by the ParticleSystem are not immediately reflected

close #280
pull/289/head
mob-sakai 2023-12-23 19:23:44 +09:00
parent 6126af9f37
commit 3184ba94ae
2 changed files with 22 additions and 3 deletions

View File

@ -7,13 +7,13 @@ namespace Coffee.UIParticleExtensions
{
private static readonly List<MatEntry> s_Entries = new List<MatEntry>();
public static Material Add(Material baseMat, Texture texture, int id)
public static Material Add(Material baseMat, Texture texture, int id, int props)
{
MatEntry e;
for (var i = 0; i < s_Entries.Count; i++)
{
e = s_Entries[i];
if (e.baseMat != baseMat || e.texture != texture || e.id != id) continue;
if (e.baseMat != baseMat || e.texture != texture || e.id != id || e.props != props) continue;
++e.count;
return e.customMat;
}
@ -24,6 +24,7 @@ namespace Coffee.UIParticleExtensions
baseMat = baseMat,
texture = texture,
id = id,
props = props,
customMat = new Material(baseMat)
{
name = $"{baseMat.name}_{id}",
@ -64,6 +65,7 @@ namespace Coffee.UIParticleExtensions
public int count;
public Material customMat;
public int id;
public int props;
public Texture texture;
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Coffee.UIParticleExtensions;
using UnityEditor;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.Rendering;
@ -200,7 +201,12 @@ namespace Coffee.UIExtensions
//
var id = _parent.m_AnimatableProperties.Length == 0 ? 0 : GetInstanceID();
modifiedMaterial = ModifiedMaterial.Add(modifiedMaterial, texture, id);
#if UNITY_EDITOR
var props = EditorJsonUtility.ToJson(modifiedMaterial).GetHashCode();
#else
var props = 0;
#endif
modifiedMaterial = ModifiedMaterial.Add(modifiedMaterial, texture, id, props);
ModifiedMaterial.Remove(_modifiedMaterial);
_modifiedMaterial = modifiedMaterial;
@ -443,6 +449,17 @@ namespace Coffee.UIExtensions
// Update animatable material properties.
Profiler.BeginSample("[UIParticleRenderer] Update Animatable Material Properties");
#if UNITY_EDITOR
if (_modifiedMaterial != material)
{
_renderer.GetSharedMaterials(s_Materials);
material = s_Materials[_isTrail ? 1 : 0];
s_Materials.Clear();
SetMaterialDirty();
}
#endif
UpdateMaterialProperties();
if (_parent.useMeshSharing)
{