Skip to content

Commit

Permalink
Merge pull request #159 from Algoryx/fix/compatibility-updates
Browse files Browse the repository at this point in the history
Draft: Update scripts for Unity 6.0
  • Loading branch information
FilipAlg authored Aug 9, 2024
2 parents e24cfc7 + c7cca8c commit 7e0df63
Show file tree
Hide file tree
Showing 31 changed files with 214 additions and 178 deletions.
8 changes: 0 additions & 8 deletions AGXUnity/IFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,23 +237,15 @@ public static Quaternion CalculateLocalRotation( GameObject gameObject, Quaterni
if ( gameObject == null )
return worldRotation;

#if UNITY_2018_1_OR_NEWER
return ( Quaternion.Inverse( gameObject.transform.rotation ) * worldRotation ).normalized;
#else
return ( Quaternion.Inverse( gameObject.transform.rotation ) * worldRotation ).Normalize();
#endif
}

public static Quaternion CalculateWorldRotation( GameObject gameObject, Quaternion localRotation )
{
if ( gameObject == null )
return localRotation;

#if UNITY_2018_1_OR_NEWER
return ( gameObject.transform.rotation * localRotation ).normalized;
#else
return ( gameObject.transform.rotation * localRotation ).Normalize();
#endif
}
}
}
8 changes: 0 additions & 8 deletions AGXUnity/IO/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@ public static FileInfo FindFile( string filename,
/// <returns>Path to the plugins folder.</returns>
public static string GetPlayerPluginPath( string dataPath )
{
#if UNITY_2019_3_OR_NEWER
return dataPath + "/Plugins/x86_64";
#else
return dataPath + "/Plugins";
#endif
}

/// <summary>
Expand Down Expand Up @@ -329,11 +325,7 @@ private static void OnEditorCLI()
OnCLI();
}
#else
#if UNITY_2019_1_OR_NEWER
[UnityEngine.RuntimeInitializeOnLoadMethod( UnityEngine.RuntimeInitializeLoadType.BeforeSplashScreen )]
#else
[UnityEngine.RuntimeInitializeOnLoadMethod( UnityEngine.RuntimeInitializeLoadType.BeforeSceneLoad )]
#endif
private static void OnPlayerCli()
{
OnCLI();
Expand Down
2 changes: 0 additions & 2 deletions AGXUnity/IO/StlFileImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,7 @@ public static explicit operator Mesh( MeshData meshData )
mesh.SetTriangles( meshData.Triangles, 0 );
mesh.normals = meshData.CalculateNormals();
mesh.RecalculateTangents();
#if UNITY_2019_1_OR_NEWER
mesh.Optimize();
#endif

meshData.TriangleNormals.Clear();
meshData.Vertices.Clear();
Expand Down
4 changes: 4 additions & 0 deletions AGXUnity/Model/ConveyorBelt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ public bool Add( GameObject roller )
var alreadyPresentTrackWheels = roller.GetComponents<TrackWheel>();
if ( alreadyPresentTrackWheels.Length > 0 ) {
// Don't think disabled tracks will appear here.
#if UNITY_6000_0_OR_NEWER
var tracks = FindObjectsByType<Track>(FindObjectsSortMode.None);
#else
var tracks = FindObjectsOfType<Track>();
#endif
if ( tracks.Any( track => alreadyPresentTrackWheels.Any( wheel => track.Contains( wheel ) ) ) ) {
Debug.LogWarning( $"Roller {roller.name} already has {roller.GetComponents<TrackWheel>().Length} TrackWheel components " +
"belonging to other belt/track instances." );
Expand Down
4 changes: 0 additions & 4 deletions AGXUnity/Model/DeformableTerrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,7 @@ private void UpdateHeights( agxTerrain.ModifiedVerticesVector modifiedVertices )
OnModification?.Invoke( Native, index, Terrain, unityIndex );
}

#if UNITY_2019_1_OR_NEWER
TerrainData.SyncHeightmap();
#else
Terrain.ApplyDelayedHeightmapModification();
#endif
}

private GUIStyle m_textLabelStyle = null;
Expand Down
7 changes: 6 additions & 1 deletion AGXUnity/Model/DeformableTerrainFailureVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ public bool Remove( DeformableTerrainBase terrain )

protected override bool Initialize()
{
if ( AddAllTerrainsOnStart )
if ( AddAllTerrainsOnStart ) {
#if UNITY_6000_0_OR_NEWER
m_terrains.UnionWith( FindObjectsByType<DeformableTerrainBase>(FindObjectsSortMode.None) );
#else
m_terrains.UnionWith( FindObjectsOfType<DeformableTerrainBase>() );
#endif
}
return base.Initialize();
}

Expand Down
5 changes: 5 additions & 0 deletions AGXUnity/Model/DeformableTerrainShovel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,13 @@ protected override bool Initialize()

private void AddToAllTerrains()
{
#if UNITY_6000_0_OR_NEWER
foreach ( var terr in FindObjectsByType<DeformableTerrainBase>(FindObjectsSortMode.None) )
terr.Add( this );
#else
foreach ( var terr in FindObjectsOfType<DeformableTerrainBase>() )
terr.Add( this );
#endif
}

protected override void OnDestroy()
Expand Down
4 changes: 4 additions & 0 deletions AGXUnity/PickHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ private void OnSimulationPre()
var raycastBody = closestGeometryContact.rigidBody( 0 );
RigidBody body = null;
if ( raycastBody != null && raycastBody.getMotionControl() == agx.RigidBody.MotionControl.DYNAMICS ) {
#if UNITY_6000_0_OR_NEWER
var bodies = FindObjectsByType<RigidBody>(FindObjectsSortMode.None);
#else
var bodies = FindObjectsOfType<RigidBody>();
#endif
for ( int i = 0; body == null && i < bodies.Length; ++i )
if ( bodies[ i ].Native != null && bodies[ i ].Native.getUuid().str() == raycastBody.getUuid().str() )
body = bodies[ i ];
Expand Down
18 changes: 12 additions & 6 deletions AGXUnity/Rendering/DebugRenderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,23 @@ protected void Update()

// Shapes with inactive game objects will be updated below when we're
// traversing all children.
FindObjectsOfType<Collide.Shape>().ToList().ForEach(
#if UNITY_6000_0_OR_NEWER
FindObjectsByType<Collide.Shape>( FindObjectsInactive.Include, FindObjectsSortMode.None ).ToList().ForEach(
shape => SynchronizeShape( shape )
);

FindObjectsOfType<Constraint>().ToList().ForEach(
FindObjectsByType<Constraint>( FindObjectsInactive.Include, FindObjectsSortMode.None ).ToList().ForEach(
constraint => constraint.AttachmentPair.Synchronize()
);
#else
FindObjectsOfType<Collide.Shape>( true ).ToList().ForEach(
shape => SynchronizeShape( shape )
);

FindObjectsOfType<Constraint>( true ).ToList().ForEach(
constraint => constraint.AttachmentPair.Synchronize()
);
#endif

List<GameObject> gameObjectsToDestroy = new List<GameObject>();
foreach ( var node in Children ) {
Expand All @@ -349,10 +359,6 @@ protected void Update()

if ( proxy.Target == null )
gameObjectsToDestroy.Add( node );
// FindObjectsOfType will not include the Shape if its game object is inactive.
// We're handling that shape here instead.
else if ( !proxy.Target.activeInHierarchy && proxy.Component is Collide.Shape )
SynchronizeShape( proxy.Component as Collide.Shape );
}

while ( gameObjectsToDestroy.Count > 0 ) {
Expand Down
28 changes: 6 additions & 22 deletions AGXUnity/RigidBodyEmitterSink.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;

namespace AGXUnity
Expand Down Expand Up @@ -145,7 +144,11 @@ protected override bool Initialize()
return false;
}

m_emitters = FindObjectsIncudingDisabledOfType<RigidBodyEmitter>();
#if UNITY_6000_0_OR_NEWER
m_emitters = FindObjectsByType<RigidBodyEmitter>( FindObjectsInactive.Include, FindObjectsSortMode.None );
#else
m_emitters = FindObjectsOfType<RigidBodyEmitter>(true);
#endif

if ( m_emitters.Length == 0 ) {
m_emitters = null;
Expand Down Expand Up @@ -198,25 +201,6 @@ protected override bool Initialize()
return true;
}

public static T[] FindObjectsIncudingDisabledOfType<T>()
where T : Component
{
#if UNITY_2020_1_OR_NEWER
return FindObjectsOfType<T>( true );
#else
var components = new List<T>();
for ( int i = 0; i < UnityEngine.SceneManagement.SceneManager.sceneCount; ++i ) {
var scene = UnityEngine.SceneManagement.SceneManager.GetSceneAt( i );
if ( !scene.isLoaded )
continue;
foreach ( var go in UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects() )
components.AddRange( go.GetComponentsInChildren<T>( true ) );
}

return components.ToArray();
#endif
}

private void Reset()
{
Shape = GetComponent<Collide.Shape>();
Expand Down
6 changes: 5 additions & 1 deletion AGXUnity/UniqueGameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ private static T FindOrCreateInstance( bool onlyFind = false )
// object in, e.g., OnDestroy/Unloading of as scene.
s_wasCreated = false;

Instance = FindObjectOfType<T>();
#if UNITY_6000_0_OR_NEWER
Instance = FindAnyObjectByType<T>( FindObjectsInactive.Include );
#else
Instance = FindObjectOfType<T>(true);
#endif
if ( !onlyFind && s_instance == null )
Instance = new GameObject( typeof( T ).FullName ).AddComponent<T>();

Expand Down
4 changes: 2 additions & 2 deletions AGXUnity/Utils/RenderingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public enum PipelineType
/// <returns>An enum representing the render pipeline currently in use</returns>
public static PipelineType DetectPipeline()
{
if ( GraphicsSettings.renderPipelineAsset != null ) {
if ( GraphicsSettings.defaultRenderPipeline != null ) {
// SRP
var srpType = GraphicsSettings.renderPipelineAsset.GetType().ToString();
var srpType = GraphicsSettings.defaultRenderPipeline.GetType().ToString();
if ( srpType.Contains( "HDRenderPipelineAsset" ) )
return PipelineType.HDRP;
else if ( srpType.Contains( "UniversalRenderPipelineAsset" ) || srpType.Contains( "LightweightRenderPipelineAsset" ) )
Expand Down
4 changes: 0 additions & 4 deletions AGXUnity/Utils/TerrainUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ public static agx.AffineMatrix4x4 CalculateNativeOffset( Transform transform, Te

public static int TerrainDataResolution( TerrainData terrainData )
{
#if UNITY_2019_3_OR_NEWER
return terrainData.heightmapResolution;
#else
return terrainData.heightmapWidth;
#endif
}

/// <summary>
Expand Down
12 changes: 8 additions & 4 deletions AGXUnityUpdateHandler/Editor/AGXUnityImportCleanup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ private static void UpdateCleanup()
ImportAssetOptions.ImportRecursive |
ImportAssetOptions.DontDownloadFromCacheServer |
ImportAssetOptions.ForceSynchronousImport );
#if UNITY_2020_1_OR_NEWER
EditorApplication.update += OnEditorUpdate;
DefineSymbols.Add( OnImportDefineSymbol );
#endif
}

private static void OnImportCompleted( string package )
Expand Down Expand Up @@ -93,10 +91,8 @@ private static string AGXUnityDirectory

private static void ShowNotification( string message, double fadeoutWait )
{
#if UNITY_2019_1_OR_NEWER
foreach ( SceneView sceneView in SceneView.sceneViews )
sceneView.ShowNotification( new GUIContent( message ), fadeoutWait );
#endif
}

private static void OnEditorUpdate()
Expand Down Expand Up @@ -151,13 +147,21 @@ private static void Set( List<string> symbols )
symbols.Count == 1 ?
symbols[ 0 ] :
string.Join( ";", symbols );
#if UNITY_2021_2_OR_NEWER
PlayerSettings.SetScriptingDefineSymbols( UnityEditor.Build.NamedBuildTarget.Standalone, symbolsToSet );
#else
PlayerSettings.SetScriptingDefineSymbolsForGroup( BuildTargetGroup.Standalone, symbolsToSet );
#endif
AssetDatabase.SaveAssets();
}

private static List<string> Get()
{
#if UNITY_2021_2_OR_NEWER
return PlayerSettings.GetScriptingDefineSymbols( UnityEditor.Build.NamedBuildTarget.Standalone ).Split( ';' ).ToList();
#else
return PlayerSettings.GetScriptingDefineSymbolsForGroup( BuildTargetGroup.Standalone ).Split( ';' ).ToList();
#endif
}
}
}
Expand Down
40 changes: 16 additions & 24 deletions Editor/AGXUnityEditor/AutoUpdateSceneHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static void HandleUpdates( Scene scene )
/// <returns>true if changes were made, otherwise false</returns>
public static bool VerifyPrefabInstance( GameObject instance )
{
#if UNITY_2018_3_OR_NEWER
var isDisconnected =
#if UNITY_2022_1_OR_NEWER
false;
Expand All @@ -44,32 +43,15 @@ public static bool VerifyPrefabInstance( GameObject instance )
var objectToCheck = isDisconnected ?
instance :
(GameObject)PrefabUtility.GetCorrespondingObjectFromSource( instance );
#else
var isDisconnected = PrefabUtility.GetPrefabType( instance ) != PrefabType.PrefabInstance;

// We're modifying the instance and replacing the prefab in this "old" prefab
// work flow.
var objectToCheck = instance;
#endif

if ( !HandleSegmentSpawner( objectToCheck ) )
return false;

try {
if ( !isDisconnected ) {
#if UNITY_2018_3_OR_NEWER
PrefabUtility.SaveAsPrefabAssetAndConnect( objectToCheck,
AssetDatabase.GetAssetPath( objectToCheck ),
InteractionMode.UserAction );
#else
PrefabUtility.ReplacePrefab( objectToCheck,
#if UNITY_2018_1_OR_NEWER
(GameObject)PrefabUtility.GetCorrespondingObjectFromSource( instance ),
#else
PrefabUtility.GetPrefabParent( instance ),
#endif
ReplacePrefabOptions.ConnectToPrefab );
#endif
}
}
catch ( System.ArgumentException ) {
Expand All @@ -87,7 +69,11 @@ public static bool VerifyPrefabInstance( GameObject instance )
/// </summary>
private static void VerifyOnSelectionTarget( Scene scene )
{
#if UNITY_6000_0_OR_NEWER
var shapes = Object.FindObjectsByType<AGXUnity.Collide.Shape>(FindObjectsSortMode.None);
#else
var shapes = Object.FindObjectsOfType<AGXUnity.Collide.Shape>();
#endif
foreach ( var shape in shapes ) {
OnSelectionProxy selectionProxy = shape.GetComponent<OnSelectionProxy>();
if ( selectionProxy != null && selectionProxy.Target == null )
Expand All @@ -105,7 +91,11 @@ private static void VerifyOnSelectionTarget( Scene scene )

private static void VerifySimulationInstance( Scene scene )
{
var simulation = Object.FindObjectOfType<Simulation>();
#if UNITY_6000_0_OR_NEWER
var simulation = Object.FindAnyObjectByType<Simulation>( FindObjectsInactive.Include );
#else
var simulation = Object.FindObjectOfType<Simulation>(true);
#endif
if ( simulation == null )
return;

Expand All @@ -123,22 +113,24 @@ private static void VerifyCableWireRendering( Scene scene )
var containsChanges = false;
System.Action<GameObject> checkGameObject = go =>
{
#if UNITY_2018_3_OR_NEWER
var root = PrefabUtility.GetOutermostPrefabInstanceRoot( go );
#else
var root = PrefabUtility.FindPrefabRoot( go );
#endif
if ( root != null )
containsChanges = VerifyPrefabInstance( root ) || containsChanges;
else
containsChanges = HandleSegmentSpawner( go ) || containsChanges;
};

#if UNITY_6000_0_OR_NEWER
var cables = Object.FindObjectsByType<Cable>(FindObjectsSortMode.None);
var wires = Object.FindObjectsByType<Wire>(FindObjectsSortMode.None);
#else
var cables = Object.FindObjectsOfType<Cable>();
var wires = Object.FindObjectsOfType<Wire>();
#endif

foreach ( var cable in cables )
checkGameObject( cable.gameObject );

var wires = Object.FindObjectsOfType<Wire>();
foreach ( var wire in wires )
checkGameObject( wire.gameObject );

Expand Down
Loading

0 comments on commit 7e0df63

Please sign in to comment.