public HighlightMatchesPopup(RawComponent component, PotentialType potentialType) { this.component = component; this.potentialType = potentialType; this.matchingFields = new bool[component.fields.Count]; for (int i = 0; i < component.fields.Count; i++) { for (int j = 0; j < potentialType.fields.Length; j++) { if (component.fields[i] == potentialType.fields[j]) { this.matchingFields[i] = true; break; } } } for (int i = 0; i < potentialType.fields.Length; i++) { int j = 0; for (; j < component.fields.Count; j++) { if (component.fields[j] == potentialType.fields[i]) { break; } } if (j >= component.fields.Count) { this.extraFields.Add(potentialType.fields[i]); } } float maxWidth = 0F; for (int i = 0; i < component.fields.Count; i++) { Utility.content.text = component.fields[i]; float width = GUI.skin.label.CalcSize(Utility.content).x; if (maxWidth < width) { maxWidth = width; } } for (int i = 0; i < this.extraFields.Count; i++) { Utility.content.text = this.extraFields[i]; float width = GUI.skin.label.CalcSize(Utility.content).x; if (maxWidth < width) { maxWidth = width; } } float height = 0F; if (component.fields.Count > 0) { height = (Constants.SingleLineHeight + HighlightMatchesPopup.Spacing) * (component.fields.Count + 1); } if (this.extraFields.Count > 0) { if (this.component.fields.Count > 0) { height += HighlightMatchesPopup.FieldsExtraSpacing; } height += (Constants.SingleLineHeight + HighlightMatchesPopup.Spacing) * (this.extraFields.Count + 1); } this.size = new Vector2(maxWidth + HighlightMatchesPopup.Spacing + HighlightMatchesPopup.Spacing, height - HighlightMatchesPopup.Spacing); }
public void Diagnose(GameObject gameObject) { this.rawComponents.Clear(); PrefabType prefabType = PrefabUtility.GetPrefabType(gameObject); if (prefabType == PrefabType.Prefab) { this.target = gameObject; } else if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance) { this.target = PrefabUtility.GetPrefabParent(gameObject) as GameObject; } else { this.target = null; } // Look into a prefab. if (this.target != null) { Component[] components = this.target.GetComponents <Component>(); string prefabPath = AssetDatabase.GetAssetPath(this.target); string[] lines = File.ReadAllLines(prefabPath); List <string> matchFields = new List <string>(); this.ExtractComponentsIDs(lines, this.target, prefabPath); for (int k = 0; k < this.componentIDs.Count; k++) { RawComponent rawComponent = new RawComponent(this.componentIDs[k]); this.rawComponents.Add(rawComponent); for (int i = 0; i < lines.Length; i++) { if (lines[i].StartsWith("---") == true && lines[i].EndsWith(this.componentIDs[k]) == true) { ++i; if (components != null && components[k] != null) { rawComponent.asset = components[k]; rawComponent.name = rawComponent.asset.GetType().Name; break; } else { rawComponent.name = lines[i].Remove(lines[i].Length - 1); } if (lines[i] == "MonoBehaviour:") { bool inFields = false; ++i; for (; i < lines.Length; i++) { if (lines[i].StartsWith(" m_Script") == true) { rawComponent.line = lines[i]; } else if (inFields == true && lines[i].StartsWith("---") == true) { break; } else if (lines[i].StartsWith(" m_EditorClassIdentifier") == true) { inFields = true; } else if (inFields == true) { if (lines[i][3] != ' ' && lines[i][3] != '-') { rawComponent.fields.Add(lines[i].Substring(2, lines[i].IndexOf(':') - 2)); } } } } break; } } if (rawComponent.fields.Count == 0) { continue; } foreach (Type type in Utility.EachAllSubClassesOf(typeof(MonoBehaviour))) { if (type.IsAbstract == true || type.IsGenericType == true) { continue; } matchFields.Clear(); for (int l = 0; l < rawComponent.fields.Count; l++) { if (type.GetField(rawComponent.fields[l], BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null) { matchFields.Add(rawComponent.fields[l]); } } if (matchFields.Count > 0) { FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); int l = 0; int matchingFieldsCount = matchFields.Count; matchFields.Clear(); for (int i = 0; i < fields.Length; i++) { if ((fields[i].IsPublic == false && fields[i].IsDefined(typeof(SerializeField), true) == false) || fields[i].IsDefined(typeof(NonSerializedAttribute), true) == true || NGTools.Utility.CanExposeTypeInInspector(fields[i].FieldType) == false) { continue; } matchFields.Add(fields[i].Name); } for (; l < rawComponent.potentialTypes.Count; l++) { if (rawComponent.potentialTypes[l].matchingFields < matchingFieldsCount || (rawComponent.potentialTypes[l].matchingFields == matchingFieldsCount && (rawComponent.potentialTypes[l].fields.Length == rawComponent.potentialTypes[l].matchingFields ? rawComponent.potentialTypes[l].matchingFields - rawComponent.fields.Count : rawComponent.potentialTypes[l].fields.Length - rawComponent.potentialTypes[l].matchingFields) > (matchFields.Count == matchingFieldsCount ? matchingFieldsCount - rawComponent.fields.Count : matchFields.Count - matchingFieldsCount))) { rawComponent.potentialTypes.Insert(l, new PotentialType(type, matchingFieldsCount, matchFields.ToArray())); break; } } if (l >= rawComponent.potentialTypes.Count) { rawComponent.potentialTypes.Add(new PotentialType(type, matchingFieldsCount, matchFields.ToArray())); } } } } } // Look into a scene. else { // TODO Implement looking into scene. } if (this.PostDiagnostic != null) { this.PostDiagnostic(); } }