Skip to content

Commit

Permalink
feat: Added some API to get LDtkFields point transform components
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammin committed Jul 29, 2024
1 parent 60ed745 commit aaf9971
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Assets/LDtkUnity/Runtime/Fields/LDtkFieldElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,30 @@ public FieldsResult<TEnum> GetEnumValue<TEnum>() where TEnum : struct
LDtkDebug.LogError($"C# enum \"{type.Name}\" does not define enum value \"{_string}\". Possible values are \"{joined}\"");
return result;
}

public FieldsResult<Transform> GetPointValueTransform()
{
FieldsResult<Vector2> result = GetData(_vector2, LDtkFieldType.Point);
if (!result.Success)
{
return FieldsResult<Transform>.Null();
}
if (_obj is Transform transform)
{
return new FieldsResult<Transform>()
{
Success = true,
Value = transform
};
}
//it's okay to return null
return new FieldsResult<Transform>()
{
Success = true,
Value = null
};
}

public FieldsResult<Vector2> GetPointValue()
{
FieldsResult<Vector2> result = GetData(_vector2, LDtkFieldType.Point);
Expand Down
49 changes: 49 additions & 0 deletions Assets/LDtkUnity/Runtime/Fields/LDtkFieldsPublicAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,16 @@ public LDtkDefinitionObjectField GetDefinition(string identifier)
/// The field's value. If the field doesn't exist, then returns a default value type.
/// </returns>
public Vector2 GetPoint(string identifier) => GetFieldSingle(identifier, LDtkFieldType.Point, element => element.GetPointValue());
/// <summary>
/// Gets a point field's value as transform.
/// </summary>
/// <param name="identifier">
/// The field instance's identifier. Case sensitive.
/// </param>
/// <returns>
/// The field's value. If the field doesn't exist, then returns a default value type.
/// </returns>
public Transform GetPointTransform(string identifier) => GetFieldSingle(identifier, LDtkFieldType.Point, element => element.GetPointValueTransform());

/// <summary>
/// Gets a point field's value.
Expand All @@ -476,6 +486,20 @@ public LDtkDefinitionObjectField GetDefinition(string identifier)
/// </returns>
public bool TryGetPoint(string identifier, out Vector2 value) => TryGetFieldSingle(identifier, LDtkFieldType.Point, element => element.GetPointValue(), out value);

/// <summary>
/// Gets a point field's value as transform.
/// </summary>
/// <param name="identifier">
/// The field instance's identifier. Case sensitive.
/// </param>
/// <param name="value">
/// The field's value.
/// </param>
/// <returns>
/// If the field exists.
/// </returns>
public bool TryGetPointTransform(string identifier, out Transform value) => TryGetFieldSingle(identifier, LDtkFieldType.Point, element => element.GetPointValueTransform(), out value);

/// <summary>
/// Gets a point field's values.
/// </summary>
Expand All @@ -487,6 +511,31 @@ public LDtkDefinitionObjectField GetDefinition(string identifier)
/// </returns>
public Vector2[] GetPointArray(string identifier) => GetFieldArray(identifier, LDtkFieldType.Point, element => element.GetPointValue());

/// <summary>
/// Gets a point field's values as transforms.
/// </summary>
/// <param name="identifier">
/// The field instance's identifier. Case sensitive.
/// </param>
/// <returns>
/// The field's value. If the field doesn't exist, then returns a default value type.
/// </returns>
public Transform[] GetPointTransformArray(string identifier) => GetFieldArray(identifier, LDtkFieldType.Point, element => element.GetPointValueTransform());

/// <summary>
/// Gets a point field's values as transforms.
/// </summary>
/// <param name="identifier">
/// The field instance's identifier. Case sensitive.
/// </param>
/// <param name="values">
/// The field's values.
/// </param>
/// <returns>
/// If the field exists.
/// </returns>
public bool TryGetPointTransformArray(string identifier, out Transform[] values) => TryGetFieldArray(identifier, LDtkFieldType.Point, element => element.GetPointValueTransform(), out values);

/// <summary>
/// Gets a point field's values.
/// </summary>
Expand Down

0 comments on commit aaf9971

Please sign in to comment.