Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nested class types #265

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,15 @@ private static void ChangeLayersRecursively(this Transform trans, int layer)
public static void BroadcastProperty(this GameObject go, CustomProperty property, Dictionary<int, GameObject> objectsById)
{
object objValue;

if (property.m_Type == "class")
{
var classProperty = (ClassCustomProperty)property;
foreach (var entry in classProperty.m_CustomProperties)
{
BroadcastProperty(go, entry.Value, objectsById);
}
return;
}
if (property.m_Type == "bool")
{
objValue = property.GetValueAsBool();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,28 @@ public static CustomProperty LoadCustomProperty(XElement xProperty)

property.m_Name = xProperty.GetAttributeAs("name", "");
property.m_Type = xProperty.GetAttributeAs("type", "string");


if (property.m_Type == "class")
{
var classProperty = new ClassCustomProperty
{
m_Name = property.m_Name,
m_Type = property.m_Type
};
var elements = xProperty.Descendants();
if (elements != null)
{
foreach (var element in elements)
{
var childProperty = LoadCustomProperty(element);
if (!childProperty.IsEmpty)
{
classProperty.m_CustomProperties.Add(childProperty.m_Name, childProperty);
}
}
}
return classProperty;
}
// In some cases, value may be in the default attribute
property.m_Value = xProperty.GetAttributeAs("default", "");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,26 @@ public class CustomProperty
public string m_Type;
public string m_Value;

public bool IsEmpty => string.IsNullOrEmpty(m_Name);
public virtual CustomProperty GetCustomProperty(string key) => this;

public bool IsEmpty
{
get { return string.IsNullOrEmpty(m_Name); }
}
}

[Serializable]
public class ClassCustomProperty : CustomProperty
{
public Dictionary<string, CustomProperty> m_CustomProperties = new Dictionary<string, CustomProperty>();

public override CustomProperty GetCustomProperty(string key)
{
if (m_CustomProperties.TryGetValue(key, out var customProperty))
return customProperty;
return null;
}

}

// Helper extension methods
Expand All @@ -20,12 +39,17 @@ public static bool TryGetProperty(this List<CustomProperty> list, string propert
{
if (list != null)
{
property = list.Find(p => String.Equals(p.m_Name, propertyName, StringComparison.OrdinalIgnoreCase));
return property != null;
property = list.Find(p => String.Equals(p.GetCustomProperty(propertyName)?.m_Name, propertyName, StringComparison.OrdinalIgnoreCase));
if (property != null)
{
property = property.GetCustomProperty(propertyName);
return true;
}
return false;
}

property = null;
return false;
}
}
}
}