feat: add 'AutoScaling' option for UIParticle

Transform.lossyScale (=world scale) is automatically set to (1, 1, 1).
It prevents the root-Canvas scale from affecting the hierarchy-scaled ParticleSystem.
This option works in reverse of ’IgnoreCanvasScaler’ option in v3.x.
pull/289/head
mob-sakai 2023-08-15 21:56:09 +09:00
parent 903f702d7b
commit 35325c8899
3 changed files with 92 additions and 18 deletions

View File

@ -52,6 +52,7 @@ namespace Coffee.UIExtensions
private static readonly GUIContent s_Content3D = new GUIContent("3D"); private static readonly GUIContent s_Content3D = new GUIContent("3D");
private static readonly GUIContent s_ContentRandom = new GUIContent("Random"); private static readonly GUIContent s_ContentRandom = new GUIContent("Random");
private static readonly GUIContent s_ContentScale = new GUIContent("Scale"); private static readonly GUIContent s_ContentScale = new GUIContent("Scale");
private static readonly GUIContent s_ContentAutoScaling = new GUIContent("Auto Scaling", "Transform.lossyScale (=world scale) is automatically set to (1, 1, 1), to prevent the root-Canvas scale from affecting the hierarchy-scaled ParticleSystem.");
private static SerializedObject s_SerializedObject; private static SerializedObject s_SerializedObject;
#if !SERIALIZE_FIELD_MASKABLE #if !SERIALIZE_FIELD_MASKABLE
@ -63,6 +64,7 @@ namespace Coffee.UIExtensions
private SerializedProperty m_GroupId; private SerializedProperty m_GroupId;
private SerializedProperty m_GroupMaxId; private SerializedProperty m_GroupMaxId;
private SerializedProperty m_AbsoluteMode; private SerializedProperty m_AbsoluteMode;
private SerializedProperty m_IgnoreCanvasScaler;
private ReorderableList _ro; private ReorderableList _ro;
@ -152,6 +154,7 @@ namespace Coffee.UIExtensions
m_GroupId = serializedObject.FindProperty("m_GroupId"); m_GroupId = serializedObject.FindProperty("m_GroupId");
m_GroupMaxId = serializedObject.FindProperty("m_GroupMaxId"); m_GroupMaxId = serializedObject.FindProperty("m_GroupMaxId");
m_AbsoluteMode = serializedObject.FindProperty("m_AbsoluteMode"); m_AbsoluteMode = serializedObject.FindProperty("m_AbsoluteMode");
m_IgnoreCanvasScaler = serializedObject.FindProperty("m_IgnoreCanvasScaler");
var sp = serializedObject.FindProperty("m_Particles"); var sp = serializedObject.FindProperty("m_Particles");
_ro = new ReorderableList(sp.serializedObject, sp, true, true, true, true); _ro = new ReorderableList(sp.serializedObject, sp, true, true, true, true);
@ -262,6 +265,18 @@ namespace Coffee.UIExtensions
// Absolute Mode // Absolute Mode
EditorGUILayout.PropertyField(m_AbsoluteMode); EditorGUILayout.PropertyField(m_AbsoluteMode);
// Auto Scaling
DrawInversedToggle(m_IgnoreCanvasScaler, s_ContentAutoScaling, () =>
{
foreach (var uip in targets.OfType<UIParticle>())
{
if (uip && !uip.autoScaling)
{
uip.transform.localScale = Vector3.one;
}
}
});
// Target ParticleSystems. // Target ParticleSystems.
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
EditorGUI.BeginDisabledGroup(targets.OfType<UIParticle>().Any(x => !x.canvas)); EditorGUI.BeginDisabledGroup(targets.OfType<UIParticle>().Any(x => !x.canvas));
@ -426,6 +441,24 @@ namespace Coffee.UIExtensions
return showMax; return showMax;
} }
private static void DrawInversedToggle(SerializedProperty inversedProperty, GUIContent label, Action onChanged)
{
EditorGUI.showMixedValue = inversedProperty.hasMultipleDifferentValues;
var autoScaling = !inversedProperty.boolValue;
EditorGUI.BeginChangeCheck();
if (autoScaling != EditorGUILayout.Toggle(label, autoScaling))
{
inversedProperty.boolValue = autoScaling;
}
if (EditorGUI.EndChangeCheck())
{
EditorApplication.delayCall += onChanged.Invoke;
}
EditorGUI.showMixedValue = false;
}
private static void WindowFunction(UnityEngine.Object target, SceneView sceneView) private static void WindowFunction(UnityEngine.Object target, SceneView sceneView)
{ {
try try
@ -441,6 +474,17 @@ namespace Coffee.UIExtensions
EditorGUILayout.PropertyField(s_SerializedObject.FindProperty("m_Enabled")); EditorGUILayout.PropertyField(s_SerializedObject.FindProperty("m_Enabled"));
_xyzMode = DrawFloatOrVector3Field(s_SerializedObject.FindProperty("m_Scale3D"), _xyzMode); _xyzMode = DrawFloatOrVector3Field(s_SerializedObject.FindProperty("m_Scale3D"), _xyzMode);
EditorGUILayout.PropertyField(s_SerializedObject.FindProperty("m_AbsoluteMode")); EditorGUILayout.PropertyField(s_SerializedObject.FindProperty("m_AbsoluteMode"));
DrawInversedToggle(s_SerializedObject.FindProperty("m_IgnoreCanvasScaler"), s_ContentAutoScaling,
() =>
{
foreach (var uip in s_SerializedObject.targetObjects.OfType<UIParticle>())
{
if (uip && !uip.autoScaling)
{
uip.transform.localScale = Vector3.one;
}
}
});
EditorGUIUtility.labelWidth = labelWidth; EditorGUIUtility.labelWidth = labelWidth;
} }
s_SerializedObject.ApplyModifiedProperties(); s_SerializedObject.ApplyModifiedProperties();

View File

@ -7,6 +7,7 @@ using System.Runtime.CompilerServices;
using Coffee.UIParticleExtensions; using Coffee.UIParticleExtensions;
using UnityEngine; using UnityEngine;
using UnityEngine.Rendering; using UnityEngine.Rendering;
using UnityEngine.Serialization;
using UnityEngine.UI; using UnityEngine.UI;
[assembly: InternalsVisibleTo("Coffee.UIParticle.Editor")] [assembly: InternalsVisibleTo("Coffee.UIParticle.Editor")]
@ -59,6 +60,13 @@ namespace Coffee.UIExtensions
[Tooltip("The particles will be emitted at the ParticleSystem position.\nMove the UIParticle/ParticleSystem to move the particle.")] [Tooltip("The particles will be emitted at the ParticleSystem position.\nMove the UIParticle/ParticleSystem to move the particle.")]
private bool m_AbsoluteMode = false; private bool m_AbsoluteMode = false;
/// <summary>
/// This field uses the inverted value as "AutoScaling".
/// </summary>
[SerializeField]
[FormerlySerializedAs("m_IgnoreParent")]
private bool m_IgnoreCanvasScaler = false;
private List<UIParticleRenderer> m_Renderers = new List<UIParticleRenderer>(); private List<UIParticleRenderer> m_Renderers = new List<UIParticleRenderer>();
#if !SERIALIZE_FIELD_MASKABLE #if !SERIALIZE_FIELD_MASKABLE
@ -128,6 +136,17 @@ namespace Coffee.UIExtensions
set { m_AbsoluteMode = value; } set { m_AbsoluteMode = value; }
} }
public bool autoScaling
{
get { return !m_IgnoreCanvasScaler; }
set
{
if (m_IgnoreCanvasScaler != value) return;
m_IgnoreCanvasScaler = !value;
UpdateTracker();
}
}
internal bool useMeshSharing internal bool useMeshSharing
{ {
get { return m_MeshSharing != MeshSharing.None; } get { return m_MeshSharing != MeshSharing.None; }
@ -197,6 +216,8 @@ namespace Coffee.UIExtensions
/// </summary> /// </summary>
public bool isPaused { get; internal set; } public bool isPaused { get; internal set; }
public Vector3 parentScale { get; private set; }
public void Play() public void Play()
{ {
particles.Exec(p => p.Simulate(0, false, true)); particles.Exec(p => p.Simulate(0, false, true));
@ -335,12 +356,10 @@ namespace Coffee.UIExtensions
internal void UpdateTransformScale() internal void UpdateTransformScale()
{ {
//var newScale = Vector3.one; parentScale = transform.parent.lossyScale;
//if (uiScaling) if (!autoScaling) return;
//{
// newScale = transform.parent.lossyScale.Inverse(); var newScale = parentScale.Inverse();
//}
var newScale = transform.parent.lossyScale.Inverse();
if (transform.localScale != newScale) if (transform.localScale != newScale)
{ {
transform.localScale = newScale; transform.localScale = newScale;
@ -383,7 +402,7 @@ namespace Coffee.UIExtensions
maskable = m_Maskable; maskable = m_Maskable;
#endif #endif
ResetGroupId(); ResetGroupId();
_tracker.Add(this, rectTransform, DrivenTransformProperties.Scale); UpdateTracker();
UIParticleUpdater.Register(this); UIParticleUpdater.Register(this);
RegisterDirtyMaterialCallback(UpdateRendererMaterial); RegisterDirtyMaterialCallback(UpdateRendererMaterial);
@ -416,7 +435,7 @@ namespace Coffee.UIExtensions
/// </summary> /// </summary>
protected override void OnDisable() protected override void OnDisable()
{ {
_tracker.Clear(); UpdateTracker();
UIParticleUpdater.Unregister(this); UIParticleUpdater.Unregister(this);
m_Renderers.ForEach(r => r.Reset()); m_Renderers.ForEach(r => r.Reset());
UnregisterDirtyMaterialCallback(UpdateRendererMaterial); UnregisterDirtyMaterialCallback(UpdateRendererMaterial);
@ -499,5 +518,25 @@ namespace Coffee.UIExtensions
return _orthoCamera; return _orthoCamera;
} }
private void UpdateTracker()
{
if (!enabled || !autoScaling)
{
_tracker.Clear();
}
else
{
_tracker.Add(this, rectTransform, DrivenTransformProperties.Scale);
}
}
#if UNITY_EDITOR
protected override void OnValidate()
{
base.OnValidate();
UpdateTracker();
}
#endif
} }
} }

View File

@ -430,16 +430,7 @@ namespace Coffee.UIExtensions
private Vector3 GetWorldScale() private Vector3 GetWorldScale()
{ {
Profiler.BeginSample("[UIParticleRenderer] GetWorldScale"); Profiler.BeginSample("[UIParticleRenderer] GetWorldScale");
var scale = _parent.scale3D; var scale = _parent.scale3D.GetScaled(_parent.parentScale);
//if (_parent.uiScaling)
{
scale.Scale(_parent.transform.localScale.Inverse());
}
//else if (_parent.scalingMode == UIParticle.ScalingMode.UI && _particleSystem.main.scalingMode != ParticleSystemScalingMode.Hierarchy)
//{
// var gscale = _parent.transform.lossyScale.GetScaled(canvas.transform.lossyScale.Inverse());
// scale.Scale(gscale * canvas.scaleFactor);
//}
Profiler.EndSample(); Profiler.EndSample();
return scale; return scale;
} }