Khoatv: xoa GuiFX

parent b21dd01f
fileFormatVersion: 2
guid: d5ed746199d6808468f5c099038d9b12
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: d3bb3b2f89b21ba47807137b85f21859
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
namespace Coffee.UIExtensions
{
[System.Serializable]
public class AnimatableProperty : ISerializationCallbackReceiver
{
public enum ShaderPropertyType
{
Color,
Vector,
Float,
Range,
Texture,
}
[SerializeField] string m_Name = "";
[SerializeField] ShaderPropertyType m_Type = ShaderPropertyType.Vector;
public int id { get; private set; }
public ShaderPropertyType type
{
get { return m_Type; }
}
public void UpdateMaterialProperties(Material material, MaterialPropertyBlock mpb)
{
if (!material.HasProperty(id)) return;
switch (type)
{
case ShaderPropertyType.Color:
var color = mpb.GetColor(id);
if (color != default(Color))
material.SetColor(id, color);
break;
case ShaderPropertyType.Vector:
var vector = mpb.GetVector(id);
if (vector != default(Vector4))
material.SetVector(id, vector);
break;
case ShaderPropertyType.Float:
case ShaderPropertyType.Range:
var value = mpb.GetFloat(id);
if (value != default(float))
material.SetFloat(id, value);
break;
case ShaderPropertyType.Texture:
var tex = mpb.GetTexture(id);
if (tex != default(Texture))
material.SetTexture(id, tex);
break;
}
}
public void OnBeforeSerialize()
{
}
public void OnAfterDeserialize()
{
id = Shader.PropertyToID(m_Name);
}
}
}
fileFormatVersion: 2
guid: 9d017c10f452f104fadd0cbeb529c366
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
namespace Coffee.UIParticleExtensions
{
[AddComponentMenu("")]
internal class BakingCamera : MonoBehaviour
{
static BakingCamera s_Instance;
private static readonly Vector3 s_OrthoPosition = new Vector3(0, 0, -1000);
private static readonly Quaternion s_OrthoRotation = Quaternion.identity;
#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
static BakingCamera s_InstanceForPrefab;
private static BakingCamera InstanceForPrefab
{
get
{
// If current scene is prefab mode, create OverlayCamera for editor.
var prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
if (prefabStage == null || !prefabStage.scene.isLoaded) return null;
if (s_InstanceForPrefab) return s_InstanceForPrefab;
s_InstanceForPrefab = Create();
s_InstanceForPrefab.name += " (For Prefab Stage)";
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(s_InstanceForPrefab.gameObject, prefabStage.scene);
return s_InstanceForPrefab;
}
}
#endif
private static BakingCamera Instance
{
get
{
#if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
var inst = InstanceForPrefab;
if (inst) return inst;
#endif
// Find instance in scene, or create new one.
return s_Instance
? s_Instance
: (s_Instance = Create());
}
}
private Camera _camera;
private static BakingCamera Create()
{
var gameObject = new GameObject(typeof(BakingCamera).Name);
// This camera object is just for internal use
gameObject.hideFlags = HideFlags.HideAndDontSave;
var inst = gameObject.AddComponent<BakingCamera>();
inst._camera = gameObject.AddComponent<Camera>();
inst._camera.enabled = false;
inst._camera.orthographic = true;
// Turn camera off because particle mesh baker will use only camera matrix
gameObject.SetActive(false);
return inst;
}
private void Awake()
{
if (this == s_Instance)
DontDestroyOnLoad(gameObject);
}
public static Camera GetCamera(Canvas canvas)
{
if (!canvas) return Camera.main;
canvas = canvas.rootCanvas;
// Adjust camera orthographic size to canvas size
// for canvas-based coordinates of particles' size and speed.
var size = ((RectTransform) canvas.transform).rect.size;
Instance._camera.orthographicSize = Mathf.Max(size.x, size.y) * canvas.scaleFactor;
var camera = canvas.worldCamera;
var transform = Instance.transform;
var rotation = canvas.renderMode != RenderMode.ScreenSpaceOverlay && camera
? camera.transform.rotation
: s_OrthoRotation;
transform.SetPositionAndRotation(s_OrthoPosition, rotation);
Instance._camera.orthographic = true;
Instance._camera.farClipPlane = 2000f;
return Instance._camera;
}
}
}
fileFormatVersion: 2
guid: efb2d11d64545fd4288c08cdcc439082
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
{
"name": "Coffee.UIParticle",
"references": [],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false
}
\ No newline at end of file
fileFormatVersion: 2
guid: bb66ba55c0b383f4da5cea3a1f1bbe56
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections.Generic;
using UnityEngine;
namespace Coffee.UIParticleExtensions
{
internal class CombineInstanceEx
{
private int count;
public long hash = -1;
public int index = -1;
private readonly List<CombineInstance> combineInstances = new List<CombineInstance>(32);
public Mesh mesh;
public Matrix4x4 transform;
public void Combine()
{
switch (count)
{
case 0:
return;
case 1:
mesh = combineInstances[0].mesh;
transform = combineInstances[0].transform;
return;
default:
{
var cis = CombineInstanceArrayPool.Get(combineInstances);
mesh = MeshPool.Rent();
mesh.CombineMeshes(cis, true, true);
transform = Matrix4x4.identity;
cis.Clear();
return;
}
}
}
public void Clear()
{
for (var i = 0; i < combineInstances.Count; i++)
{
var inst = combineInstances[i];
MeshPool.Return(inst.mesh);
inst.mesh = null;
combineInstances[i] = inst;
}
combineInstances.Clear();
MeshPool.Return(mesh);
mesh = null;
count = 0;
hash = -1;
index = -1;
}
public void Push(Mesh mesh, Matrix4x4 transform)
{
combineInstances.Add(new CombineInstance {mesh = mesh, transform = transform});
count++;
}
}
}
fileFormatVersion: 2
guid: 9db2761424470db47b402a0ef355fcee
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: b0b71ffe101d12847b4a78168beeefcc
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using ShaderPropertyType = Coffee.UIExtensions.AnimatableProperty.ShaderPropertyType;
namespace Coffee.UIExtensions
{
internal class AnimatedPropertiesEditor
{
private static readonly List<string> s_ActiveNames = new List<string>();
private static readonly System.Text.StringBuilder s_Sb = new System.Text.StringBuilder();
private static readonly HashSet<string> s_Names = new HashSet<string>();
private string _name;
private ShaderPropertyType _type;
static string CollectActiveNames(SerializedProperty sp, List<string> result)
{
result.Clear();
for (var i = 0; i < sp.arraySize; i++)
{
var spName = sp.GetArrayElementAtIndex(i).FindPropertyRelative("m_Name");
if (spName == null) continue;
result.Add(spName.stringValue);
}
s_Sb.Length = 0;
if (result.Count == 0)
{
s_Sb.Append("Nothing");
}
else
{
result.Aggregate(s_Sb, (a, b) => s_Sb.AppendFormat("{0}, ", b));
s_Sb.Length -= 2;
}
return s_Sb.ToString();
}
public static void DrawAnimatableProperties(SerializedProperty sp, Material[] mats)
{
bool isClicked;
using (new EditorGUILayout.HorizontalScope(GUILayout.ExpandWidth(false)))
{
var r = EditorGUI.PrefixLabel(EditorGUILayout.GetControlRect(true), new GUIContent(sp.displayName, sp.tooltip));
var text = sp.hasMultipleDifferentValues ? "-" : CollectActiveNames(sp, s_ActiveNames);
isClicked = GUI.Button(r, text, EditorStyles.popup);
}
if (!isClicked) return;
var gm = new GenericMenu();
gm.AddItem(new GUIContent("Nothing"), s_ActiveNames.Count == 0, () =>
{
sp.ClearArray();
sp.serializedObject.ApplyModifiedProperties();
});
if (!sp.hasMultipleDifferentValues)
{
for (var i = 0; i < sp.arraySize; i++)
{
var p = sp.GetArrayElementAtIndex(i);
var name = p.FindPropertyRelative("m_Name").stringValue;
var type = (ShaderPropertyType) p.FindPropertyRelative("m_Type").intValue;
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = name, _type = type}, false);
}
}
s_Names.Clear();
foreach (var mat in mats)
{
if (!mat || !mat.shader) continue;
for (var i = 0; i < ShaderUtil.GetPropertyCount(mat.shader); i++)
{
var pName = ShaderUtil.GetPropertyName(mat.shader, i);
var type = (ShaderPropertyType) ShaderUtil.GetPropertyType(mat.shader, i);
var name = string.Format("{0} ({1})", pName, type);
if (s_Names.Contains(name)) continue;
s_Names.Add(name);
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName, _type = type}, true);
if (type != ShaderPropertyType.Texture) continue;
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_ST", _type = ShaderPropertyType.Vector}, true);
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_HDR", _type = ShaderPropertyType.Vector}, true);
AddMenu(gm, sp, new AnimatedPropertiesEditor {_name = pName + "_TexelSize", _type = ShaderPropertyType.Vector}, true);
}
}
gm.ShowAsContext();
}
private static void AddMenu(GenericMenu menu, SerializedProperty sp, AnimatedPropertiesEditor property, bool add)
{
if (add && s_ActiveNames.Contains(property._name)) return;
menu.AddItem(new GUIContent(string.Format("{0} ({1})", property._name, property._type)), s_ActiveNames.Contains(property._name), () =>
{
var index = s_ActiveNames.IndexOf(property._name);
if (0 <= index)
{
sp.DeleteArrayElementAtIndex(index);
}
else
{
sp.InsertArrayElementAtIndex(sp.arraySize);
var p = sp.GetArrayElementAtIndex(sp.arraySize - 1);
p.FindPropertyRelative("m_Name").stringValue = property._name;
p.FindPropertyRelative("m_Type").intValue = (int) property._type;
}
sp.serializedObject.ApplyModifiedProperties();
});
}
}
}
fileFormatVersion: 2
guid: 84d3b36abdafe404588035dd54784f55
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
{
"name": "Coffee.UIParticle.Editor",
"references": [
"Coffee.UIParticle"
],
"optionalUnityReferences": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": []
}
\ No newline at end of file
fileFormatVersion: 2
guid: 5d8b63e9af0cd714ba7c69ce01c71ce3
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
#if !UNITY_2019_1_OR_NEWER
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
namespace Coffee.UIExtensions
{
internal static class ImportSampleMenu_UIParticle
{
private const string k_DisplayName = "UI Particle";
private const string k_JsonGuid = "823dc693d087a4b559c7e1547274cc7d";
[MenuItem("Assets/Samples/" + k_DisplayName + "/Demo")]
private static void ImportSample()
{
ImportSample(k_JsonGuid, "Demo");
}
[MenuItem("Assets/Samples/" + k_DisplayName + "/Cartoon FX & War FX Demo")]
private static void ImportSample_CFX()
{
ImportSample(k_JsonGuid, "Cartoon FX & War FX Demo");
}
private static void ImportSample(string jsonGuid, string sampleName)
{
var jsonPath = AssetDatabase.GUIDToAssetPath(jsonGuid);
var packageRoot = Path.GetDirectoryName(jsonPath).Replace('\\', '/');
var json = File.ReadAllText(jsonPath);
var version = Regex.Match(json, "\"version\"\\s*:\\s*\"([^\"]+)\"").Groups[1].Value;
var src = string.Format("{0}/Samples~/{1}", packageRoot, sampleName);
var dst = string.Format("Assets/Samples/{0}/{1}/{2}", k_DisplayName, version, sampleName);
var previousPath = GetPreviousSamplePath(k_DisplayName, sampleName);
// Remove the previous sample directory.
if (!string.IsNullOrEmpty(previousPath))
{
var msg = "A different version of the sample is already imported at\n\n"
+ previousPath
+ "\n\nIt will be deleted when you update. Are you sure you want to continue?";
if (!EditorUtility.DisplayDialog("Sample Importer", msg, "OK", "Cancel"))
return;
FileUtil.DeleteFileOrDirectory(previousPath);
var metaFile = previousPath + ".meta";
if (File.Exists(metaFile))
FileUtil.DeleteFileOrDirectory(metaFile);
}
if (!Directory.Exists(dst))
FileUtil.DeleteFileOrDirectory(dst);
var dstDir = Path.GetDirectoryName(dst);
if (!Directory.Exists(dstDir))
Directory.CreateDirectory(dstDir);
if (Directory.Exists(src))
FileUtil.CopyFileOrDirectory(src, dst);
else
throw new DirectoryNotFoundException(src);
AssetDatabase.Refresh(ImportAssetOptions.ImportRecursive);
}
private static string GetPreviousSamplePath(string displayName, string sampleName)
{
var sampleRoot = string.Format("Assets/Samples/{0}", displayName);
var sampleRootInfo = new DirectoryInfo(sampleRoot);
if (!sampleRootInfo.Exists) return null;
return sampleRootInfo.GetDirectories()
.Select(versionDir => Path.Combine(versionDir.ToString(), sampleName))
.FirstOrDefault(Directory.Exists);
}
}
}
#endif
fileFormatVersion: 2
guid: 6931172d39be3334bbf5b738ed79cd2c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEditor;
using UnityEditor.UI;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using UnityEditorInternal;
using UnityEngine.UI;
namespace Coffee.UIExtensions
{
[CustomEditor(typeof(UIParticle))]
[CanEditMultipleObjects]
internal class UIParticleEditor : GraphicEditor
{
//################################
// Constant or Static Members.
//################################
private static readonly GUIContent s_ContentRenderingOrder = new GUIContent("Rendering Order");
private static readonly GUIContent s_ContentRefresh = new GUIContent("Refresh");
private static readonly GUIContent s_ContentFix = new GUIContent("Fix");
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_ContentScale = new GUIContent("Scale");
private SerializedProperty _spMaskable;
private SerializedProperty _spScale;
private SerializedProperty _spIgnoreCanvasScaler;
private SerializedProperty _spAnimatableProperties;
private SerializedProperty _spShrinkByMaterial;
private ReorderableList _ro;
private bool _xyzMode;
private bool _showMaterials;
private static readonly List<string> s_MaskablePropertyNames = new List<string>
{
"_Stencil",
"_StencilComp",
"_StencilOp",
"_StencilWriteMask",
"_StencilReadMask",
"_ColorMask",
};
//################################
// Public/Protected Members.
//################################
/// <summary>
/// This function is called when the object becomes enabled and active.
/// </summary>
protected override void OnEnable()
{
base.OnEnable();
_spMaskable = serializedObject.FindProperty("m_Maskable");
_spScale = serializedObject.FindProperty("m_Scale3D");
_spIgnoreCanvasScaler = serializedObject.FindProperty("m_IgnoreCanvasScaler");
_spAnimatableProperties = serializedObject.FindProperty("m_AnimatableProperties");
_spShrinkByMaterial = serializedObject.FindProperty("m_ShrinkByMaterial");
_showMaterials = EditorPrefs.GetBool("Coffee.UIExtensions.UIParticleEditor._showMaterials", true);
var sp = serializedObject.FindProperty("m_Particles");
_ro = new ReorderableList(sp.serializedObject, sp, true, true, true, true);
_ro.elementHeight = EditorGUIUtility.singleLineHeight * 3 + 4;
_ro.elementHeightCallback = _ => _showMaterials
? 3 * (EditorGUIUtility.singleLineHeight + 2)
: EditorGUIUtility.singleLineHeight + 2;
_ro.drawElementCallback = (rect, index, active, focused) =>
{
EditorGUI.BeginDisabledGroup(sp.hasMultipleDifferentValues);
rect.y += 1;
rect.height = EditorGUIUtility.singleLineHeight;
var p = sp.GetArrayElementAtIndex(index);
EditorGUI.ObjectField(rect, p, GUIContent.none);
if (!_showMaterials) return;
rect.x += 15;
rect.width -= 15;
var ps = p.objectReferenceValue as ParticleSystem;
var materials = ps
? new SerializedObject(ps.GetComponent<ParticleSystemRenderer>()).FindProperty("m_Materials")
: null;
rect.y += rect.height + 1;
MaterialField(rect, s_ContentMaterial, materials, 0);
rect.y += rect.height + 1;
MaterialField(rect, s_ContentTrailMaterial, materials, 1);
EditorGUI.EndDisabledGroup();
if (materials != null)
{
materials.serializedObject.ApplyModifiedProperties();
}
};
_ro.drawHeaderCallback += rect =>
{
#if !UNITY_2019_3_OR_NEWER
rect.y -= 1;
#endif
EditorGUI.LabelField(new Rect(rect.x, rect.y, 150, rect.height), s_ContentRenderingOrder);
var content = EditorGUIUtility.IconContent(_showMaterials ? "VisibilityOn" : "VisibilityOff");
_showMaterials = GUI.Toggle(new Rect(rect.width - 55, rect.y, 24, 20), _showMaterials, content, EditorStyles.label);
if (GUI.Button(new Rect(rect.width - 35, rect.y, 60, rect.height), s_ContentRefresh, EditorStyles.miniButton))
{
foreach (UIParticle t in targets)
{
t.RefreshParticles();
}
}
};
}
private static void MaterialField(Rect rect, GUIContent label, SerializedProperty sp, int index)
{
if (sp == null || sp.arraySize <= index)
{
EditorGUI.BeginDisabledGroup(true);
EditorGUI.ObjectField(rect, label, null, typeof(Material), true);
EditorGUI.EndDisabledGroup();
}
else
{
EditorGUI.PropertyField(rect, sp.GetArrayElementAtIndex(index), label);
}
}
/// <summary>
/// Implement this function to make a custom inspector.
/// </summary>
public override void OnInspectorGUI()
{
var current = target as UIParticle;
if (current == null) return;
serializedObject.Update();
// Maskable
EditorGUILayout.PropertyField(_spMaskable);
// IgnoreCanvasScaler
using (var ccs = new EditorGUI.ChangeCheckScope())
{
EditorGUILayout.PropertyField(_spIgnoreCanvasScaler);
if (ccs.changed)
{
foreach (UIParticle p in targets)
{
p.ignoreCanvasScaler = _spIgnoreCanvasScaler.boolValue;
}
}
}
// Scale
_xyzMode = DrawFloatOrVector3Field(_spScale, _xyzMode);
// AnimatableProperties
var mats = current.particles
.Where(x => x)
.Select(x => x.GetComponent<ParticleSystemRenderer>().sharedMaterial)
.Where(x => x)
.ToArray();
// Animated properties
EditorGUI.BeginChangeCheck();
AnimatedPropertiesEditor.DrawAnimatableProperties(_spAnimatableProperties, mats);
if (EditorGUI.EndChangeCheck())
{
foreach (UIParticle t in targets)
t.SetMaterialDirty();
}
// ShrinkByMaterial
EditorGUILayout.PropertyField(_spShrinkByMaterial);
// Target ParticleSystems.
_ro.DoLayoutList();
serializedObject.ApplyModifiedProperties();
// Does the shader support UI masks?
if (current.maskable && current.GetComponentInParent<Mask>())
{
foreach (var mat in current.materials)
{
if (!mat || !mat.shader) continue;
var shader = mat.shader;
foreach (var propName in s_MaskablePropertyNames)
{
if (mat.HasProperty(propName)) continue;
EditorGUILayout.HelpBox(string.Format("Shader '{0}' doesn't have '{1}' property. This graphic cannot be masked.", shader.name, propName), MessageType.Warning);
break;
}
}
}
// Does the shader support UI masks?
if (FixButton(current.m_IsTrail, "This UIParticle component should be removed. The UIParticle for trails is no longer needed."))
{
DestroyUIParticle(current);
return;
}
}
void DestroyUIParticle(UIParticle p, bool ignoreCurrent = false)
{
if (!p || ignoreCurrent && target == p) return;
var cr = p.canvasRenderer;
DestroyImmediate(p);
DestroyImmediate(cr);
#if UNITY_2018_3_OR_NEWER
var stage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
if (stage != null && stage.scene.isLoaded)
{
#if UNITY_2020_1_OR_NEWER
string prefabAssetPath = stage.assetPath;
#else
string prefabAssetPath = stage.prefabAssetPath;
#endif
PrefabUtility.SaveAsPrefabAsset(stage.prefabContentsRoot, prefabAssetPath);
}
#endif
}
bool FixButton(bool show, string text)
{
if (!show) return false;
using (new EditorGUILayout.HorizontalScope(GUILayout.ExpandWidth(true)))
{
EditorGUILayout.HelpBox(text, MessageType.Warning, true);
using (new EditorGUILayout.VerticalScope())
{
return GUILayout.Button(s_ContentFix, GUILayout.Width(30));
}
}
}
private static bool DrawFloatOrVector3Field(SerializedProperty sp, bool showXyz)
{
var x = sp.FindPropertyRelative("x");
var y = sp.FindPropertyRelative("y");
var z = sp.FindPropertyRelative("z");
showXyz |= !Mathf.Approximately(x.floatValue, y.floatValue) ||
!Mathf.Approximately(y.floatValue, z.floatValue) ||
y.hasMultipleDifferentValues ||
z.hasMultipleDifferentValues;
EditorGUILayout.BeginHorizontal();
if (showXyz)
{
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(sp);
if (EditorGUI.EndChangeCheck())
{
x.floatValue = Mathf.Max(0.001f, x.floatValue);
y.floatValue = Mathf.Max(0.001f, y.floatValue);
z.floatValue = Mathf.Max(0.001f, z.floatValue);
}
}
else
{
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(x, s_ContentScale);
if (EditorGUI.EndChangeCheck())
{
x.floatValue = Mathf.Max(0.001f, x.floatValue);
y.floatValue = Mathf.Max(0.001f, x.floatValue);
z.floatValue = Mathf.Max(0.001f, x.floatValue);
}
}
EditorGUI.BeginChangeCheck();
showXyz = GUILayout.Toggle(showXyz, s_Content3D, EditorStyles.miniButton, GUILayout.Width(30));
if (EditorGUI.EndChangeCheck() && !showXyz)
z.floatValue = y.floatValue = x.floatValue;
EditorGUILayout.EndHorizontal();
return showXyz;
}
}
}
fileFormatVersion: 2
guid: 698bf3aeef88d524caf6f1798d56c52d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
namespace Coffee.UIExtensions
{
internal class UIParticleMenu
{
[MenuItem("GameObject/UI/Particle System (Empty)", false, 2018)]
private static void AddParticleEmpty(MenuCommand menuCommand)
{
// Create empty UI element.
EditorApplication.ExecuteMenuItem("GameObject/UI/Image");
var ui = Selection.activeGameObject;
Object.DestroyImmediate(ui.GetComponent<Image>());
// Add UIParticle.
var uiParticle = ui.AddComponent<UIParticle>();
uiParticle.name = "UIParticle";
uiParticle.scale = 10;
uiParticle.rectTransform.sizeDelta = Vector2.zero;
}
[MenuItem("GameObject/UI/Particle System", false, 2019)]
private static void AddParticle(MenuCommand menuCommand)
{
// Create empty UIEffect.
AddParticleEmpty(menuCommand);
var uiParticle = Selection.activeGameObject.GetComponent<UIParticle>();
// Create ParticleSystem.
EditorApplication.ExecuteMenuItem("GameObject/Effects/Particle System");
var ps = Selection.activeGameObject;
ps.transform.SetParent(uiParticle.transform, false);
ps.transform.localPosition = Vector3.zero;
// Assign default material.
var renderer = ps.GetComponent<ParticleSystemRenderer>();
var defaultMat = AssetDatabase.GetBuiltinExtraResource<Material>("Default-Particle.mat");
renderer.material = defaultMat ? defaultMat : renderer.material;
// Refresh particles.
uiParticle.RefreshParticles();
}
}
}
fileFormatVersion: 2
guid: 8010af99ccf412145a417ba374e31f4f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;
namespace Coffee.UIParticleExtensions
{
internal static class MeshHelper
{
public static List<bool> activeMeshIndices { get; private set; }
private static readonly List<CombineInstanceEx> s_CachedInstance;
private static int count;
public static void Init()
{
activeMeshIndices = new List<bool>();
}
static MeshHelper()
{
s_CachedInstance = new List<CombineInstanceEx>(8);
for (var i = 0; i < 8; i++)
{
s_CachedInstance.Add(new CombineInstanceEx());
}
}
private static CombineInstanceEx Get(int index, long hash)
{
if (0 < count && s_CachedInstance[count - 1].hash == hash)
return s_CachedInstance[count - 1];
if (s_CachedInstance.Count <= count)
{
var newInst = new CombineInstanceEx();
s_CachedInstance.Add(newInst);
}
var inst = s_CachedInstance[count];
inst.hash = hash;
if (inst.index != -1) return inst;
inst.index = index;
count++;
return inst;
}
public static Mesh GetTemporaryMesh()
{
return MeshPool.Rent();
}
public static void Push(int index, long hash, Mesh mesh, Matrix4x4 transform)
{
if (mesh.vertexCount <= 0)
{
DiscardTemporaryMesh(mesh);
return;
}
Profiler.BeginSample("[UIParticle] MeshHelper > Get CombineInstanceEx");
var inst = Get(index, hash);
Profiler.EndSample();
Profiler.BeginSample("[UIParticle] MeshHelper > Push To Mesh Helper");
inst.Push(mesh, transform);
Profiler.EndSample();
activeMeshIndices[inst.index] = true;
}
public static void Clear()
{
count = 0;
activeMeshIndices.Clear();
foreach (var inst in s_CachedInstance)
{
inst.Clear();
}
}
public static void CombineMesh(Mesh result)
{
if (count == 0) return;
for (var i = 0; i < count; i++)
{
Profiler.BeginSample("[UIParticle] MeshHelper > Combine Mesh Internal");
s_CachedInstance[i].Combine();
Profiler.EndSample();
}
Profiler.BeginSample("[UIParticle] MeshHelper > Combine Mesh");
var cis = CombineInstanceArrayPool.Get(s_CachedInstance, count);
result.CombineMeshes(cis, false, true);
cis.Clear();
Profiler.EndSample();
result.RecalculateBounds();
}
public static void DiscardTemporaryMesh(Mesh mesh)
{
MeshPool.Return(mesh);
}
}
}
fileFormatVersion: 2
guid: aa45559f8dc317f4780d87e69c9a71d4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections.Generic;
using UnityEngine;
namespace Coffee.UIParticleExtensions
{
internal class ModifiedMaterial
{
private static readonly List<MatEntry> s_Entries = new List<MatEntry>();
public static Material Add(Material baseMat, Texture texture, int id)
{
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;
++e.count;
return e.customMat;
}
e = new MatEntry();
e.count = 1;
e.baseMat = baseMat;
e.texture = texture;
e.id = id;
e.customMat = new Material(baseMat);
e.customMat.hideFlags = HideFlags.HideAndDontSave;
if (texture)
e.customMat.mainTexture = texture;
s_Entries.Add(e);
// Debug.LogFormat(">>>> ModifiedMaterial.Add -> count = {0} {1} {2} {3}", s_Entries.Count, baseMat, texture, id);
return e.customMat;
}
public static void Remove(Material customMat)
{
if (!customMat) return;
for (var i = 0; i < s_Entries.Count; ++i)
{
var e = s_Entries[i];
if (e.customMat != customMat) continue;
if (--e.count == 0)
{
// Debug.LogFormat(">>>> ModifiedMaterial.Add -> count = {0} {1} {2} {3}", s_Entries.Count - 1, e.customMat, e.texture, e.id);
DestroyImmediate(e.customMat);
e.baseMat = null;
e.texture = null;
s_Entries.RemoveAt(i);
}
break;
}
}
private static void DestroyImmediate(Object obj)
{
if (!obj) return;
if (Application.isEditor)
Object.DestroyImmediate(obj);
else
Object.Destroy(obj);
}
private class MatEntry
{
public Material baseMat;
public Material customMat;
public int count;
public Texture texture;
public int id;
}
}
}
fileFormatVersion: 2
guid: eda92d0d666e48b48ae74cafc020395f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
#if UNITY_2019_3_11 || UNITY_2019_3_12 || UNITY_2019_3_13 || UNITY_2019_3_14 || UNITY_2019_3_15 || UNITY_2019_4_OR_NEWER
#define SERIALIZE_FIELD_MASKABLE
#endif
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Coffee.UIParticleExtensions;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Serialization;
using UnityEngine.UI;
[assembly: InternalsVisibleTo("Coffee.UIParticle.Editor")]
namespace Coffee.UIExtensions
{
/// <summary>
/// Render maskable and sortable particle effect ,without Camera, RenderTexture or Canvas.
/// </summary>
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[RequireComponent(typeof(CanvasRenderer))]
public class UIParticle : MaskableGraphic
#if UNITY_EDITOR
, ISerializationCallbackReceiver
#endif
{
[HideInInspector] [SerializeField] internal bool m_IsTrail = false;
[Tooltip("Ignore canvas scaler")] [SerializeField] [FormerlySerializedAs("m_IgnoreParent")]
bool m_IgnoreCanvasScaler = true;
[Tooltip("Particle effect scale")] [SerializeField]
float m_Scale = 100;
[Tooltip("Particle effect scale")] [SerializeField]
private Vector3 m_Scale3D;
[Tooltip("Animatable material properties. If you want to change the material properties of the ParticleSystem in Animation, enable it.")] [SerializeField]
internal AnimatableProperty[] m_AnimatableProperties = new AnimatableProperty[0];
[Tooltip("Particles")] [SerializeField]
private List<ParticleSystem> m_Particles = new List<ParticleSystem>();
[Tooltip("Shrink rendering by material on refresh.\nNOTE: Performance will be improved, but in some cases the rendering is not correct.")] [SerializeField]
bool m_ShrinkByMaterial = false;
#if !SERIALIZE_FIELD_MASKABLE
[SerializeField] private bool m_Maskable = true;
#endif
private bool _shouldBeRemoved;
private DrivenRectTransformTracker _tracker;
private Mesh _bakedMesh;
private readonly List<Material> _modifiedMaterials = new List<Material>();
private readonly List<Material> _maskMaterials = new List<Material>();
private readonly List<bool> _activeMeshIndices = new List<bool>();
private Vector3 _cachedPosition;
private static readonly List<Material> s_TempMaterials = new List<Material>(2);
private static MaterialPropertyBlock s_Mpb;
private static readonly List<Material> s_PrevMaskMaterials = new List<Material>();
private static readonly List<Material> s_PrevModifiedMaterials = new List<Material>();
private static readonly List<Component> s_Components = new List<Component>();
private static readonly List<ParticleSystem> s_ParticleSystems = new List<ParticleSystem>();
/// <summary>
/// Should this graphic be considered a target for raycasting?
/// </summary>
public override bool raycastTarget
{
get { return false; }
set { }
}
public bool ignoreCanvasScaler
{
get { return m_IgnoreCanvasScaler; }
set
{
// if (m_IgnoreCanvasScaler == value) return;
m_IgnoreCanvasScaler = value;
_tracker.Clear();
if (isActiveAndEnabled && m_IgnoreCanvasScaler)
_tracker.Add(this, rectTransform, DrivenTransformProperties.Scale);
}
}
public bool shrinkByMaterial
{
get { return m_ShrinkByMaterial; }
set
{
if (m_ShrinkByMaterial == value) return;
m_ShrinkByMaterial = value;
RefreshParticles();
}
}
/// <summary>
/// Particle effect scale.
/// </summary>
public float scale
{
get { return m_Scale3D.x; }
set
{
m_Scale = Mathf.Max(0.001f, value);
m_Scale3D = new Vector3(m_Scale, m_Scale, m_Scale);
}
}
/// <summary>
/// Particle effect scale.
/// </summary>
public Vector3 scale3D
{
get { return m_Scale3D; }
set
{
if (m_Scale3D == value) return;
m_Scale3D.x = Mathf.Max(0.001f, value.x);
m_Scale3D.y = Mathf.Max(0.001f, value.y);
m_Scale3D.z = Mathf.Max(0.001f, value.z);
}
}
internal Mesh bakedMesh
{
get { return _bakedMesh; }
}
public List<ParticleSystem> particles
{
get { return m_Particles; }
}
public IEnumerable<Material> materials
{
get { return _modifiedMaterials; }
}
public override Material materialForRendering
{
get { return canvasRenderer.GetMaterial(0); }
}
public List<bool> activeMeshIndices
{
get { return _activeMeshIndices; }
set
{
if (_activeMeshIndices.SequenceEqualFast(value)) return;
_activeMeshIndices.Clear();
_activeMeshIndices.AddRange(value);
UpdateMaterial();
}
}
internal Vector3 cachedPosition
{
get { return _cachedPosition; }
set { _cachedPosition = value; }
}
public void Play()
{
particles.Exec(p => p.Play());
}
public void Pause()
{
particles.Exec(p => p.Pause());
}
public void Stop()
{
particles.Exec(p => p.Stop());
}
public void Clear()
{
particles.Exec(p => p.Clear());
}
public void SetParticleSystemInstance(GameObject instance)
{
SetParticleSystemInstance(instance, true);
}
public void SetParticleSystemInstance(GameObject instance, bool destroyOldParticles)
{
if (!instance) return;
foreach (Transform child in transform)
{
var go = child.gameObject;
go.SetActive(false);
if (!destroyOldParticles) continue;
#if UNITY_EDITOR
if (!Application.isPlaying)
DestroyImmediate(go);
else
#endif
Destroy(go);
}
var tr = instance.transform;
tr.SetParent(transform, false);
tr.localPosition = Vector3.zero;
RefreshParticles(instance);
}
public void SetParticleSystemPrefab(GameObject prefab)
{
if (!prefab) return;
SetParticleSystemInstance(Instantiate(prefab.gameObject), true);
}
public void RefreshParticles()
{
RefreshParticles(gameObject);
}
public void RefreshParticles(GameObject root)
{
if (!root) return;
root.GetComponentsInChildren(particles);
particles.RemoveAll(x => x.GetComponentInParent<UIParticle>() != this);
foreach (var ps in particles)
{
var tsa = ps.textureSheetAnimation;
if (tsa.mode == ParticleSystemAnimationMode.Sprites && tsa.uvChannelMask == (UVChannelFlags) 0)
tsa.uvChannelMask = UVChannelFlags.UV0;
}
particles.Exec(p => p.GetComponent<ParticleSystemRenderer>().enabled = !enabled);
particles.SortForRendering(transform, m_ShrinkByMaterial);
SetMaterialDirty();
}
protected override void UpdateMaterial()
{
// Clear mask materials.
s_PrevMaskMaterials.AddRange(_maskMaterials);
_maskMaterials.Clear();
// Clear modified materials.
s_PrevModifiedMaterials.AddRange(_modifiedMaterials);
_modifiedMaterials.Clear();
// Recalculate stencil value.
if (m_ShouldRecalculateStencil)
{
var rootCanvas = MaskUtilities.FindRootSortOverrideCanvas(transform);
m_StencilValue = maskable ? MaskUtilities.GetStencilDepth(transform, rootCanvas) : 0;
m_ShouldRecalculateStencil = false;
}
// No mesh to render.
var count = activeMeshIndices.CountFast();
if (count == 0 || !isActiveAndEnabled || particles.Count == 0)
{
canvasRenderer.Clear();
ClearPreviousMaterials();
return;
}
//
GetComponents(typeof(IMaterialModifier), s_Components);
var materialCount = Mathf.Min(8, count);
canvasRenderer.materialCount = materialCount;
var j = 0;
for (var i = 0; i < particles.Count; i++)
{
if (materialCount <= j) break;
var ps = particles[i];
if (!ps) continue;
var r = ps.GetComponent<ParticleSystemRenderer>();
r.GetSharedMaterials(s_TempMaterials);
// Main
var index = i * 2;
if (activeMeshIndices.Count <= index) break;
if (activeMeshIndices[index] && 0 < s_TempMaterials.Count)
{
var mat = GetModifiedMaterial(s_TempMaterials[0], ps.GetTextureForSprite());
for (var k = 1; k < s_Components.Count; k++)
mat = (s_Components[k] as IMaterialModifier).GetModifiedMaterial(mat);
canvasRenderer.SetMaterial(mat, j);
UpdateMaterialProperties(r, j);
j++;
}
// Trails
index++;
if (activeMeshIndices.Count <= index || materialCount <= j) break;
if (activeMeshIndices[index] && 1 < s_TempMaterials.Count)
{
var mat = GetModifiedMaterial(s_TempMaterials[1], null);
for (var k = 1; k < s_Components.Count; k++)
mat = (s_Components[k] as IMaterialModifier).GetModifiedMaterial(mat);
canvasRenderer.SetMaterial(mat, j++);
}
}
ClearPreviousMaterials();
}
private void ClearPreviousMaterials()
{
foreach (var m in s_PrevMaskMaterials)
StencilMaterial.Remove(m);
s_PrevMaskMaterials.Clear();
foreach (var m in s_PrevModifiedMaterials)
ModifiedMaterial.Remove(m);
s_PrevModifiedMaterials.Clear();
}
private Material GetModifiedMaterial(Material baseMaterial, Texture2D texture)
{
if (0 < m_StencilValue)
{
baseMaterial = StencilMaterial.Add(baseMaterial, (1 << m_StencilValue) - 1, StencilOp.Keep, CompareFunction.Equal, ColorWriteMask.All, (1 << m_StencilValue) - 1, 0);
_maskMaterials.Add(baseMaterial);
}
if (texture == null && m_AnimatableProperties.Length == 0) return baseMaterial;
var id = m_AnimatableProperties.Length == 0 ? 0 : GetInstanceID();
baseMaterial = ModifiedMaterial.Add(baseMaterial, texture, id);
_modifiedMaterials.Add(baseMaterial);
return baseMaterial;
}
internal void UpdateMaterialProperties()
{
if (m_AnimatableProperties.Length == 0) return;
//
var count = activeMeshIndices.CountFast();
var materialCount = Mathf.Max(8, count);
canvasRenderer.materialCount = materialCount;
var j = 0;
for (var i = 0; i < particles.Count; i++)
{
if (materialCount <= j) break;
var ps = particles[i];
if (!ps) continue;
var r = ps.GetComponent<ParticleSystemRenderer>();
r.GetSharedMaterials(s_TempMaterials);
// Main
if (activeMeshIndices[i * 2] && 0 < s_TempMaterials.Count)
{
UpdateMaterialProperties(r, j);
j++;
}
}
}
internal void UpdateMaterialProperties(Renderer r, int index)
{
if (m_AnimatableProperties.Length == 0 || canvasRenderer.materialCount <= index) return;
r.GetPropertyBlock(s_Mpb ?? (s_Mpb = new MaterialPropertyBlock()));
if (s_Mpb.isEmpty) return;
// #41: Copy the value from MaterialPropertyBlock to CanvasRenderer
var mat = canvasRenderer.GetMaterial(index);
if (!mat) return;
foreach (var ap in m_AnimatableProperties)
{
ap.UpdateMaterialProperties(mat, s_Mpb);
}
s_Mpb.Clear();
}
/// <summary>
/// This function is called when the object becomes enabled and active.
/// </summary>
protected override void OnEnable()
{
#if !SERIALIZE_FIELD_MASKABLE
maskable = m_Maskable;
#endif
activeMeshIndices.Clear();
UIParticleUpdater.Register(this);
particles.Exec(p => p.GetComponent<ParticleSystemRenderer>().enabled = false);
if (isActiveAndEnabled && m_IgnoreCanvasScaler)
{
_tracker.Add(this, rectTransform, DrivenTransformProperties.Scale);
}
// Create objects.
_bakedMesh = MeshPool.Rent();
base.OnEnable();
InitializeIfNeeded();
}
private new IEnumerator Start()
{
// #147: ParticleSystem creates Particles in wrong position during prewarm
// #148: Particle Sub Emitter not showing when start game
var delayToPlay = particles.AnyFast(ps =>
{
ps.GetComponentsInChildren(false, s_ParticleSystems);
return s_ParticleSystems.AnyFast(p => p.isPlaying && (p.subEmitters.enabled || p.main.prewarm));
});
s_ParticleSystems.Clear();
if (!delayToPlay) yield break;
Stop();
Clear();
yield return null;
Play();
}
/// <summary>
/// This function is called when the behaviour becomes disabled.
/// </summary>
protected override void OnDisable()
{
UIParticleUpdater.Unregister(this);
if (!_shouldBeRemoved)
particles.Exec(p => p.GetComponent<ParticleSystemRenderer>().enabled = true);
_tracker.Clear();
// Destroy object.
MeshPool.Return(_bakedMesh);
_bakedMesh = null;
base.OnDisable();
}
/// <summary>
/// Call to update the geometry of the Graphic onto the CanvasRenderer.
/// </summary>
protected override void UpdateGeometry()
{
}
/// <summary>
/// Callback for when properties have been changed by animation.
/// </summary>
protected override void OnDidApplyAnimationProperties()
{
}
private void InitializeIfNeeded()
{
if (enabled && m_IsTrail)
{
UnityEngine.Debug.LogWarningFormat(this, "[UIParticle] The UIParticle component should be removed: {0}\nReason: UIParticle for trails is no longer needed.", name);
gameObject.hideFlags = HideFlags.None;
_shouldBeRemoved = true;
enabled = false;
return;
}
if (!this || particles.AnyFast()) return;
// refresh.
#if UNITY_EDITOR
if (!Application.isPlaying)
UnityEditor.EditorApplication.delayCall += () =>
{
if (this) RefreshParticles();
};
else
#endif
RefreshParticles();
}
#if UNITY_EDITOR
protected override void OnValidate()
{
SetLayoutDirty();
SetVerticesDirty();
m_ShouldRecalculateStencil = true;
RecalculateClipping();
#if !SERIALIZE_FIELD_MASKABLE
maskable = m_Maskable;
#endif
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
if (Application.isPlaying) return;
InitializeIfNeeded();
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
if (m_Scale3D == Vector3.zero)
{
scale = m_Scale;
}
UnityEditor.EditorApplication.delayCall += () =>
{
if (Application.isPlaying || !this) return;
InitializeIfNeeded();
};
}
#endif
}
}
fileFormatVersion: 2
guid: 18fe414fec9caac42aa01a056ad915f5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: -100
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections.Generic;
using Coffee.UIParticleExtensions;
using UnityEngine;
using UnityEngine.Profiling;
namespace Coffee.UIExtensions
{
internal static class UIParticleUpdater
{
static readonly List<UIParticle> s_ActiveParticles = new List<UIParticle>();
static MaterialPropertyBlock s_Mpb;
static ParticleSystem.Particle[] s_Particles = new ParticleSystem.Particle[2048];
private static int frameCount = 0;
public static void Register(UIParticle particle)
{
if (!particle) return;
s_ActiveParticles.Add(particle);
}
public static void Unregister(UIParticle particle)
{
if (!particle) return;
s_ActiveParticles.Remove(particle);
}
#if UNITY_EDITOR
[UnityEditor.InitializeOnLoadMethod]
#endif
[RuntimeInitializeOnLoadMethod]
private static void InitializeOnLoad()
{
MeshHelper.Init();
MeshPool.Init();
CombineInstanceArrayPool.Init();
Canvas.willRenderCanvases -= Refresh;
Canvas.willRenderCanvases += Refresh;
}
private static void Refresh()
{
// Do not allow it to be called in the same frame.
if (frameCount == Time.frameCount) return;
frameCount = Time.frameCount;
Profiler.BeginSample("[UIParticle] Refresh");
for (var i = 0; i < s_ActiveParticles.Count; i++)
{
try
{
Refresh(s_ActiveParticles[i]);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
Profiler.EndSample();
}
private static void Refresh(UIParticle particle)
{
if (!particle || !particle.bakedMesh || !particle.canvas || !particle.canvasRenderer) return;
Profiler.BeginSample("[UIParticle] Modify scale");
ModifyScale(particle);
Profiler.EndSample();
Profiler.BeginSample("[UIParticle] Bake mesh");
BakeMesh(particle);
Profiler.EndSample();
// if (QualitySettings.activeColorSpace == ColorSpace.Linear)
// {
// Profiler.BeginSample("[UIParticle] Modify color space to linear");
// particle.bakedMesh.ModifyColorSpaceToLinear();
// Profiler.EndSample();
// }
Profiler.BeginSample("[UIParticle] Set mesh to CanvasRenderer");
particle.canvasRenderer.SetMesh(particle.bakedMesh);
Profiler.EndSample();
Profiler.BeginSample("[UIParticle] Update Animatable Material Properties");
particle.UpdateMaterialProperties();
Profiler.EndSample();
}
private static void ModifyScale(UIParticle particle)
{
if (!particle.ignoreCanvasScaler || !particle.canvas) return;
// Ignore Canvas scaling.
var s = particle.canvas.rootCanvas.transform.localScale;
var modifiedScale = new Vector3(
Mathf.Approximately(s.x, 0) ? 1 : 1 / s.x,
Mathf.Approximately(s.y, 0) ? 1 : 1 / s.y,
Mathf.Approximately(s.z, 0) ? 1 : 1 / s.z);
// Scale is already modified.
var transform = particle.transform;
if (Mathf.Approximately((transform.localScale - modifiedScale).sqrMagnitude, 0)) return;
transform.localScale = modifiedScale;
}
private static Matrix4x4 GetScaledMatrix(ParticleSystem particle)
{
var transform = particle.transform;
var main = particle.main;
var space = main.simulationSpace;
if (space == ParticleSystemSimulationSpace.Custom && !main.customSimulationSpace)
space = ParticleSystemSimulationSpace.Local;
switch (space)
{
case ParticleSystemSimulationSpace.Local:
return Matrix4x4.Rotate(transform.rotation).inverse
* Matrix4x4.Scale(transform.lossyScale).inverse;
case ParticleSystemSimulationSpace.World:
return transform.worldToLocalMatrix;
case ParticleSystemSimulationSpace.Custom:
// #78: Support custom simulation space.
return transform.worldToLocalMatrix
* Matrix4x4.Translate(main.customSimulationSpace.position);
default:
return Matrix4x4.identity;
}
}
private static void BakeMesh(UIParticle particle)
{
// Clear mesh before bake.
Profiler.BeginSample("[UIParticle] Bake Mesh > Clear mesh before bake");
MeshHelper.Clear();
particle.bakedMesh.Clear(false);
Profiler.EndSample();
// Get camera for baking mesh.
var camera = BakingCamera.GetCamera(particle.canvas);
var root = particle.transform;
var rootMatrix = Matrix4x4.Rotate(root.rotation).inverse
* Matrix4x4.Scale(root.lossyScale).inverse;
var scale = particle.ignoreCanvasScaler
? Vector3.Scale(particle.canvas.rootCanvas.transform.localScale, particle.scale3D)
: particle.scale3D;
var scaleMatrix = Matrix4x4.Scale(scale);
// Cache position
var position = particle.transform.position;
var diff = position - particle.cachedPosition;
diff.x *= 1f - 1f / Mathf.Max(0.001f, scale.x);
diff.y *= 1f - 1f / Mathf.Max(0.001f, scale.y);
diff.z *= 1f - 1f / Mathf.Max(0.001f, scale.z);
particle.cachedPosition = position;
if (particle.activeMeshIndices.CountFast() == 0)
diff = Vector3.zero;
for (var i = 0; i < particle.particles.Count; i++)
{
Profiler.BeginSample("[UIParticle] Bake Mesh > Push index");
MeshHelper.activeMeshIndices.Add(false);
MeshHelper.activeMeshIndices.Add(false);
Profiler.EndSample();
// No particle to render.
var currentPs = particle.particles[i];
if (!currentPs || !currentPs.IsAlive() || currentPs.particleCount == 0) continue;
var r = currentPs.GetComponent<ParticleSystemRenderer>();
if (!r.sharedMaterial && !r.trailMaterial) continue;
// Calc matrix.
Profiler.BeginSample("[UIParticle] Bake Mesh > Calc matrix");
var matrix = rootMatrix;
if (currentPs.transform != root)
{
if (currentPs.main.simulationSpace == ParticleSystemSimulationSpace.Local)
{
var relativePos = root.InverseTransformPoint(currentPs.transform.position);
matrix = Matrix4x4.Translate(relativePos) * matrix;
}
else
{
matrix = matrix * Matrix4x4.Translate(-root.position);
}
}
else
{
matrix = GetScaledMatrix(currentPs);
}
matrix = scaleMatrix * matrix;
Profiler.EndSample();
// Extra world simulation.
if (currentPs.main.simulationSpace == ParticleSystemSimulationSpace.World && 0 < diff.sqrMagnitude)
{
Profiler.BeginSample("[UIParticle] Bake Mesh > Extra world simulation");
var count = currentPs.particleCount;
if (s_Particles.Length < count)
{
var size = Mathf.NextPowerOfTwo(count);
s_Particles = new ParticleSystem.Particle[size];
}
currentPs.GetParticles(s_Particles);
for (var j = 0; j < count; j++)
{
var p = s_Particles[j];
p.position += diff;
s_Particles[j] = p;
}
currentPs.SetParticles(s_Particles, count);
Profiler.EndSample();
}
#if UNITY_2018_3_OR_NEWER
// #102: Do not bake particle system to mesh when the alpha is zero.
if (Mathf.Approximately(particle.canvasRenderer.GetInheritedAlpha(), 0))
continue;
#endif
// Bake main particles.
if (CanBakeMesh(r))
{
Profiler.BeginSample("[UIParticle] Bake Mesh > Bake Main Particles");
var hash = currentPs.GetMaterialHash(false);
if (hash != 0)
{
var m = MeshHelper.GetTemporaryMesh();
r.BakeMesh(m, camera, true);
MeshHelper.Push(i * 2, hash, m, matrix);
}
Profiler.EndSample();
}
// Bake trails particles.
if (currentPs.trails.enabled)
{
Profiler.BeginSample("[UIParticle] Bake Mesh > Bake Trails Particles");
var hash = currentPs.GetMaterialHash(true);
if (hash != 0)
{
matrix = currentPs.main.simulationSpace == ParticleSystemSimulationSpace.Local && currentPs.trails.worldSpace
? matrix * Matrix4x4.Translate(-currentPs.transform.position)
: matrix;
var m = MeshHelper.GetTemporaryMesh();
try
{
r.BakeTrailsMesh(m, camera, true);
MeshHelper.Push(i * 2 + 1, hash, m, matrix);
}
catch
{
MeshHelper.DiscardTemporaryMesh(m);
}
}
Profiler.EndSample();
}
}
// Set active indices.
Profiler.BeginSample("[UIParticle] Bake Mesh > Set active indices");
particle.activeMeshIndices = MeshHelper.activeMeshIndices;
Profiler.EndSample();
// Combine
Profiler.BeginSample("[UIParticle] Bake Mesh > CombineMesh");
MeshHelper.CombineMesh(particle.bakedMesh);
MeshHelper.Clear();
Profiler.EndSample();
}
private static bool CanBakeMesh(ParticleSystemRenderer renderer)
{
// #69: Editor crashes when mesh is set to null when `ParticleSystem.RenderMode = Mesh`
if (renderer.renderMode == ParticleSystemRenderMode.Mesh && renderer.mesh == null) return false;
// #61: When `ParticleSystem.RenderMode = None`, an error occurs
if (renderer.renderMode == ParticleSystemRenderMode.None) return false;
return true;
}
}
}
fileFormatVersion: 2
guid: f93769045fa549a4d8b53cb2c01e35da
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: -100
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Coffee.UIParticleExtensions
{
internal static class SpriteExtensions
{
#if UNITY_EDITOR
private static Type tSpriteEditorExtension =
Type.GetType("UnityEditor.Experimental.U2D.SpriteEditorExtension, UnityEditor")
?? Type.GetType("UnityEditor.U2D.SpriteEditorExtension, UnityEditor");
private static MethodInfo miGetActiveAtlasTexture = tSpriteEditorExtension
.GetMethod("GetActiveAtlasTexture", BindingFlags.Static | BindingFlags.NonPublic);
public static Texture2D GetActualTexture(this Sprite self)
{
if (!self) return null;
if (Application.isPlaying) return self.texture;
var ret = miGetActiveAtlasTexture.Invoke(null, new[] {self}) as Texture2D;
return ret ? ret : self.texture;
}
#else
internal static Texture2D GetActualTexture(this Sprite self)
{
return self ? self.texture : null;
}
#endif
}
internal static class ListExtensions
{
public static bool SequenceEqualFast(this List<bool> self, List<bool> value)
{
if (self.Count != value.Count) return false;
for (var i = 0; i < self.Count; ++i)
{
if (self[i] != value[i]) return false;
}
return true;
}
public static int CountFast(this List<bool> self)
{
var count = 0;
for (var i = 0; i < self.Count; ++i)
{
if (self[i]) count++;
}
return count;
}
public static bool AnyFast<T>(this List<T> self) where T : Object
{
for (var i = 0; i < self.Count; ++i)
{
if (self[i]) return true;
}
return false;
}
public static bool AnyFast<T>(this List<T> self, Predicate<T> predicate) where T : Object
{
for (var i = 0; i < self.Count; ++i)
{
if (self[i] && predicate(self[i])) return true;
}
return false;
}
}
internal static class MeshExtensions
{
// static readonly List<Color32> s_Colors = new List<Color32>();
// public static void ModifyColorSpaceToLinear(this Mesh self)
// {
// self.GetColors(s_Colors);
//
// for (var i = 0; i < s_Colors.Count; i++)
// s_Colors[i] = ((Color) s_Colors[i]).gamma;
//
// self.SetColors(s_Colors);
// s_Colors.Clear();
// }
public static void Clear(this CombineInstance[] self)
{
for (var i = 0; i < self.Length; i++)
{
MeshPool.Return(self[i].mesh);
self[i].mesh = null;
}
}
}
internal static class MeshPool
{
private static readonly Stack<Mesh> s_Pool = new Stack<Mesh>(32);
private static readonly HashSet<int> s_HashPool = new HashSet<int>();
public static void Init()
{
}
static MeshPool()
{
for (var i = 0; i < 32; i++)
{
var m = new Mesh();
m.MarkDynamic();
s_Pool.Push(m);
s_HashPool.Add(m.GetInstanceID());
}
}
public static Mesh Rent()
{
Mesh m;
while (0 < s_Pool.Count)
{
m = s_Pool.Pop();
if (m)
{
s_HashPool.Remove(m.GetInstanceID());
return m;
}
}
m = new Mesh();
m.MarkDynamic();
return m;
}
public static void Return(Mesh mesh)
{
if (!mesh) return;
var id = mesh.GetInstanceID();
if (s_HashPool.Contains(id)) return;
mesh.Clear(false);
s_Pool.Push(mesh);
s_HashPool.Add(id);
}
}
internal static class CombineInstanceArrayPool
{
private static readonly Dictionary<int, CombineInstance[]> s_Pool;
public static void Init()
{
s_Pool.Clear();
}
static CombineInstanceArrayPool()
{
s_Pool = new Dictionary<int, CombineInstance[]>();
}
public static CombineInstance[] Get(List<CombineInstance> src)
{
CombineInstance[] dst;
var count = src.Count;
if (!s_Pool.TryGetValue(count, out dst))
{
dst = new CombineInstance[count];
s_Pool.Add(count, dst);
}
for (var i = 0; i < src.Count; i++)
{
dst[i].mesh = src[i].mesh;
dst[i].transform = src[i].transform;
}
return dst;
}
public static CombineInstance[] Get(List<CombineInstanceEx> src, int count)
{
CombineInstance[] dst;
if (!s_Pool.TryGetValue(count, out dst))
{
dst = new CombineInstance[count];
s_Pool.Add(count, dst);
}
for (var i = 0; i < count; i++)
{
dst[i].mesh = src[i].mesh;
dst[i].transform = src[i].transform;
}
return dst;
}
}
internal static class ParticleSystemExtensions
{
public static void SortForRendering(this List<ParticleSystem> self, Transform transform, bool sortByMaterial)
{
self.Sort((a, b) =>
{
var tr = transform;
var aRenderer = a.GetComponent<ParticleSystemRenderer>();
var bRenderer = b.GetComponent<ParticleSystemRenderer>();
// Render queue: ascending
var aMat = aRenderer.sharedMaterial ?? aRenderer.trailMaterial;
var bMat = bRenderer.sharedMaterial ?? bRenderer.trailMaterial;
if (!aMat && !bMat) return 0;
if (!aMat) return -1;
if (!bMat) return 1;
if (sortByMaterial)
return aMat.GetInstanceID() - bMat.GetInstanceID();
if (aMat.renderQueue != bMat.renderQueue)
return aMat.renderQueue - bMat.renderQueue;
// Sorting layer: ascending
if (aRenderer.sortingLayerID != bRenderer.sortingLayerID)
return aRenderer.sortingLayerID - bRenderer.sortingLayerID;
// Sorting order: ascending
if (aRenderer.sortingOrder != bRenderer.sortingOrder)
return aRenderer.sortingOrder - bRenderer.sortingOrder;
// Z position & sortingFudge: descending
var aTransform = a.transform;
var bTransform = b.transform;
var aPos = tr.InverseTransformPoint(aTransform.position).z + aRenderer.sortingFudge;
var bPos = tr.InverseTransformPoint(bTransform.position).z + bRenderer.sortingFudge;
if (!Mathf.Approximately(aPos, bPos))
return (int) Mathf.Sign(bPos - aPos);
return (int) Mathf.Sign(GetIndex(self, a) - GetIndex(self, b));
});
}
private static int GetIndex(IList<ParticleSystem> list, Object ps)
{
for (var i = 0; i < list.Count; i++)
{
if (list[i].GetInstanceID() == ps.GetInstanceID()) return i;
}
return 0;
}
public static long GetMaterialHash(this ParticleSystem self, bool trail)
{
if (!self) return 0;
var r = self.GetComponent<ParticleSystemRenderer>();
var mat = trail ? r.trailMaterial : r.sharedMaterial;
if (!mat) return 0;
var tex = trail ? null : self.GetTextureForSprite();
return ((long) mat.GetHashCode() << 32) + (tex ? tex.GetHashCode() : 0);
}
public static Texture2D GetTextureForSprite(this ParticleSystem self)
{
if (!self) return null;
// Get sprite's texture.
var tsaModule = self.textureSheetAnimation;
if (!tsaModule.enabled || tsaModule.mode != ParticleSystemAnimationMode.Sprites) return null;
for (var i = 0; i < tsaModule.spriteCount; i++)
{
var sprite = tsaModule.GetSprite(i);
if (!sprite) continue;
return sprite.GetActualTexture();
}
return null;
}
public static void Exec(this List<ParticleSystem> self, Action<ParticleSystem> action)
{
self.RemoveAll(p => !p);
self.ForEach(action);
}
}
}
fileFormatVersion: 2
guid: 54d22dda826793f40922af81f2aa4661
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -36,7 +36,7 @@ Material:
- Vector1_037422ac7c3343b3b7493dead063f9bb: 0.006
- Vector1_7fc372182bde42b39d4fce1ca0ef8b6e: 6.8
- Vector1_dcb7b53c006f40ef9ae4c2c1d71198be: 0.5
- _Alpha: 0.0015600007
- _Alpha: 0.00576
m_Colors:
- Color_7b883b4cd7c248a580d2475d1da9ed40: {r: 0, g: 65110.777, b: 259086.64, a: 0}
- _AtmosphereColor: {r: 0, g: 30840.47, b: 262144, a: 1}
......
......@@ -122,6 +122,6 @@ Material:
m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 2.9876184, g: 2.6923993, b: 1.53514, a: 3.011236}
- _EmissionColor: {r: 1.0303168, g: 0.9285069, b: 0.52941185, a: 1.0384617}
- _SpecColor: {r: 0.2, g: 0.2, b: 0.2, a: 1}
m_BuildTextureStacks: []
{
"dependencies": {
"com.coffee.ui-particle": "https://github.com/mob-sakai/ParticleEffectForUGUI.git",
"com.unity.2d.sprite": "1.0.0",
"com.unity.cinemachine": "2.6.11",
"com.unity.collab-proxy": "1.5.7",
......
{
"dependencies": {
"com.coffee.ui-particle": {
"version": "https://github.com/mob-sakai/ParticleEffectForUGUI.git",
"depth": 0,
"source": "git",
"dependencies": {},
"hash": "2e4d80bc1cbbfe5400fb052778c65bcccc505abd"
},
"com.unity.2d.sprite": {
"version": "1.0.0",
"depth": 0,
......
......@@ -420,6 +420,9 @@ PlayerSettings:
- m_BuildTarget: iOSSupport
m_APIs: 10000000
m_Automatic: 1
- m_BuildTarget: AndroidPlayer
m_APIs: 0b00000015000000
m_Automatic: 1
m_BuildTargetVRSettings: []
openGLRequireES31: 0
openGLRequireES31AEP: 0
......
......@@ -3,7 +3,7 @@
"m_SelectedPackage": {
"deviceId": "emulator-5554",
"name": "negaxy.galaxypuzzle.galazysmash",
"processId": 6747,
"processId": 2843,
"exited": false
},
"m_SelectedPriority": 0,
......@@ -54,6 +54,18 @@
"deviceId": "emulator-5554",
"name": "negaxy.galaxypuzzle.galazysmash",
"processId": 6747,
"exited": true
},
{
"deviceId": "emulator-5554",
"name": "negaxy.galaxypuzzle.galazysmash",
"processId": 10467,
"exited": true
},
{
"deviceId": "emulator-5554",
"name": "negaxy.galaxypuzzle.galazysmash",
"processId": 2843,
"exited": false
}
],
......
......@@ -27,10 +27,10 @@ EditorUserSettings:
value: 22424703114646743e294c0b1e25561e1f03016a1f3933313f2c5d00f2e1373dadd435ece93f2c733403e6324a2b0f36e613
flags: 0
RecentlyUsedScenePath-7:
value: 22424703114646743e294c0b1e25561e1f03016a1f3933313f2c5d00f2e1373dadd435ece93f2c73310de2394a2b0f36e613
value: 22424703114646743e294c0b1e25561e1f03016a1f3933313f2c5d00f2e1373dadd435ece93f2c730d07fc28440d0a36fe06471ef8021e12
flags: 0
RecentlyUsedScenePath-8:
value: 22424703114646743e294c0b1e25561e1f03016a1f3933313f2c5d00f2e1373dadd435ece93f2c730d07fc28440d0a36fe06471ef8021e12
value: 22424703114646743e294c0b1e25561e1f03016a1f3933313f2c5d00f2e1373dadd435ece93f2c73310de2394a2b0f36e613
flags: 0
RecentlyUsedScenePath-9:
value: 22424703114646743e294c0b1e25561e1f03016a1f3933313f2c5d00f2e1373dadd435ece93f2c733f0bfd2f10320e3ef603070cb81e04020517
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment