From 4fa43eda4bc70c9c827c4fad9d5ae1327bfbc322 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Tue, 14 Jun 2022 13:27:54 +0900 Subject: [PATCH] feat: random mesh sharing group The mesh sharing group id will be selected randomly. --- Scripts/Editor/UIParticleEditor.cs | 46 +++++++++++++++++++++++++----- Scripts/UIParticle.cs | 38 ++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/Scripts/Editor/UIParticleEditor.cs b/Scripts/Editor/UIParticleEditor.cs index d410ea0..2d423aa 100644 --- a/Scripts/Editor/UIParticleEditor.cs +++ b/Scripts/Editor/UIParticleEditor.cs @@ -50,6 +50,7 @@ namespace Coffee.UIExtensions 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; @@ -60,9 +61,12 @@ namespace Coffee.UIExtensions 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 s_MaskablePropertyNames = new List { @@ -137,6 +141,7 @@ namespace Coffee.UIExtensions 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); @@ -232,7 +237,16 @@ namespace Coffee.UIExtensions 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()) + { + uip.ResetGroupId(); + } + } // Target ParticleSystems. _ro.DoLayoutList(); @@ -281,20 +295,38 @@ namespace Coffee.UIExtensions } } - 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) diff --git a/Scripts/UIParticle.cs b/Scripts/UIParticle.cs index 30ff9e5..696b642 100644 --- a/Scripts/UIParticle.cs +++ b/Scripts/UIParticle.cs @@ -51,6 +51,9 @@ namespace Coffee.UIExtensions [SerializeField] private int m_GroupId = 0; + [SerializeField] + private int m_GroupMaxId = 0; + private List m_Renderers = new List(); #if !SERIALIZE_FIELD_MASKABLE @@ -59,6 +62,7 @@ namespace Coffee.UIExtensions private DrivenRectTransformTracker _tracker; private Camera _orthoCamera; + private int _groupId; /// /// Should this graphic be considered a target for raycasting? @@ -87,8 +91,25 @@ namespace Coffee.UIExtensions /// 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 @@ -295,6 +316,7 @@ namespace Coffee.UIExtensions #if !SERIALIZE_FIELD_MASKABLE maskable = m_Maskable; #endif + ResetGroupId(); _tracker.Add(this, rectTransform, DrivenTransformProperties.Scale); UIParticleUpdater.Register(this); RegisterDirtyMaterialCallback(UpdateRendererMaterial); @@ -303,6 +325,18 @@ namespace Coffee.UIExtensions base.OnEnable(); } + internal void ResetGroupId() + { + if (m_GroupId == m_GroupMaxId) + { + _groupId = m_GroupId; + } + else + { + _groupId = Random.Range(m_GroupId, m_GroupMaxId + 1); + } + } + /// /// This function is called when the behaviour becomes disabled. ///