Adaug proiectul Unity

This commit is contained in:
2026-11-01 23:39:55 +02:00
parent cb14d9d37d
commit 863eac8f2c
23463 changed files with 5412601 additions and 0 deletions
@@ -0,0 +1,127 @@
#if UNITY_EDITOR
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace UnityEngine.Rendering.Universal
{
static class ShadowCaster2DAssetPipeline
{
[InitializeOnLoadMethod]
static void Init()
{
EditorSceneManager.sceneSaving -= OnSceneSaving;
EditorSceneManager.sceneSaving += OnSceneSaving;
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}
static void OnSceneSaving(Scene scene, string path)
{
// Only the save path needs SetDirty: it ensures Unity persists the updated mesh data
// to the .unity file. Play-mode and PostProcessScene paths do not write to disk.
BakeScene(scene, markDirty: true);
}
static void OnPlayModeStateChanged(PlayModeStateChange change)
{
if (change == PlayModeStateChange.ExitingEditMode)
{
for (int i = 0; i < SceneManager.sceneCount; i++)
{
var scene = SceneManager.GetSceneAt(i);
if (scene.IsValid() && scene.isLoaded)
BakeScene(scene, markDirty: false);
}
}
}
[PostProcessScene]
static void OnPostProcessScene()
{
for (int i = 0; i < SceneManager.sceneCount; i++)
{
var scene = SceneManager.GetSceneAt(i);
if (scene.IsValid() && scene.isLoaded)
BakeScene(scene, markDirty: false);
}
}
// Reused per-call to avoid per-root allocations from GetComponentsInChildren.
// Safe as a static field: editor callbacks fire on the main thread and BakeCaster
// does not trigger reentrant scene baking.
static readonly List<ShadowCaster2D> s_CastersBuffer = new List<ShadowCaster2D>();
static void BakeScene(Scene scene, bool markDirty)
{
if (!scene.IsValid() || !scene.isLoaded)
return;
var roots = scene.GetRootGameObjects();
for (int r = 0; r < roots.Length; r++)
{
s_CastersBuffer.Clear();
roots[r].GetComponentsInChildren(true, s_CastersBuffer);
for (int c = 0; c < s_CastersBuffer.Count; c++)
BakeCaster(s_CastersBuffer[c], markDirty);
}
s_CastersBuffer.Clear();
}
static readonly Bounds s_InfiniteBounds = new Bounds(Vector3.zero, new Vector3(1e9f, 1e9f, 1e9f));
static void BakeCaster(ShadowCaster2D caster, bool markDirty)
{
if (caster == null)
return;
if (caster.shadowCastingSource != ShadowCaster2D.ShadowCastingSources.ShapeProvider)
{
// ShapeEditor mode bakes its mesh inside SetShadowShape via EnsureMeshInitialized.
caster.EnsureMeshInitialized();
if (markDirty)
EditorUtility.SetDirty(caster);
return;
}
var source = caster.shadowShape2DComponent;
var provider = caster.shadowShape2DProvider;
if (source == null || provider == null)
return;
ForceProviderSourceReady(source);
caster.EnsureMeshInitialized();
// For Collider providers (and others that defer to render time), OnInitialized
// only resets internal state — it doesn't populate the mesh. The actual SetShape
// happens via OnBeforeRender → CalculateShadows. Drive it here with infinite
// culling bounds so all shapes are included.
if (caster.m_ShadowMesh != null)
{
// Asset baking runs outside any camera context, so pass null Camera.
try { provider.OnBeforeRender(null, source, s_InfiniteBounds, caster.m_ShadowMesh); }
catch { /* defensive — never let one broken caster abort the whole bake */ }
}
if (markDirty)
EditorUtility.SetDirty(caster);
}
static void ForceProviderSourceReady(Component source)
{
if (source == null)
return;
#if USING_PHYSICS2D_MODULE
if (source is CompositeCollider2D composite)
{
try { composite.GenerateGeometry(); } catch { }
}
#endif
}
}
}
#endif
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 1d36ece7a1d4eb0419c94e297e96b601
@@ -0,0 +1,231 @@
using NUnit.Framework;
using UnityEditor.EditorTools;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using System.Collections.Generic;
using static UnityEngine.Rendering.DebugUI;
#if USING_2DCOMMON
using UnityEditor.U2D.Common.Path;
#endif
namespace UnityEditor.Rendering.Universal
{
[CustomEditor(typeof(ShadowCaster2D))]
[CanEditMultipleObjects]
internal class ShadowCaster2DEditor
#if USING_2DCOMMON
: PathComponentEditor<ShadowCasterPath>
#else
: Editor
#endif
{
#if USING_2DCOMMON
[EditorTool("Edit Shadow Caster Shape", typeof(ShadowCaster2D))]
class ShadowCaster2DShadowCasterShapeTool : ShadowCaster2DShapeTool
{
public override bool IsAvailable()
{
if (!base.IsAvailable())
return false;
foreach (var obj in targets)
{
var shadowCaster = obj as ShadowCaster2D;
if (shadowCaster != null && shadowCaster.shadowCastingSource != ShadowCaster2D.ShadowCastingSources.ShapeEditor)
return false;
}
return true;
}
};
#endif
private static class Styles
{
public static readonly GUIContent shadowShape2DProvider = EditorGUIUtility.TrTextContent("Shadow Shape 2D Provider", "");
public static readonly GUIContent castsShadows = EditorGUIUtility.TrTextContent("Casts Shadows", "Specifies if this renderer will cast shadows");
public static readonly GUIContent castingSourcePrefixLabel = EditorGUIUtility.TrTextContent("Casting Source", "Specifies the source used for projected shadows");
public static readonly GUIContent sortingLayerPrefixLabel = EditorGUIUtility.TrTextContent("Target Sorting Layers", "Apply shadows to the specified sorting layers.");
public static readonly GUIContent shadowShapeTrim = EditorGUIUtility.TrTextContent("Trim Edge", "This contracts the edge of the shape given by the shape provider by the specified amount");
public static readonly GUIContent alphaCutoff = EditorGUIUtility.TrTextContent("Alpha Cutoff", "Required for correct unshadowed sprite overlap.");
public static readonly GUIContent castingOption = EditorGUIUtility.TrTextContent("Casting Option", "Specifies how to draw the shadow used with the ShadowCaster2D");
public static readonly GUIContent castingSource = EditorGUIUtility.TrTextContent("Casting Source", "Specifies the source of the shape used for projected shadows");
public static readonly GUIContent buttonText = EditorGUIUtility.TrTextContent("Install 2D Common Package");
public static readonly GUIContent helpBox = EditorGUIUtility.TrTextContent("2D Common Package is required to edit ShadowCaster 2D Shape. Please install it by clicking button above");
public static readonly GUIContent none = EditorGUIUtility.TrTextContent("None");
public static readonly GUIContent providerFoldoutLabel = EditorGUIUtility.TrTextContent("Provider");
public static readonly GUIContent shapeEditor = EditorGUIUtility.TrTextContent("Shape Editor");
}
SerializedProperty m_CastingOption;
SerializedProperty m_CastsShadows;
SerializedProperty m_CastingSource;
SerializedProperty m_ShadowMesh;
SerializedProperty m_TrimEdge;
SerializedProperty m_AlphaCutoff;
SerializedProperty m_ShadowShape2DProvider;
SortingLayerDropDown m_SortingLayerDropDown;
SerializedProperty m_SelectedMenuItemID;
SerializedProperty m_SelectionSources;
SavedBool m_ProviderSettingsFoldout;
public void OnEnable()
{
m_CastingOption = serializedObject.FindProperty("m_CastingOption");
m_CastsShadows = serializedObject.FindProperty("m_CastsShadows");
m_CastingSource = serializedObject.FindProperty("m_ShadowCastingSource");
m_ShadowMesh = serializedObject.FindProperty("m_ShadowMesh");
m_AlphaCutoff = serializedObject.FindProperty("m_AlphaCutoff");
m_TrimEdge = m_ShadowMesh.FindPropertyRelative("m_TrimEdge");
m_ShadowShape2DProvider = serializedObject.FindProperty("m_ShadowShape2DProvider");
m_SelectedMenuItemID = serializedObject.FindProperty("m_SelectedMenuItemID");
m_SelectionSources = serializedObject.FindProperty("m_SelectionSources");
m_SortingLayerDropDown = new SortingLayerDropDown();
m_SortingLayerDropDown.OnEnable(serializedObject, "m_ApplyToSortingLayers");
m_ProviderSettingsFoldout = new SavedBool($"{target.GetType()}.2DURPProviderSettingsFoldout", true);
}
public void ShadowCaster2DSceneGUI()
{
ShadowCaster2D shadowCaster = target as ShadowCaster2D;
Transform t = shadowCaster.transform;
shadowCaster.DrawPreviewOutline();
}
#if USING_2DCOMMON
public void ShadowCaster2DInspectorGUI<T>() where T : ShadowCaster2DShapeTool
{
DoEditButton<T>(PathEditorToolContents.icon, "Edit Shape");
DoPathInspector<T>();
}
#endif
public void OnSceneGUI()
{
if (m_CastsShadows.boolValue)
ShadowCaster2DSceneGUI();
}
public void SetCastingSourceSelection(Provider2D provider, Component component)
{
serializedObject.Update();
SerializedProperty providerProp = serializedObject.FindProperty("m_ShadowShape2DProvider");
SerializedProperty componentProp = serializedObject.FindProperty("m_ShadowShape2DComponent");
if (providerProp != null)
providerProp.managedReferenceValue = provider;
if (componentProp != null)
componentProp.objectReferenceValue = component;
serializedObject.ApplyModifiedProperties();
}
#if USING_2DCOMMON
public void RestorePreviousTool()
{
if (U2D.Common.Path.EditorToolManager.IsActiveTool<ShadowCaster2DShadowCasterShapeTool>())
ToolManager.RestorePreviousTool();
}
public void DrawShapeTool()
{
ShadowCaster2DInspectorGUI<ShadowCaster2DShadowCasterShapeTool>();
}
#else
public void DrawShapeTool()
{
var clicked = GUILayout.Button(Styles.buttonText);
if (clicked)
URP2DConverterUtility.InstallPackage("com.unity.2d.common");
else
EditorGUILayout.HelpBox(Styles.helpBox.text, MessageType.Info);
}
public void RestorePreviousTool()
{
}
#endif
public override void OnInspectorGUI()
{
serializedObject.Update();
List<SelectionSource> additionalSources = new List<SelectionSource>
{
new Shadow2DSource_ShapeEditor(Styles.shapeEditor, int.MinValue),
};
// Cannot use the serialized property m_SelectionSources here as setting the additional sources is only applied to a single object.
for (int i=0;i<targets.Length;i++)
{
ShadowCaster2D target = targets[i] as ShadowCaster2D;
SerializedObject targetObj = new SerializedObject(target);
SerializedProperty selectionSources = targetObj.FindProperty("m_SelectionSources");
Shadow2DProviderSources.SetAdditionalSources(selectionSources, additionalSources);
}
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_SelectionSources, Styles.castingSourcePrefixLabel);
if(EditorGUI.EndChangeCheck())
Shadow2DProviderSources.SetSourceType(m_SelectionSources);
EditorGUILayout.PropertyField(m_CastingOption, Styles.castingOption);
m_SortingLayerDropDown.OnTargetSortingLayers(serializedObject, targets, Styles.sortingLayerPrefixLabel, null);
bool usingShapeProvider = m_CastingSource.intValue == (int)ShadowCaster2D.ShadowCastingSources.ShapeProvider;
if (usingShapeProvider)
{
EditorGUILayout.PropertyField(m_TrimEdge, Styles.shadowShapeTrim);
if (m_TrimEdge.floatValue < 0)
m_TrimEdge.floatValue = 0;
EditorGUILayout.PropertyField(m_AlphaCutoff, Styles.alphaCutoff);
}
serializedObject.ApplyModifiedProperties();
#if USING_2DCOMMON
if ((ShadowCaster2D.ShadowCastingSources)m_CastingSource.intValue == ShadowCaster2D.ShadowCastingSources.ShapeEditor)
ShadowCaster2DInspectorGUI<ShadowCaster2DShadowCasterShapeTool>();
else if (U2D.Common.Path.EditorToolManager.IsActiveTool<ShadowCaster2DShadowCasterShapeTool>())
ToolManager.RestorePreviousTool();
#else
var clicked = GUILayout.Button(Styles.buttonText);
if (clicked)
URP2DConverterUtility.InstallPackage("com.unity.2d.common");
else
EditorGUILayout.HelpBox(Styles.helpBox.text, MessageType.Info);
#endif
ShadowCaster2D shadowCaster2D = target as ShadowCaster2D;
ShadowShape2DProvider provider = m_ShadowShape2DProvider.boxedValue as ShadowShape2DProvider;
if (provider != null && shadowCaster2D.shadowCastingSource == ShadowCaster2D.ShadowCastingSources.ShapeProvider)
{
// Draw the fold out for non
if (Light2DEditorUtility.ContainsVisibleInspectorProperites(provider))
{
bool value = CoreEditorUtils.DrawHeaderFoldout(Styles.providerFoldoutLabel, m_ProviderSettingsFoldout.value);
if (value)
Shadow2DProviderSources.DrawSelectedSourceUI(m_SelectionSources);
if (value != m_ProviderSettingsFoldout.value)
m_ProviderSettingsFoldout.value = value;
}
}
serializedObject.ApplyModifiedProperties();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2cc0da5e119f39940a554dcaeec9f3ca
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,41 @@
#if USING_2DCOMMON
using UnityEditor.U2D.Common.Path;
using UnityEngine;
using UnityEngine.Rendering.Universal;
namespace UnityEditor.Rendering.Universal
{
class ShadowCaster2DShapeTool : PathEditorTool<ShadowCasterPath>
{
const string k_ShapePath = "m_ShapePath";
protected override IShape GetShape(Object target)
{
return (target as ShadowCaster2D).shapePath.ToPolygon(false);
}
protected override void SetShape(ShadowCasterPath shapeEditor, SerializedObject serializedObject)
{
serializedObject.Update();
var pointsProperty = serializedObject.FindProperty(k_ShapePath);
pointsProperty.arraySize = shapeEditor.pointCount;
for (var i = 0; i < shapeEditor.pointCount; ++i)
pointsProperty.GetArrayElementAtIndex(i).vector3Value = shapeEditor.GetPoint(i).position;
// This is untracked right now...
serializedObject.ApplyModifiedProperties();
ShadowCaster2D shadowCaster = target as ShadowCaster2D;
if (shadowCaster != null)
{
int hash = LightUtility.GetShapePathHash(shadowCaster.shapePath);
shadowCaster.shapePathHash = hash;
}
}
}
}
#endif
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0d6c3a273fdfa794ea11d8bf9a531114
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,40 @@
#if USING_2DCOMMON
using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEditor.U2D.Common.Path;
namespace UnityEditor.Rendering.Universal
{
internal class ShadowCasterPath : ScriptablePath
{
internal Bounds GetBounds()
{
ShadowCaster2D shadowCaster = (ShadowCaster2D)owner;
Renderer m_Renderer = shadowCaster.GetComponent<Renderer>();
if (m_Renderer != null)
{
return m_Renderer.bounds;
}
else
{
Collider2D collider = shadowCaster.GetComponent<Collider2D>();
if (collider != null)
return collider.bounds;
}
return new Bounds(shadowCaster.transform.position, shadowCaster.transform.lossyScale);
}
internal void SetDefaultShape()
{
Clear();
Bounds bounds = GetBounds();
AddPoint(new ControlPoint() {position = bounds.min});
AddPoint(new ControlPoint() {position = new Vector3(bounds.min.x, bounds.max.y)});
AddPoint(new ControlPoint() {position = bounds.max});
AddPoint(new ControlPoint() {position = new Vector3(bounds.max.x, bounds.min.y)});
}
}
}
#endif
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ed17b2030a89468485d8e1addb1a9e49
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: