Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Issues
- {
- using System.Collections.Generic;
- using UIWidgets;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.UI;
- public class TestSO
- {
- [MenuItem("Window/Crash")]
- public static void Test()
- {
- Debug.Log("x0");
- var x0 = new TestSO();
- Debug.Log("x1");
- var x1 = new TestSO();
- Debug.Log("x2");
- var x2 = new TestSO();
- //Debug.Log("x3");
- //var x3 = new TestSO();
- Debug.Log("finish");
- }
- class PropertyReference
- {
- protected Component Target;
- protected string PropertyPath;
- public PropertyReference(Component target, string propertyPath)
- {
- Target = target;
- PropertyPath = propertyPath;
- }
- }
- List<GameObject> GameObjectsWithReferences = new List<GameObject>();
- readonly Dictionary<Component, List<PropertyReference>> References = new Dictionary<Component, List<PropertyReference>>();
- public TestSO()
- {
- var root_gos = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();
- foreach (var go in root_gos)
- {
- GetGameObjectHierarchy(go.transform);
- }
- FindReferences<InputField, Text>();
- FindReferences<Text, Text>();
- }
- void GetGameObjectHierarchy(Transform current)
- {
- GameObjectsWithReferences.Add(current.gameObject);
- for (int i = 0; i < current.childCount; i++)
- {
- GetGameObjectHierarchy(current.GetChild(i));
- }
- }
- public void FindReferences<TComponent, TCheck>()
- where TComponent : Component
- where TCheck : Component
- {
- var go_components = new List<Component>();
- foreach (var go in GameObjectsWithReferences)
- {
- go.GetComponents(go_components);
- foreach (var component in go_components)
- {
- FindReferencesInComponent(component);
- }
- go_components.Clear();
- }
- }
- /// <summary>
- /// Get all component of the TCheck type and contained in the referenceComponents.
- /// </summary>
- /// <typeparam name="TComponent">Component type.</typeparam>
- /// <typeparam name="TCheck">Checked type.</typeparam>
- /// <param name="components">Components.</param>
- /// <param name="referenceComponents">Reference components.</param>
- /// <param name="matched">Matched components.</param>
- protected void GetMatchedComponents<TComponent, TCheck>(List<Component> components, List<Component> matched)
- where TComponent : Component
- where TCheck : Component
- {
- matched.Clear();
- foreach (var component in components)
- {
- var typed = component as TCheck;
- if (typed != null)
- {
- matched.Add(typed);
- }
- }
- }
- /// <summary>
- /// Find references in the specified component to any reference components and create actions to replace references.
- /// </summary>
- /// <param name="component">Component with references.</param>
- protected void FindReferencesInComponent(Component component)
- {
- if (component == null)
- {
- return;
- }
- using (var serialized = new SerializedObject(component))
- {
- var property = serialized.GetIterator();
- while (property.NextVisible(true))
- {
- if (property.propertyType != SerializedPropertyType.ObjectReference)
- {
- continue;
- }
- var reference = property.objectReferenceValue as Component;
- if (reference == null)
- {
- continue;
- }
- /*
- if (!References.ContainsKey(reference))
- {
- References[reference] = new List<PropertyReference>();
- }
- References[reference].Add(new PropertyReference(component, property.propertyPath));
- */
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement