Repository navigation
Expand file tree
/
Copy pathMaterialPropertyAttributeValidator.cs
More file actions
81 lines (69 loc) · 3.36 KB
/
Copy pathMaterialPropertyAttributeValidator.cs
File metadata and controls
81 lines (69 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.Linq;
using TriInspector.Drawers;
namespace TriInspector.Validators
{
[RegisterTriAttributeValidator(ApplyOnArrayElement = true)]
public class MaterialPropertyAttributeValidator : TriAttributeValidator<MaterialPropertyAttribute>
{
private MaterialPropertyHelper.ResolvedParams _resolvedParams;
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
{
_resolvedParams = MaterialPropertyHelper.Initialize(propertyDefinition, Attribute);
return _resolvedParams.ErrorResult;
}
public override TriValidationResult Validate(TriProperty property)
{
var material = _resolvedParams.MaterialResolver.GetValue(property);
if (material == null)
{
return TriValidationResult.Error("Animator reference is null.");
}
if (material.shader == null)
{
return TriValidationResult.Error("Material has no shader assigned.");
}
var (allParameters, allFilteredParameters) = MaterialPropertyHelper.GetProperties(material, Attribute.PropertyType);
if (property.ValueType == typeof(string))
{
var currentValue = (string)property.Value;
if (string.IsNullOrEmpty(currentValue))
{
return TriValidationResult.Valid;
}
if (!allParameters.Names.Contains(currentValue))
{
string label = MaterialPropertyHelper.GetInvalidPropertyLabel(material, currentValue);
return TriValidationResult.Error($"Parameter {label} not found in animator.")
.WithFix(() => property.SetValue(null), "Clear value");
}
if (!allFilteredParameters.Names.Contains(currentValue))
{
string label = MaterialPropertyHelper.GetInvalidPropertyLabel(material, currentValue);
return TriValidationResult.Error($"Parameter {label} is not of the expected type ({Attribute.PropertyType}).")
.WithFix(() => property.SetValue(null), "Clear value");
}
}
if (property.ValueType == typeof(int))
{
var currentValue = (int)property.Value;
if (currentValue == 0)
{
return TriValidationResult.Valid;
}
if (!allParameters.Hashes.Contains(currentValue))
{
string label = MaterialPropertyHelper.GetInvalidPropertyLabel(material, currentValue);
return TriValidationResult.Error($"Parameter {label} not found in animator.")
.WithFix(() => property.SetValue(0), "Clear value");
}
if (!allFilteredParameters.Hashes.Contains(currentValue))
{
string label = MaterialPropertyHelper.GetInvalidPropertyLabel(material, currentValue);
return TriValidationResult.Error($"Parameter {label} is not of the expected type ({Attribute.PropertyType}).")
.WithFix(() => property.SetValue(0), "Clear value");
}
}
return TriValidationResult.Valid;
}
}
}