Skip to content

Commit

Permalink
feat: random mesh sharing group
Browse files Browse the repository at this point in the history
The mesh sharing group id will be selected randomly.
  • Loading branch information
mob-sakai committed Jun 18, 2022
1 parent 9afeebf commit 4fa43ed
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
46 changes: 39 additions & 7 deletions Scripts/Editor/UIParticleEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public override void OnGUI()
private static readonly GUIContent s_ContentMaterial = new GUIContent("Material");
private static readonly GUIContent s_ContentTrailMaterial = new GUIContent("Trail Material");
private static readonly GUIContent s_Content3D = new GUIContent("3D");
private static readonly GUIContent s_ContentRandom = new GUIContent("Random");
private static readonly GUIContent s_ContentScale = new GUIContent("Scale");
private static SerializedObject s_SerializedObject;

Expand All @@ -60,9 +61,12 @@ public override void OnGUI()
private SerializedProperty m_AnimatableProperties;
private SerializedProperty m_MeshSharing;
private SerializedProperty m_GroupId;
private SerializedProperty m_GroupMaxId;


private ReorderableList _ro;
static private bool _xyzMode;
private bool _showMax;

private static readonly List<string> s_MaskablePropertyNames = new List<string>
{
Expand Down Expand Up @@ -137,6 +141,7 @@ protected override void OnEnable()
m_AnimatableProperties = serializedObject.FindProperty("m_AnimatableProperties");
m_MeshSharing = serializedObject.FindProperty("m_MeshSharing");
m_GroupId = serializedObject.FindProperty("m_GroupId");
m_GroupMaxId = serializedObject.FindProperty("m_GroupMaxId");

var sp = serializedObject.FindProperty("m_Particles");
_ro = new ReorderableList(sp.serializedObject, sp, true, true, true, true);
Expand Down Expand Up @@ -232,7 +237,16 @@ public override void OnInspectorGUI()
AnimatedPropertiesEditor.DrawAnimatableProperties(m_AnimatableProperties, mats);

// Mesh sharing
DrawMeshSharing();
EditorGUI.BeginChangeCheck();
_showMax = DrawMeshSharing(m_MeshSharing, m_GroupId, m_GroupMaxId, _showMax);
if (EditorGUI.EndChangeCheck())
{
serializedObject.ApplyModifiedProperties();
foreach (var uip in targets.OfType<UIParticle>())
{
uip.ResetGroupId();
}
}

// Target ParticleSystems.
_ro.DoLayoutList();
Expand Down Expand Up @@ -281,20 +295,38 @@ public override void OnInspectorGUI()
}
}

private void DrawMeshSharing()
private static bool DrawMeshSharing(SerializedProperty spMeshSharing, SerializedProperty spGroupId, SerializedProperty spGroupMaxId, bool showMax)
{
EditorGUILayout.PropertyField(m_MeshSharing);
EditorGUI.BeginDisabledGroup(m_MeshSharing.intValue == 0);
showMax |= spGroupId.intValue != spGroupMaxId.intValue ||
spGroupId.hasMultipleDifferentValues ||
spGroupMaxId.hasMultipleDifferentValues;

EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(spMeshSharing);

EditorGUI.BeginChangeCheck();
showMax = GUILayout.Toggle(showMax, s_ContentRandom, EditorStyles.miniButton, GUILayout.Width(60));
if (EditorGUI.EndChangeCheck() && !showMax)
spGroupMaxId.intValue = spGroupId.intValue;
EditorGUILayout.EndHorizontal();

EditorGUI.BeginDisabledGroup(spMeshSharing.intValue == 0);
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(m_GroupId);
if (m_MeshSharing.intValue == 1 || m_MeshSharing.intValue == 4)
EditorGUILayout.PropertyField(spGroupId);
if (showMax)
{
EditorGUILayout.PropertyField(spGroupMaxId);
}
else if (spMeshSharing.intValue == 1 || spMeshSharing.intValue == 4)
{
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.ObjectField("Primary", UIParticleUpdater.GetPrimary(m_GroupId.intValue), typeof(UIParticle), false);
EditorGUILayout.ObjectField("Primary", UIParticleUpdater.GetPrimary(spGroupId.intValue), typeof(UIParticle), false);
EditorGUI.EndDisabledGroup();
}
EditorGUI.indentLevel--;
EditorGUI.EndDisabledGroup();

return showMax;
}

private static void WindowFunction(UnityEngine.Object target, SceneView sceneView)
Expand Down
38 changes: 36 additions & 2 deletions Scripts/UIParticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public enum MeshSharing
[SerializeField]
private int m_GroupId = 0;

[SerializeField]
private int m_GroupMaxId = 0;

private List<UIParticleRenderer> m_Renderers = new List<UIParticleRenderer>();

#if !SERIALIZE_FIELD_MASKABLE
Expand All @@ -59,6 +62,7 @@ public enum MeshSharing

private DrivenRectTransformTracker _tracker;
private Camera _orthoCamera;
private int _groupId;

/// <summary>
/// Should this graphic be considered a target for raycasting?
Expand Down Expand Up @@ -87,8 +91,25 @@ public MeshSharing meshSharing
/// </summary>
public int groupId
{
get { return m_GroupId; }
set { m_GroupId = value; }
get { return _groupId; }
set
{
if (m_GroupId == value) return;
m_GroupId = value;
if (m_GroupId != m_GroupMaxId)
ResetGroupId();
}
}

public int groupMaxId
{
get { return m_GroupMaxId; }
set
{
if (m_GroupMaxId == value) return;
m_GroupMaxId = value;
ResetGroupId();
}
}

internal bool useMeshSharing
Expand Down Expand Up @@ -295,6 +316,7 @@ protected override void OnEnable()
#if !SERIALIZE_FIELD_MASKABLE
maskable = m_Maskable;
#endif
ResetGroupId();
_tracker.Add(this, rectTransform, DrivenTransformProperties.Scale);
UIParticleUpdater.Register(this);
RegisterDirtyMaterialCallback(UpdateRendererMaterial);
Expand All @@ -303,6 +325,18 @@ protected override void OnEnable()
base.OnEnable();
}

internal void ResetGroupId()
{
if (m_GroupId == m_GroupMaxId)
{
_groupId = m_GroupId;
}
else
{
_groupId = Random.Range(m_GroupId, m_GroupMaxId + 1);
}
}

/// <summary>
/// This function is called when the behaviour becomes disabled.
/// </summary>
Expand Down

0 comments on commit 4fa43ed

Please sign in to comment.