Skip to content

Commit

Permalink
feat: add 'custom view' option.
Browse files Browse the repository at this point in the history
Use this if the particles are not displayed correctly due to min/max particle size.
  • Loading branch information
mob-sakai committed Jun 27, 2024
1 parent 5babd6d commit 4252f11
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
19 changes: 19 additions & 0 deletions Packages/src/Editor/UIParticleEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ internal class UIParticleEditor : Editor
private SerializedProperty _groupMaxId;
private SerializedProperty _positionMode;
private SerializedProperty _autoScalingMode;
private SerializedProperty _useCustomView;
private SerializedProperty _customViewSize;
private ReorderableList _ro;
private bool _showMax;

Expand Down Expand Up @@ -82,6 +84,8 @@ private void OnEnable()
_groupMaxId = serializedObject.FindProperty("m_GroupMaxId");
_positionMode = serializedObject.FindProperty("m_PositionMode");
_autoScalingMode = serializedObject.FindProperty("m_AutoScalingMode");
_useCustomView = serializedObject.FindProperty("m_UseCustomView");
_customViewSize = serializedObject.FindProperty("m_CustomViewSize");

var sp = serializedObject.FindProperty("m_Particles");
_ro = new ReorderableList(sp.serializedObject, sp, true, true, true, true)
Expand Down Expand Up @@ -201,14 +205,29 @@ public override void OnInspectorGUI()
// Auto Scaling
DrawAutoScaling(_autoScalingMode, targets.OfType<UIParticle>());

// Custom View Size
EditorGUILayout.PropertyField(_useCustomView);
EditorGUI.BeginChangeCheck();
EditorGUI.BeginDisabledGroup(!_useCustomView.boolValue);
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(_customViewSize);
EditorGUI.indentLevel--;
EditorGUI.EndDisabledGroup();
if (EditorGUI.EndChangeCheck())
{
_customViewSize.floatValue = Mathf.Max(0.1f, _customViewSize.floatValue);
}

// Target ParticleSystems.
EditorGUI.BeginChangeCheck();
EditorGUI.BeginDisabledGroup(targets.OfType<UIParticle>().Any(x => !x.canvas));
_ro.DoLayoutList();
EditorGUI.EndDisabledGroup();
serializedObject.ApplyModifiedProperties();

if (EditorGUI.EndChangeCheck())
{
EditorApplication.QueuePlayerLoopUpdate();
foreach (var uip in targets.OfType<UIParticle>())
{
uip.RefreshParticles(uip.particles);
Expand Down
43 changes: 41 additions & 2 deletions Packages/src/Runtime/UIParticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ public enum PositionMode
"UIParticle: UIParticle.scale will be adjusted.")]
private AutoScalingMode m_AutoScalingMode = AutoScalingMode.Transform;

[SerializeField]
[Tooltip("Use a custom view.\n" +
"Use this if the particles are not displayed correctly due to min/max particle size.")]
private bool m_UseCustomView;

[SerializeField]
[Tooltip("Custom view size.\n" +
"Change the bake view size.")]
private float m_CustomViewSize = 10;

[SerializeField]
private bool m_Maskable = true;

Expand Down Expand Up @@ -243,6 +253,26 @@ public AutoScalingMode autoScalingMode
}
}

/// <summary>
/// Use a custom view.
/// Use this if the particles are not displayed correctly due to min/max particle size.
/// </summary>
public bool useCustomView
{
get => m_UseCustomView;
set => m_UseCustomView = value;
}

/// <summary>
/// Custom view size.
/// Change the bake view size.
/// </summary>
public float customViewSize
{
get => m_CustomViewSize;
set => m_CustomViewSize = Mathf.Max(0.1f, value);
}

internal bool useMeshSharing => m_MeshSharing != MeshSharing.None;

internal bool isPrimary =>
Expand Down Expand Up @@ -673,7 +703,16 @@ internal UIParticleRenderer GetRenderer(int index)
private Camera GetBakeCamera()
{
if (!canvas) return Camera.main;
if (_bakeCamera) return _bakeCamera;
if (!useCustomView && canvas.renderMode != RenderMode.ScreenSpaceOverlay && canvas.rootCanvas.worldCamera)
{
return canvas.rootCanvas.worldCamera;
}

if (_bakeCamera)
{
_bakeCamera.orthographicSize = useCustomView ? customViewSize : 10;
return _bakeCamera;
}

// Find existing baking camera.
var childCount = transform.childCount;
Expand All @@ -698,7 +737,7 @@ private Camera GetBakeCamera()

// Setup baking camera.
_bakeCamera.enabled = false;
_bakeCamera.orthographicSize = 1000;
_bakeCamera.orthographicSize = useCustomView ? customViewSize : 10;
_bakeCamera.transform.SetPositionAndRotation(new Vector3(0, 0, -1000), Quaternion.identity);
_bakeCamera.orthographic = true;
_bakeCamera.farClipPlane = 2000f;
Expand Down

0 comments on commit 4252f11

Please sign in to comment.