Advertisement
ilih

Untitled

Jan 21st, 2022
1,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. namespace Issues
  2. {
  3.     using System.Collections.Generic;
  4.     using UIWidgets;
  5.     using UnityEditor;
  6.     using UnityEngine;
  7.     using UnityEngine.UI;
  8.  
  9.     public class TestSO
  10.     {
  11.         [MenuItem("Window/Crash")]
  12.         public static void Test()
  13.         {
  14.             Debug.Log("x0");
  15.             var x0 = new TestSO();
  16.             Debug.Log("x1");
  17.             var x1 = new TestSO();
  18.             Debug.Log("x2");
  19.             var x2 = new TestSO();
  20.             //Debug.Log("x3");
  21.             //var x3 = new TestSO();
  22.             Debug.Log("finish");
  23.         }
  24.  
  25.         class PropertyReference
  26.         {
  27.             protected Component Target;
  28.  
  29.             protected string PropertyPath;
  30.  
  31.             public PropertyReference(Component target, string propertyPath)
  32.             {
  33.                 Target = target;
  34.                 PropertyPath = propertyPath;
  35.             }
  36.         }
  37.  
  38.         List<GameObject> GameObjectsWithReferences = new List<GameObject>();
  39.  
  40.         readonly Dictionary<Component, List<PropertyReference>> References = new Dictionary<Component, List<PropertyReference>>();
  41.  
  42.         public TestSO()
  43.         {
  44.             var root_gos = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();
  45.  
  46.             foreach (var go in root_gos)
  47.             {
  48.                 GetGameObjectHierarchy(go.transform);
  49.             }
  50.  
  51.             FindReferences<InputField, Text>();
  52.             FindReferences<Text, Text>();
  53.         }
  54.  
  55.         void GetGameObjectHierarchy(Transform current)
  56.         {
  57.             GameObjectsWithReferences.Add(current.gameObject);
  58.  
  59.             for (int i = 0; i < current.childCount; i++)
  60.             {
  61.                 GetGameObjectHierarchy(current.GetChild(i));
  62.             }
  63.         }
  64.  
  65.         public void FindReferences<TComponent, TCheck>()
  66.             where TComponent : Component
  67.             where TCheck : Component
  68.         {
  69.             var go_components = new List<Component>();
  70.             foreach (var go in GameObjectsWithReferences)
  71.             {
  72.                 go.GetComponents(go_components);
  73.  
  74.                 foreach (var component in go_components)
  75.                 {
  76.                     FindReferencesInComponent(component);
  77.                 }
  78.  
  79.                 go_components.Clear();
  80.             }
  81.         }
  82.  
  83.         /// <summary>
  84.         /// Get all component of the TCheck type and contained in the referenceComponents.
  85.         /// </summary>
  86.         /// <typeparam name="TComponent">Component type.</typeparam>
  87.         /// <typeparam name="TCheck">Checked type.</typeparam>
  88.         /// <param name="components">Components.</param>
  89.         /// <param name="referenceComponents">Reference components.</param>
  90.         /// <param name="matched">Matched components.</param>
  91.         protected void GetMatchedComponents<TComponent, TCheck>(List<Component> components, List<Component> matched)
  92.             where TComponent : Component
  93.             where TCheck : Component
  94.         {
  95.             matched.Clear();
  96.             foreach (var component in components)
  97.             {
  98.                 var typed = component as TCheck;
  99.                 if (typed != null)
  100.                 {
  101.                     matched.Add(typed);
  102.                 }
  103.             }
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Find references in the specified component to any reference components and create actions to replace references.
  108.         /// </summary>
  109.         /// <param name="component">Component with references.</param>
  110.         protected void FindReferencesInComponent(Component component)
  111.         {
  112.             if (component == null)
  113.             {
  114.                 return;
  115.             }
  116.  
  117.             using (var serialized = new SerializedObject(component))
  118.             {
  119.                 var property = serialized.GetIterator();
  120.  
  121.                 while (property.NextVisible(true))
  122.                 {
  123.                     if (property.propertyType != SerializedPropertyType.ObjectReference)
  124.                     {
  125.                         continue;
  126.                     }
  127.  
  128.                     var reference = property.objectReferenceValue as Component;
  129.                     if (reference == null)
  130.                     {
  131.                         continue;
  132.                     }
  133.  
  134.                     /*
  135.                     if (!References.ContainsKey(reference))
  136.                     {
  137.                         References[reference] = new List<PropertyReference>();
  138.                     }
  139.  
  140.                     References[reference].Add(new PropertyReference(component, property.propertyPath));
  141.                     */
  142.                 }
  143.             }
  144.         }
  145.     }
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement