Skip to content

Commit

Permalink
fix: the changes made to the material used by the ParticleSystem are …
Browse files Browse the repository at this point in the history
…not immediately reflected

close #280
  • Loading branch information
mob-sakai committed Dec 23, 2023
1 parent 6126af9 commit 3184ba9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Scripts/ModifiedMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ internal class ModifiedMaterial
{
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;
}
Expand All @@ -24,6 +24,7 @@ public static Material Add(Material baseMat, Texture texture, int id)
baseMat = baseMat,
texture = texture,
id = id,
props = props,
customMat = new Material(baseMat)
{
name = $"{baseMat.name}_{id}",
Expand Down Expand Up @@ -64,6 +65,7 @@ private class MatEntry
public int count;
public Material customMat;
public int id;
public int props;
public Texture texture;
}
}
Expand Down
19 changes: 18 additions & 1 deletion Scripts/UIParticleRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Coffee.UIParticleExtensions;
using UnityEditor;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.Rendering;
Expand Down Expand Up @@ -200,7 +201,12 @@ public override Material GetModifiedMaterial(Material baseMaterial)

//
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;

Expand Down Expand Up @@ -443,6 +449,17 @@ public void UpdateMesh(Camera bakeCamera)

// 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)
{
Expand Down

0 comments on commit 3184ba9

Please sign in to comment.