Skip to content

Commit

Permalink
feat: new attribute AutoTypeMenuAttribute, caching, and example
Browse files Browse the repository at this point in the history
  • Loading branch information
DhafinFawwaz committed Dec 17, 2024
1 parent f938fc5 commit 04048a4
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 58 deletions.
79 changes: 79 additions & 0 deletions Assets/Example/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,85 @@ public Banana ()
}
}

[Serializable]
public class Snack : Food
{
public Snack ()
{
name = "Snack";
kcal = 100f;
}
}
[Serializable]
public class Candy : Snack
{
public Candy ()
{
name = "Candy";
kcal = 100f;
}
}

[Serializable, AutoTypeMenu]
public class Chocolate : Snack
{
public Chocolate ()
{
name = "Chocolate";
kcal = 100f;
}
}

[Serializable]
public class ChocolateBar : Chocolate
{
public ChocolateBar ()
{
name = "ChocolateBar";
kcal = 100f;
}
}

[Serializable]
public class ChocolateIceCream : Chocolate
{
public ChocolateIceCream ()
{
name = "ChocolateIceCream";
kcal = 100f;
}
}

[Serializable]
public class ChocolateBean : Chocolate
{
public ChocolateBean ()
{
name = "ChocolateBean";
kcal = 100f;
}
}

[Serializable]
public class ChocolateBeanAlmond : ChocolateBean
{
public ChocolateBeanAlmond ()
{
name = "ChocolateBeanAlmond";
kcal = 100f;
}
}

[Serializable]
public class ChocolateBeanHazelnut : ChocolateBean
{
public ChocolateBeanHazelnut ()
{
name = "ChocolateBeanHazelnut";
kcal = 100f;
}
}

public class Example : MonoBehaviour
{

Expand Down
84 changes: 33 additions & 51 deletions Assets/Example/Example.unity
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ OcclusionCullingSettings:
--- !u!104 &2
RenderSettings:
m_ObjectHideFlags: 0
serializedVersion: 10
serializedVersion: 9
m_Fog: 0
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
m_FogMode: 3
Expand All @@ -38,12 +38,13 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 12
m_GIWorkflowMode: 1
m_GISettings:
serializedVersion: 2
m_BounceScale: 1
Expand All @@ -66,6 +67,9 @@ LightmapSettings:
m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1
m_TextureCompression: 1
m_FinalGather: 0
m_FinalGatherFiltering: 1
m_FinalGatherRayCount: 256
m_ReflectionCompression: 2
m_MixedBakeMode: 2
m_BakeBackend: 1
Expand Down Expand Up @@ -100,7 +104,7 @@ NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 3
serializedVersion: 2
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
Expand All @@ -113,7 +117,7 @@ NavMeshSettings:
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
buildHeightMesh: 0
accuratePlacement: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
Expand Down Expand Up @@ -159,17 +163,9 @@ Camera:
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
m_Iso: 200
m_ShutterSpeed: 0.005
m_Aperture: 16
m_FocusDistance: 10
m_FocalLength: 50
m_BladeCount: 5
m_Curvature: {x: 2, y: 11}
m_BarrelClipping: 0.25
m_Anamorphism: 0
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
Expand Down Expand Up @@ -203,13 +199,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 126803971}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &586792171
GameObject:
Expand Down Expand Up @@ -251,18 +247,34 @@ MonoBehaviour:
foodTwo:
rid: 9020349853700980767
foodThree:
rid: 9020349853700980768
rid: 5331542178295447552
foodsOne:
- rid: 9020349853700980769
- rid: 9020349853700980770
- rid: 9020349853700980771
foodsTwo:
- rid: 9020349853700980772
- rid: 9020349853700980773
- rid: 5331542178476589060
- rid: 9020349853700980774
- rid: 5331542178476589059
references:
version: 2
RefIds:
- rid: 5331542178295447552
type: {class: Grape, ns: , asm: Assembly-CSharp}
data:
name: Grape
kcal: 100
- rid: 5331542178476589059
type: {class: ChocolateBean, ns: , asm: Assembly-CSharp}
data:
name: ChocolateBar
kcal: 100
- rid: 5331542178476589060
type: {class: Grape, ns: , asm: Assembly-CSharp}
data:
name: Peach
kcal: 100
- rid: 9020349853700980763
type: {class: Apple, ns: , asm: Assembly-CSharp}
data:
Expand All @@ -288,11 +300,6 @@ MonoBehaviour:
data:
name: Peach
kcal: 100
- rid: 9020349853700980768
type: {class: Grape, ns: , asm: Assembly-CSharp}
data:
name: Grape
kcal: 100
- rid: 9020349853700980769
type: {class: Apple, ns: , asm: Assembly-CSharp}
data:
Expand All @@ -313,11 +320,6 @@ MonoBehaviour:
data:
name: Apple
kcal: 100
- rid: 9020349853700980773
type: {class: Peach, ns: , asm: Assembly-CSharp}
data:
name: Peach
kcal: 100
- rid: 9020349853700980774
type: {class: Grape, ns: , asm: Assembly-CSharp}
data:
Expand All @@ -330,13 +332,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 586792171}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &994260143
GameObject:
Expand Down Expand Up @@ -367,33 +369,20 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: dff76005e1dfac84287448b12c4b160e, type: 3}
m_Name:
m_EditorClassIdentifier:
contravarianceActions:
- rid: 3354424774140624943
covarianceActions:
- rid: 3354424774140624944
references:
version: 2
RefIds:
- rid: 3354424774140624943
type: {class: DerivedAction1, ns: , asm: Assembly-CSharp}
data:
- rid: 3354424774140624944
type: {class: NetworkActorAction1, ns: , asm: Assembly-CSharp}
data:
--- !u!4 &994260145
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 994260143}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1127138992
GameObject:
Expand All @@ -420,8 +409,9 @@ Light:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1127138992}
m_Enabled: 1
serializedVersion: 11
serializedVersion: 10
m_Type: 1
m_Shape: 0
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
m_Intensity: 1
m_Range: 10
Expand Down Expand Up @@ -480,19 +470,11 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1127138992}
serializedVersion: 2
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
m_LocalPosition: {x: 0, y: 3, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1660057539 &9223372036854775807
SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 126803974}
- {fileID: 1127138994}
- {fileID: 586792173}
- {fileID: 994260145}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AdvancedTypePopup : AdvancedDropdown {

const int kMaxNamespaceNestCount = 16;

public static void AddTo (AdvancedDropdownItem root,IEnumerable<Type> types) {
public static void AddTo (AdvancedDropdownItem root,IEnumerable<Type> types, Type baseType) {
int itemCount = 0;

// Add null item.
Expand All @@ -39,10 +39,18 @@ public static void AddTo (AdvancedDropdownItem root,IEnumerable<Type> types) {
bool isSingleNamespace = true;
string[] namespaces = new string[kMaxNamespaceNestCount];
foreach (Type type in typeArray) {
string[] splittedTypePath = TypeMenuUtility.GetSplittedTypePath(type);
string[] splittedTypePath = TypeMenuUtility.GetSplittedTypePath(type, baseType);

// If any type has AutoTypeMenuAttribute, it is not single namespace.
if(Attribute.GetCustomAttribute(type, typeof(AutoTypeMenuAttribute)) as AutoTypeMenuAttribute != null) {
isSingleNamespace = false;
break;
}

if (splittedTypePath.Length <= 1) {
continue;
}

// If they explicitly want sub category, let them do.
if (TypeMenuUtility.GetAttribute(type) != null) {
isSingleNamespace = false;
Expand All @@ -66,7 +74,7 @@ public static void AddTo (AdvancedDropdownItem root,IEnumerable<Type> types) {

// Add type items.
foreach (Type type in typeArray) {
string[] splittedTypePath = TypeMenuUtility.GetSplittedTypePath(type);
string[] splittedTypePath = TypeMenuUtility.GetSplittedTypePath(type, baseType);
if (splittedTypePath.Length == 0) {
continue;
}
Expand Down Expand Up @@ -110,12 +118,14 @@ static AdvancedDropdownItem GetItem (AdvancedDropdownItem parent,string name) {
static readonly float k_HeaderHeight = EditorGUIUtility.singleLineHeight * 2f;

Type[] m_Types;
Type m_BaseType;

public event Action<AdvancedTypePopupItem> OnItemSelected;

public AdvancedTypePopup (IEnumerable<Type> types,int maxLineCount,AdvancedDropdownState state) : base(state) {
public AdvancedTypePopup (IEnumerable<Type> types, Type baseType,int maxLineCount,AdvancedDropdownState state) : base(state) {
SetTypes(types);
minimumSize = new Vector2(minimumSize.x,EditorGUIUtility.singleLineHeight * maxLineCount + k_HeaderHeight);
m_BaseType = baseType;
}

public void SetTypes (IEnumerable<Type> types) {
Expand All @@ -124,7 +134,7 @@ public void SetTypes (IEnumerable<Type> types) {

protected override AdvancedDropdownItem BuildRoot () {
var root = new AdvancedDropdownItem("Select Type");
AddTo(root,m_Types);
AddTo(root,m_Types,m_BaseType);
return root;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ TypePopupCache GetTypePopup (SerializedProperty property) {
Type baseType = ManagedReferenceUtility.GetType(managedReferenceFieldTypename);
var popup = new AdvancedTypePopup(
TypeSearch.GetTypes(baseType),
baseType,
k_MaxTypePopupLineCount,
state
);
Expand Down
Loading

0 comments on commit 04048a4

Please sign in to comment.