Skip to content

Commit 533b4e3

Browse files
authored
Merge pull request #75 from mackysoft/feature/support-contravariance
Support contravariance/covariance
2 parents 56d0834 + a3597f2 commit 533b4e3

File tree

7 files changed

+333
-29
lines changed

7 files changed

+333
-29
lines changed

Assets/Example/Example.unity

+82-14
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ OcclusionCullingSettings:
1313
--- !u!104 &2
1414
RenderSettings:
1515
m_ObjectHideFlags: 0
16-
serializedVersion: 9
16+
serializedVersion: 10
1717
m_Fog: 0
1818
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
1919
m_FogMode: 3
@@ -38,13 +38,12 @@ RenderSettings:
3838
m_ReflectionIntensity: 1
3939
m_CustomReflection: {fileID: 0}
4040
m_Sun: {fileID: 0}
41-
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
41+
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
4242
m_UseRadianceAmbientProbe: 0
4343
--- !u!157 &3
4444
LightmapSettings:
4545
m_ObjectHideFlags: 0
4646
serializedVersion: 12
47-
m_GIWorkflowMode: 1
4847
m_GISettings:
4948
serializedVersion: 2
5049
m_BounceScale: 1
@@ -67,9 +66,6 @@ LightmapSettings:
6766
m_LightmapParameters: {fileID: 0}
6867
m_LightmapsBakeMode: 1
6968
m_TextureCompression: 1
70-
m_FinalGather: 0
71-
m_FinalGatherFiltering: 1
72-
m_FinalGatherRayCount: 256
7369
m_ReflectionCompression: 2
7470
m_MixedBakeMode: 2
7571
m_BakeBackend: 1
@@ -104,7 +100,7 @@ NavMeshSettings:
104100
serializedVersion: 2
105101
m_ObjectHideFlags: 0
106102
m_BuildSettings:
107-
serializedVersion: 2
103+
serializedVersion: 3
108104
agentTypeID: 0
109105
agentRadius: 0.5
110106
agentHeight: 2
@@ -117,7 +113,7 @@ NavMeshSettings:
117113
cellSize: 0.16666667
118114
manualTileSize: 0
119115
tileSize: 256
120-
accuratePlacement: 0
116+
buildHeightMesh: 0
121117
maxJobWorkers: 0
122118
preserveTilesOutsideBounds: 0
123119
debug:
@@ -163,9 +159,17 @@ Camera:
163159
m_projectionMatrixMode: 1
164160
m_GateFitMode: 2
165161
m_FOVAxisMode: 0
162+
m_Iso: 200
163+
m_ShutterSpeed: 0.005
164+
m_Aperture: 16
165+
m_FocusDistance: 10
166+
m_FocalLength: 50
167+
m_BladeCount: 5
168+
m_Curvature: {x: 2, y: 11}
169+
m_BarrelClipping: 0.25
170+
m_Anamorphism: 0
166171
m_SensorSize: {x: 36, y: 24}
167172
m_LensShift: {x: 0, y: 0}
168-
m_FocalLength: 50
169173
m_NormalizedViewPortRect:
170174
serializedVersion: 2
171175
x: 0
@@ -199,13 +203,13 @@ Transform:
199203
m_PrefabInstance: {fileID: 0}
200204
m_PrefabAsset: {fileID: 0}
201205
m_GameObject: {fileID: 126803971}
206+
serializedVersion: 2
202207
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
203208
m_LocalPosition: {x: 0, y: 1, z: -10}
204209
m_LocalScale: {x: 1, y: 1, z: 1}
205210
m_ConstrainProportionsScale: 0
206211
m_Children: []
207212
m_Father: {fileID: 0}
208-
m_RootOrder: 0
209213
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
210214
--- !u!1 &586792171
211215
GameObject:
@@ -326,13 +330,70 @@ Transform:
326330
m_PrefabInstance: {fileID: 0}
327331
m_PrefabAsset: {fileID: 0}
328332
m_GameObject: {fileID: 586792171}
333+
serializedVersion: 2
334+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
335+
m_LocalPosition: {x: 0, y: 0, z: 0}
336+
m_LocalScale: {x: 1, y: 1, z: 1}
337+
m_ConstrainProportionsScale: 0
338+
m_Children: []
339+
m_Father: {fileID: 0}
340+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
341+
--- !u!1 &994260143
342+
GameObject:
343+
m_ObjectHideFlags: 0
344+
m_CorrespondingSourceObject: {fileID: 0}
345+
m_PrefabInstance: {fileID: 0}
346+
m_PrefabAsset: {fileID: 0}
347+
serializedVersion: 6
348+
m_Component:
349+
- component: {fileID: 994260145}
350+
- component: {fileID: 994260144}
351+
m_Layer: 0
352+
m_Name: Example_Generics
353+
m_TagString: Untagged
354+
m_Icon: {fileID: 0}
355+
m_NavMeshLayer: 0
356+
m_StaticEditorFlags: 0
357+
m_IsActive: 1
358+
--- !u!114 &994260144
359+
MonoBehaviour:
360+
m_ObjectHideFlags: 0
361+
m_CorrespondingSourceObject: {fileID: 0}
362+
m_PrefabInstance: {fileID: 0}
363+
m_PrefabAsset: {fileID: 0}
364+
m_GameObject: {fileID: 994260143}
365+
m_Enabled: 1
366+
m_EditorHideFlags: 0
367+
m_Script: {fileID: 11500000, guid: dff76005e1dfac84287448b12c4b160e, type: 3}
368+
m_Name:
369+
m_EditorClassIdentifier:
370+
contravarianceActions:
371+
- rid: 3354424774140624943
372+
covarianceActions:
373+
- rid: 3354424774140624944
374+
references:
375+
version: 2
376+
RefIds:
377+
- rid: 3354424774140624943
378+
type: {class: DerivedAction1, ns: , asm: Assembly-CSharp}
379+
data:
380+
- rid: 3354424774140624944
381+
type: {class: NetworkActorAction1, ns: , asm: Assembly-CSharp}
382+
data:
383+
--- !u!4 &994260145
384+
Transform:
385+
m_ObjectHideFlags: 0
386+
m_CorrespondingSourceObject: {fileID: 0}
387+
m_PrefabInstance: {fileID: 0}
388+
m_PrefabAsset: {fileID: 0}
389+
m_GameObject: {fileID: 994260143}
390+
serializedVersion: 2
329391
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
330392
m_LocalPosition: {x: 0, y: 0, z: 0}
331393
m_LocalScale: {x: 1, y: 1, z: 1}
332394
m_ConstrainProportionsScale: 0
333395
m_Children: []
334396
m_Father: {fileID: 0}
335-
m_RootOrder: 2
336397
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
337398
--- !u!1 &1127138992
338399
GameObject:
@@ -359,9 +420,8 @@ Light:
359420
m_PrefabAsset: {fileID: 0}
360421
m_GameObject: {fileID: 1127138992}
361422
m_Enabled: 1
362-
serializedVersion: 10
423+
serializedVersion: 11
363424
m_Type: 1
364-
m_Shape: 0
365425
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
366426
m_Intensity: 1
367427
m_Range: 10
@@ -420,11 +480,19 @@ Transform:
420480
m_PrefabInstance: {fileID: 0}
421481
m_PrefabAsset: {fileID: 0}
422482
m_GameObject: {fileID: 1127138992}
483+
serializedVersion: 2
423484
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
424485
m_LocalPosition: {x: 0, y: 3, z: 0}
425486
m_LocalScale: {x: 1, y: 1, z: 1}
426487
m_ConstrainProportionsScale: 0
427488
m_Children: []
428489
m_Father: {fileID: 0}
429-
m_RootOrder: 1
430490
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
491+
--- !u!1660057539 &9223372036854775807
492+
SceneRoots:
493+
m_ObjectHideFlags: 0
494+
m_Roots:
495+
- {fileID: 126803974}
496+
- {fileID: 1127138994}
497+
- {fileID: 586792173}
498+
- {fileID: 994260145}

Assets/Example/Example_Generics.cs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public interface IActor { }
6+
public interface IStandardActor : IActor { }
7+
public interface INetworkActor : IActor { }
8+
9+
public interface IContravarianceAction<in T> where T : IActor {
10+
void DoAction (T actor);
11+
}
12+
13+
public interface ICovarianceAction<out T> where T : IActor
14+
{
15+
T Actor { get; }
16+
}
17+
18+
public interface IActorAction : IContravarianceAction<IActor>, ICovarianceAction<IActor> { }
19+
public interface IStandardActorAction : IContravarianceAction<IStandardActor>, ICovarianceAction<IStandardActor> { }
20+
public interface INetworkActorAction : IContravarianceAction<INetworkActor>, ICovarianceAction<INetworkActor> { }
21+
22+
[Serializable]
23+
public sealed class StandardActorAction : IContravarianceAction<IStandardActor>, ICovarianceAction<IStandardActor>
24+
{
25+
public void DoAction (IStandardActor actor)
26+
{
27+
}
28+
public IStandardActor Actor => null;
29+
}
30+
31+
[Serializable]
32+
public sealed class ActorAction : IContravarianceAction<IActor>, ICovarianceAction<IActor>
33+
{
34+
public void DoAction (IActor actor)
35+
{
36+
}
37+
public IActor Actor => null;
38+
}
39+
40+
[Serializable]
41+
public abstract class BaseAction<T> : IContravarianceAction<T>, ICovarianceAction<T> where T : IActor
42+
{
43+
public void DoAction (T actor) {
44+
}
45+
public T Actor => default;
46+
}
47+
48+
[Serializable]
49+
public sealed class DerivedAction1 : BaseAction<IActor> { }
50+
51+
[Serializable]
52+
public sealed class DerivedAction2 : BaseAction<INetworkActor> { }
53+
54+
[Serializable]
55+
public sealed class DerivedAction3 : BaseAction<IStandardActor> { }
56+
57+
[Serializable]
58+
public sealed class NetworkActorAction1 : INetworkActorAction
59+
{
60+
public void DoAction (INetworkActor actor)
61+
{
62+
}
63+
public INetworkActor Actor => null;
64+
}
65+
66+
[Serializable]
67+
public sealed class NetworkActorAction2 : IContravarianceAction<INetworkActor>, ICovarianceAction<INetworkActor>
68+
{
69+
public void DoAction (INetworkActor actor)
70+
{
71+
}
72+
public INetworkActor Actor => null;
73+
}
74+
75+
[Serializable]
76+
public sealed class NetworkActorAction3 : IContravarianceAction<IActor>, ICovarianceAction<IActor>
77+
{
78+
public void DoAction (IActor actor)
79+
{
80+
}
81+
public IActor Actor => null;
82+
}
83+
84+
public class Example_Generics : MonoBehaviour
85+
{
86+
87+
[SerializeReference, SubclassSelector]
88+
public List<IContravarianceAction<INetworkActor>> contravarianceActions = new List<IContravarianceAction<INetworkActor>>();
89+
90+
[SerializeReference, SubclassSelector]
91+
public List<ICovarianceAction<INetworkActor>> covarianceActions = new List<ICovarianceAction<INetworkActor>>();
92+
93+
}

Assets/Example/Example_Generics.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/SubclassSelectorDrawer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ TypePopupCache GetTypePopup (SerializedProperty property) {
149149

150150
Type baseType = ManagedReferenceUtility.GetType(managedReferenceFieldTypename);
151151
var popup = new AdvancedTypePopup(
152-
TypeMenuUtility.GetTypes(baseType),
152+
TypeSearch.GetTypes(baseType),
153153
k_MaxTypePopupLineCount,
154154
state
155155
);

Assets/MackySoft/MackySoft.SerializeReferenceExtensions/Editor/TypeMenuUtility.cs

+2-14
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,11 @@
33
using System.Collections.Generic;
44
using UnityEditor;
55

6-
namespace MackySoft.SerializeReferenceExtensions.Editor {
6+
namespace MackySoft.SerializeReferenceExtensions.Editor
7+
{
78
public static class TypeMenuUtility {
89

910
public const string k_NullDisplayName = "<null>";
10-
static readonly Type k_UnityObjectType = typeof(UnityEngine.Object);
11-
12-
public static IEnumerable<Type> GetTypes (Type baseType)
13-
{
14-
return TypeCache.GetTypesDerivedFrom(baseType).Append(baseType).Where(p =>
15-
(p.IsPublic || p.IsNestedPublic || p.IsNestedPrivate) &&
16-
!p.IsAbstract &&
17-
!p.IsGenericType &&
18-
!k_UnityObjectType.IsAssignableFrom(p) &&
19-
Attribute.IsDefined(p, typeof(SerializableAttribute)) &&
20-
!Attribute.IsDefined(p, typeof(HideInTypeMenuAttribute))
21-
);
22-
}
2311

2412
public static AddTypeMenuAttribute GetAttribute (Type type) {
2513
return Attribute.GetCustomAttribute(type,typeof(AddTypeMenuAttribute)) as AddTypeMenuAttribute;

0 commit comments

Comments
 (0)