Пример #1
0
    private static void MigrateObject(UnityEngine.Object obj)
    {
        if (obj == null)
        {
            UnityEngine.Debug.LogWarning("WwiseUnity: Missing script! Please consider resolving the missing scripts before migrating your Unity project. Any WwiseType on this object will NOT be migrated!");
            return;
        }

        var migratable = obj as AK.Wwise.IMigratable;

        if (migratable == null && !AkUtilities.IsMigrationRequired(AkUtilities.MigrationStep.WwiseTypes_v2018_1_6))
        {
            return;
        }

        var hasChanged       = false;
        var serializedObject = new UnityEditor.SerializedObject(obj);

        if (migratable != null)
        {
            hasChanged = migratable.Migrate(serializedObject);
        }
        else
        {
            hasChanged = AK.Wwise.TypeMigration.SearchAndProcessWwiseTypes(serializedObject.GetIterator());
        }

        if (hasChanged)
        {
            serializedObject.ApplyModifiedPropertiesWithoutUndo();
        }
    }
Пример #2
0
        private static void CollectDependenciesFromFields(UnityEngine.Object obj, HashSet <UnityEngine.Object> dependencies, int depth)
        {
            if (depth == 0)
            {
                return;
            }

            var so = new UnityEditor.SerializedObject(obj);

            if (so == null)
            {
                return;
            }

            var property = so.GetIterator();

            if (property == null)
            {
                return;
            }

            // Iterate through each of this object's properties.
            while (property.NextVisible(true))
            {
                try
                {
                    // If it's an array which contains UnityEngine.Objects, add them as dependencies.
                    if (property.isArray)
                    {
                        for (int i = 0; i < property.arraySize; i++)
                        {
                            var element = property.GetArrayElementAtIndex(i);
                            // If the array contains UnityEngine.Object types, add them to the dependencies.
                            if (element.propertyType == UnityEditor.SerializedPropertyType.ObjectReference && element.objectReferenceValue != null)
                            {
                                dependencies.Add(element.objectReferenceValue);
                                CollectDependenciesFromFields(element.objectReferenceValue, dependencies, depth - 1);
                            }
                            // Otherwise this array does not contain UnityEngine.Object types, so we should stop.
                            else
                            {
                                break;
                            }
                        }
                    }
                    // Else if it's a normal UnityEngine.Object field, add it.
                    else if (property.propertyType == UnityEditor.SerializedPropertyType.ObjectReference && property.objectReferenceValue != null)
                    {
                        dependencies.Add(property.objectReferenceValue);
                        CollectDependenciesFromFields(property.objectReferenceValue, dependencies, depth - 1);
                    }
                }
                catch { }
            }
        }
Пример #3
0
        public static int GetOrCreateByName(string layerName)
        {
            int layer = LayerMask.NameToLayer(layerName);

            if (layer == -1)
            {
#if UNITY_EDITOR && UNITY_5
                if (string.IsNullOrEmpty(layerName))
                {
                    throw new ArgumentException("null or empty", "layerName");
                }

                // note: if Unity changes its serialization this method breaks
                // tested using "serializedVersion: 2" in Unity 5.1
                const string TagManagerAssetPath = "ProjectSettings/TagManager.asset";
                var          tagManager          = new UnityEditor.SerializedObject(UnityEditor.AssetDatabase.LoadAllAssetsAtPath(TagManagerAssetPath)[0]);
                var          prop    = tagManager.GetIterator();
                var          success = false;
                while (!success && prop.NextVisible(true))
                {
                    if (prop.isArray && prop.name == "layers")
                    {
                        // skip the first 8 layers (built-in)
                        for (int i = 8; i < 32; ++i)
                        {
                            var layerProp = prop.GetArrayElementAtIndex(i);
                            if (string.IsNullOrEmpty(layerProp.stringValue))
                            {
                                layerProp.stringValue = layerName;
                                success = true;
                                break;
                            }
                        }
                        break;
                    }
                }
                if (success &&
                    tagManager.ApplyModifiedProperties() &&
                    (-1 != (layer = LayerMask.NameToLayer(layerName))))
                {
                    Debug.Log("Created layer \"" + layerName + "\" at index " + layer);
                }
                else
                {
                    Debug.LogError("No more layers available. Could not create layer named \"" + layerName + "\".");
                }
#else
                Debug.LogError("Could not find layer named: " + layerName);
#endif
            }
            return(layer);
        }
Пример #4
0
        public static int GetOrCreateByName(string layerName)
        {
            int layer = LayerMask.NameToLayer(layerName);
            if (layer == -1)
            {
            #if UNITY_EDITOR && UNITY_5
                if (string.IsNullOrEmpty(layerName))
                {
                    throw new ArgumentException("null or empty", "layerName");
                }

                // note: if Unity changes its serialization this method breaks
                // tested using "serializedVersion: 2" in Unity 5.1
                const string TagManagerAssetPath = "ProjectSettings/TagManager.asset";
                var tagManager = new UnityEditor.SerializedObject(UnityEditor.AssetDatabase.LoadAllAssetsAtPath(TagManagerAssetPath)[0]);
                var prop = tagManager.GetIterator();
                var success = false;
                while (!success && prop.NextVisible(true))
                {
                    if (prop.isArray && prop.name == "layers")
                    {
                        // skip the first 8 layers (built-in)
                        for (int i = 8; i < 32; ++i)
                        {
                            var layerProp = prop.GetArrayElementAtIndex(i);
                            if (string.IsNullOrEmpty(layerProp.stringValue))
                            {
                                layerProp.stringValue = layerName;
                                success = true;
                                break;
                            }
                        }
                        break;
                    }
                }
                if (success &&
                    tagManager.ApplyModifiedProperties() &&
                    (-1 != (layer = LayerMask.NameToLayer(layerName))))
                {
                    Debug.Log("Created layer \"" + layerName + "\" at index " + layer);
                }
                else
                {
                    Debug.LogError("No more layers available. Could not create layer named \"" + layerName + "\".");
                }
            #else
                Debug.LogError("Could not find layer named: " + layerName);
            #endif
            }
            return layer;
        }
Пример #5
0
        public static void Copy <T>(T copyTo, T copyFrom) where T : Object
        {
                        #if UNITY_EDITOR
            UnityEditor.SerializedObject   copyToSerialized   = new UnityEditor.SerializedObject(copyTo);
            UnityEditor.SerializedObject   copyFromSerialized = new UnityEditor.SerializedObject(copyFrom);
            UnityEditor.SerializedProperty iterator           = copyFromSerialized.GetIterator();

            while (iterator.Next(true))
            {
                copyToSerialized.CopyFromSerializedProperty(iterator);
            }

            copyToSerialized.ApplyModifiedProperties();
                        #endif
        }
        public static T Clone <T>(this T scriptable) where T : ScriptableObject
        {
            T clone = default(T);

                        #if UNITY_EDITOR
            clone = ScriptableObject.CreateInstance <T>();
            UnityEditor.SerializedObject   cloneSerialized      = new UnityEditor.SerializedObject(clone);
            UnityEditor.SerializedObject   scriptableSerialized = new UnityEditor.SerializedObject(scriptable);
            UnityEditor.SerializedProperty scriptableIterator   = scriptableSerialized.GetIterator();

            while (scriptableIterator.NextVisible(true))
            {
                cloneSerialized.FindProperty(scriptableIterator.propertyPath).SetValue(scriptableIterator.GetValue());
            }
                        #endif

            return(clone);
        }
Пример #7
0
        public override void GatherProperties(PlayableDirector director, IPropertyCollector driver)
        {
#if UNITY_EDITOR
            GameObject trackBinding = director.GetGenericBinding(this) as GameObject;
            if (trackBinding != null)
            {
                var serializedObject = new UnityEditor.SerializedObject(trackBinding);
                var iterator         = serializedObject.GetIterator();
                while (iterator.NextVisible(true))
                {
                    if (iterator.hasVisibleChildren)
                    {
                        continue;
                    }

                    driver.AddFromName(trackBinding, iterator.propertyPath);
                }
            }
#endif
            base.GatherProperties(director, driver);
        }
Пример #8
0
        public override void GatherProperties(PlayableDirector director, IPropertyCollector driver)
        {
#if UNITY_EDITOR
            var binding = director.GetGenericBinding(this) as Transform;
            if (binding == null)
            {
                return;
            }
            var so   = new UnityEditor.SerializedObject(binding);
            var iter = so.GetIterator();
            while (iter.NextVisible(true))
            {
                if (iter.hasVisibleChildren)
                {
                    continue;
                }
                driver.AddFromName <Transform>(binding.gameObject, iter.propertyPath);
            }
#endif
            base.GatherProperties(director, driver);
        }
Пример #9
0
            public override void GatherProperties([NotNull] PlayableDirector director, IPropertyCollector driver)
            {
#if UNITY_EDITOR
                // Boilerplate to make sure the track (when running in editor mode) does not permanently overwrite properties
                var comp = director.GetGenericBinding(this) as TBinding;
                if (comp == null)
                {
                    return;
                }
                var so   = new UnityEditor.SerializedObject(comp);
                var iter = so.GetIterator();
                while (iter.NextVisible(true))
                {
                    if (iter.hasVisibleChildren)
                    {
                        continue;
                    }

                    driver.AddFromName <TBinding>(comp.gameObject, iter.propertyPath);
                }
#endif
                base.GatherProperties(director, driver);
            }
Пример #10
0
        private static void CollectDependenciesFromFields(UnityEngine.Object obj, HashSet <UnityEngine.Object> dependencies, int depth)
        {
            // If we've already collected dependencies for this, do nothing.
            if (!dependencies.Add(obj))
            {
                return;
            }

            if (depth < 0)
            {
                return;
            }

            var type = obj.GetType();

            if (isEnteringPlayMode && type == typeof(UnityEngine.UI.Text))
            {
                return;
            }

            try
            {
                // SerializedObject is expensive, so for known classes we manually gather references.

                if (type == typeof(Animator) || obj is Transform || type == typeof(CanvasRenderer) || type == typeof(Mesh) || type == typeof(AudioClip) || type == typeof(Rigidbody) || obj is Texture || obj is HorizontalOrVerticalLayoutGroup)
                {
                    return;
                }

                if (obj is Graphic)
                {
                    var m = (Graphic)obj;
                    dependencies.Add(m.material);
                    dependencies.Add(m.defaultMaterial);
                    dependencies.Add(m.mainTexture);

                    if (type == typeof(Text))
                    {
                        var text = (Text)obj;
                        dependencies.Add(text.font);
                    }
                    else if (type == typeof(Image))
                    {
                        var img = (Image)obj;
                        dependencies.Add(img.sprite);
                    }
                    return;
                }

                if (type == typeof(Mesh))
                {
                    if (UnityEditor.AssetDatabase.Contains(obj))
                    {
                        dependencies.Add(obj);
                    }
                    return;
                }

                if (type == typeof(Material))
                {
                    dependencies.Add(((Material)obj).shader);
                    return;
                }

                if (type == typeof(MeshFilter))
                {
                    dependencies.Add(((MeshFilter)obj).sharedMesh);
                    return;
                }

                if (type == typeof(MeshCollider))
                {
                    var mc = (MeshCollider)obj;
                    dependencies.Add(mc.sharedMesh);
                    dependencies.Add(mc.sharedMaterial);
                    dependencies.Add(mc.attachedRigidbody);
                    return;
                }

                if (type == typeof(Camera))
                {
                    var c = (Camera)obj;
                    dependencies.Add(c.targetTexture);
                    return;
                }

                if (type == typeof(SkinnedMeshRenderer))
                {
                    dependencies.Add(((SkinnedMeshRenderer)obj).sharedMesh); // Don't return. Let this fall through to the if(obj is renderer) call.
                }
                else if (type == typeof(SpriteRenderer))
                {
                    dependencies.Add(((SpriteRenderer)obj).sprite); // Don't return. Let this fall through to the if(obj is renderer) call.
                }
                else if (type == typeof(ParticleSystemRenderer))
                {
                    dependencies.Add(((ParticleSystemRenderer)obj).mesh); // Don't return. Let this fall through to the if(obj is renderer) call.
                }
                if (obj is Renderer)
                {
                    dependencies.UnionWith(((Renderer)obj).sharedMaterials);
                    return;
                }
            }
            catch { }

            var so = new UnityEditor.SerializedObject(obj);

            if (so == null)
            {
                return;
            }

            var property = so.GetIterator();

            if (property == null)
            {
                return;
            }

            // Iterate through each of this object's properties.
            while (property.NextVisible(true))
            {
                try
                {
                    // If it's an array which contains UnityEngine.Objects, add them as dependencies.
                    if (property.isArray)
                    {
                        for (int i = 0; i < property.arraySize; i++)
                        {
                            var element = property.GetArrayElementAtIndex(i);

                            // If the array contains UnityEngine.Object types, add them to the dependencies.
                            if (element.propertyType == UnityEditor.SerializedPropertyType.ObjectReference)
                            {
                                var elementValue = element.objectReferenceValue;
                                var elementType  = elementValue.GetType();

                                // If it's a GameObject, use CollectDependencies so that Components are also added.
                                if (elementType == typeof(GameObject))
                                {
                                    // Only collect deeper dependencies if this GameObject hasn't already had its dependencies added.
                                    if (dependencies.Add(elementValue))
                                    {
                                        CollectDependencies(elementValue, dependencies, depth - 1);
                                    }
                                }
                                else
                                {
                                    // Only collect more dependencies if we've not already added them for this object.
                                    if (dependencies.Add(elementValue))
                                    {
                                        CollectDependenciesFromFields(elementValue, dependencies, depth - 1);
                                    }
                                }
                            }
                            // Otherwise this array does not contain UnityEngine.Object types, so we should stop.
                            else
                            {
                                break;
                            }
                        }
                    }
                    // Else if it's a normal UnityEngine.Object field, add it.
                    else if (property.propertyType == UnityEditor.SerializedPropertyType.ObjectReference)
                    {
                        var propertyValue = property.objectReferenceValue;
                        if (propertyValue == null)
                        {
                            break;
                        }

                        // If it's a GameObject, use CollectDependencies so that Components are also added.
                        if (propertyValue.GetType() == typeof(GameObject))
                        {
                            // Only collect deeper dependencies if this GameObject hasn't already had its dependencies added.
                            if (dependencies.Add(propertyValue))
                            {
                                CollectDependencies(propertyValue, dependencies, depth - 1);
                            }
                        }
                        else
                        {
                            // Only add more dependencies if we've not already added them for this object.
                            if (dependencies.Add(propertyValue))
                            {
                                CollectDependenciesFromFields(propertyValue, dependencies, depth - 1);
                            }
                        }
                    }
                }
                catch { }
            }
        }
Пример #11
0
        public string MakeDefinition(ref int funcIndex, System.Action <FieldItemInfo> onField)
        {
            var output = string.Empty;
            var so     = new UnityEditor.SerializedObject(this.node);
            var iter   = so.GetIterator();

            iter.NextVisible(true);
            var txt = (UnityEngine.TextAsset)iter.objectReferenceValue;

            var pattern = "Box\\s+?Execute.*?\\{((?:[^\\{\\}]|(?<open>\\{)|(?<-open>\\}))+(?(open)(?!)))\\}";

            if (this.onConstruct == true)
            {
                pattern = "void\\s+?OnCreate.*?\\{((?:[^\\{\\}]|(?<open>\\{)|(?<-open>\\}))+(?(open)(?!)))\\}";
            }
            var matches = System.Text.RegularExpressions.Regex.Match(txt.text, pattern, System.Text.RegularExpressions.RegexOptions.Singleline);
            var result  = matches.Groups[1];
            var val     = result.Value;

            // Replace return
            if (this.onConstruct == false)
            {
                val = System.Text.RegularExpressions.Regex.Replace(val, @"return\s+(.*?)\s*;", new System.Text.RegularExpressions.MatchEvaluator((match) => {
                    var target    = match.Groups[1];
                    var r         = target.Value;
                    var rs        = r.Split('.');
                    var fieldName = rs[rs.Length - 1];
                    var prop      = so.FindProperty(fieldName);
                    if (prop == null)
                    {
                        return($"\ngoto __EXIT;\n");
                    }

                    var nextNode = prop.objectReferenceValue as Box;
                    if (nextNode != null)
                    {
                        for (int i = 0; i < this.blueprint.boxes.Length; ++i)
                        {
                            if (this.blueprint.boxes[i].box == nextNode)
                            {
                                return($"\ngoto __STEP{i}; // {nextNode.name}\n");
                            }
                        }
                    }

                    return($"\ngoto __EXIT;\n");
                }));
            }

            // Search for public serialized fields
            var index = funcIndex;

            while (iter.NextVisible(false) == true)
            {
                var fieldName       = iter.name;
                var publicFieldName = $"{fieldName}__step{index}";
                var fieldInfo       = this.node.GetType().GetField(fieldName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
                var itemCompiler    = Compiler.GetFieldItemInfo(this.node, val, fieldInfo, publicFieldName, fieldName);
                var addField        = false;
                if (string.IsNullOrEmpty(itemCompiler.customExecuteCode) == true)
                {
                    var replacedCount = 0;
                    val = System.Text.RegularExpressions.Regex.Replace(val, Compiler.GetClassVariable(fieldName), new System.Text.RegularExpressions.MatchEvaluator((match) => {
                        ++replacedCount;
                        var target = match.Groups[0];
                        var r      = target.Value;
                        return(r.Replace(fieldName, $"{publicFieldName}"));
                    }));
                    addField = replacedCount > 0;

                    var allFields = node.GetType().GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                    foreach (var field in allFields)
                    {
                        var innerFieldName        = field.Name;
                        var innerPrivateFieldName = $"{publicFieldName}__{innerFieldName}";
                        var replace    = $@"{innerFieldName}";
                        var replaceRaw = $@"{innerFieldName}";
                        val = System.Text.RegularExpressions.Regex.Replace(val, Compiler.GetClassVariable(replace), new System.Text.RegularExpressions.MatchEvaluator((match) => {
                            var target = match.Groups[0];
                            var r      = target.Value;
                            return(r.Replace(replaceRaw, $"{innerPrivateFieldName}"));
                        }));

                        onField.Invoke(new FieldItemInfo()
                        {
                            name      = innerPrivateFieldName,
                            type      = field.FieldType,
                            isPrivate = true,
                        });
                    }
                }
                else
                {
                    val      = itemCompiler.customExecuteCode;
                    addField = true;
                }

                if (addField == true)
                {
                    onField.Invoke(itemCompiler.fieldItemInfo);
                    if (itemCompiler.innerFields != null)
                    {
                        foreach (var item in itemCompiler.innerFields)
                        {
                            onField.Invoke(item);
                        }
                    }
                }
            }

            if (this.onConstruct == false)
            {
                var innerFunc = new FunctionCompiler(funcIndex++, val);
                output += "\n";
                output += innerFunc.MakeDefinition();
                output += "\n";
            }
            else
            {
                output += val;
                funcIndex++;
            }

            return(output);
        }
Пример #12
0
        private void Awake()
        {
#if UNITY_EDITOR
            //在scene界面不让选中
            if (LayerMask.NameToLayer("SceneLocked") == -1)
            {
                var tagManager =
                    new UnityEditor.SerializedObject(
                        UnityEditor.AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
                UnityEditor.SerializedProperty it = tagManager.GetIterator();
                bool showChildren = true;
                int  index        = 0;
                bool isLayer      = false;
                while (it.NextVisible(showChildren))
                {
//set your tags here
                    if (it.name == "data")
                    {
                        if (it.stringValue == "UI")
                        {
                            isLayer = true;
                        }
                        if (isLayer)
                        {
                            index++;
                        }
                        if (string.IsNullOrEmpty(it.stringValue) && index > 10)
                        {
                            it.stringValue = "SceneLocked";
                            break;
                        }
                    }
                }
                tagManager.ApplyModifiedProperties();
            }
            gameObject.layer = LayerMask.NameToLayer("SceneLocked");
            UnityEditor.Tools.lockedLayers |= 1 << LayerMask.NameToLayer("SceneLocked");
#endif

            Application.logMessageReceivedThreaded += ReceivedLog;

            // Only one instance of debug console is allowed
            if (!Instance)
            {
                Instance = this;

                // If it is a singleton object, don't destroy it between scene changes
                if (singleton)
                {
                    DontDestroyOnLoad(gameObject);
                }
            }
            else if (Instance != this)
            {
                Destroy(gameObject);
                return;
            }
            //gameObject.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
            pooledLogEntries = new List <DebugLogEntry>(16);
            pooledLogItems   = new List <DebugLogItem>(16);
            queuedLogEntries = new DynamicCircularBuffer <QueuedDebugLogEntry>(16);

            logEntriesLock = new object();

            canvasTR                       = (RectTransform)transform;
            logItemsScrollRectTR           = (RectTransform)logItemsScrollRect.transform;
            logItemsScrollRectOriginalSize = logItemsScrollRectTR.sizeDelta;

            // Associate sprites with log types
            logSpriteRepresentations = new Dictionary <LogType, Sprite>()
            {
                { LogType.Log, infoLog },
                { LogType.Warning, warningLog },
                { LogType.Error, errorLog },
                { LogType.Exception, errorLog },
                { LogType.Assert, errorLog }
            };

            // Initially, all log types are visible
            filterInfoButton.color    = filterButtonsSelectedColor;
            filterWarningButton.color = filterButtonsSelectedColor;
            filterErrorButton.color   = filterButtonsSelectedColor;

            collapsedLogEntries          = new List <DebugLogEntry>(128);
            collapsedLogEntriesMap       = new Dictionary <DebugLogEntry, int>(128);
            uncollapsedLogEntriesIndices = new DebugLogIndexList();
            indicesOfListEntriesToShow   = new DebugLogIndexList();

            recycledListView.Initialize(this, collapsedLogEntries, indicesOfListEntriesToShow, logItemPrefab.Transform.sizeDelta.y);
            recycledListView.UpdateItemsInTheList(true);

            if (minimumHeight < 200f)
            {
                minimumHeight = 200f;
            }

            if (enableSearchbar)
            {
                searchbar.GetComponent <InputField>().onValueChanged.AddListener(SearchTermChanged);
            }
            else
            {
                searchbar = null;
                searchbarSlotTop.gameObject.SetActive(false);
                searchbarSlotBottom.gameObject.SetActive(false);
            }

            hideButton.onClick.AddListener(HideLogWindow);
            clearButton.onClick.AddListener(ClearLogs);
            collapseButton.GetComponent <Button>().onClick.AddListener(CollapseButtonPressed);
            filterInfoButton.GetComponent <Button>().onClick.AddListener(FilterLogButtonPressed);
            filterWarningButton.GetComponent <Button>().onClick.AddListener(FilterWarningButtonPressed);
            filterErrorButton.GetComponent <Button>().onClick.AddListener(FilterErrorButtonPressed);
            snapToBottomButton.GetComponent <Button>().onClick.AddListener(() => SetSnapToBottom(true));
            showCommandBtn.onClick.AddListener(() => commandParent.gameObject.SetActive(!commandParent.gameObject.activeSelf));

            nullPointerEventData = new PointerEventData(null);
            commandBtnTemplate.gameObject.SetActive(false);
            foreach (var method in DebugLogConsole.Methods)
            {
                RefreshMethodButtons(NotifyCollectionChangedAction.Add, method);
            }
            DebugLogConsole.OnCommandChanged += RefreshMethodButtons;
        }
Пример #13
0
        /*
         * Collects all top-level dependencies of an object.
         * For GameObjects, it will traverse all children.
         * For Components or ScriptableObjects, it will get all serialisable UnityEngine.Object fields/properties as dependencies.
         */
        public static HashSet <UnityEngine.Object> CollectDependencies(UnityEngine.Object[] objs)
        {
            var dependencies = new HashSet <UnityEngine.Object>(objs);

            foreach (var obj in objs)
            {
                if (obj == null)
                {
                    continue;
                }

                var type = obj.GetType();
                // Skip types which don't need processing
                if (type == typeof(ES3ReferenceMgr) || type == typeof(ES3Prefab) || type == typeof(ES3AutoSaveMgr) || type == typeof(ES3AutoSave) || type == typeof(ES3InspectorInfo))
                {
                    continue;
                }

                // If it's a GameObject, get the GameObject's Components and collect their dependencies.
                if (type == typeof(GameObject))
                {
                    var go = (GameObject)obj;
                    // Get the dependencies of each Component in the GameObject.
                    dependencies.UnionWith(CollectDependencies(go.GetComponents <Component>()));
                    // Get the dependencies of each child in the GameObject.
                    foreach (Transform child in go.transform)
                    {
                        dependencies.UnionWith(CollectDependencies(child.gameObject));
                    }
                }
                // Else if it's a Component or ScriptableObject, add the values of any UnityEngine.Object fields as dependencies.
                else
                {
                    var so = new UnityEditor.SerializedObject(obj);
                    if (so == null)
                    {
                        continue;
                    }

                    var property = so.GetIterator();
                    if (property == null)
                    {
                        continue;
                    }

                    // Iterate through each of this object's properties.
                    while (property.NextVisible(true))
                    {
                        try
                        {
                            // If it's an array which contains UnityEngine.Objects, add them as dependencies.
                            if (property.isArray)
                            {
                                for (int i = 0; i < property.arraySize; i++)
                                {
                                    var element = property.GetArrayElementAtIndex(i);
                                    // If the array contains UnityEngine.Object types, add them to the dependencies.
                                    if (element.propertyType == UnityEditor.SerializedPropertyType.ObjectReference && element.objectReferenceValue != null)
                                    {
                                        dependencies.Add(element.objectReferenceValue);
                                    }
                                    // Otherwise this array does not contain UnityEngine.Object types, so we should stop.
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                            // Else if it's a normal UnityEngine.Object field, add it.
                            else if (property.propertyType == UnityEditor.SerializedPropertyType.ObjectReference && property.objectReferenceValue != null)
                            {
                                dependencies.Add(property.objectReferenceValue);
                            }
                        }
                        catch { }
                    }
                }
            }

            return(dependencies);
        }
Пример #14
0
        private static void CollectDependenciesFromFields(UnityEngine.Object obj, HashSet <UnityEngine.Object> dependencies, int depth)
        {
            if (depth < 0)
            {
                return;
            }

            if (isEnteringPlayMode && obj.GetType() == typeof(UnityEngine.UI.Text))
            {
                return;
            }

            var so = new UnityEditor.SerializedObject(obj);

            if (so == null)
            {
                return;
            }

            var property = so.GetIterator();

            if (property == null)
            {
                return;
            }

            // Iterate through each of this object's properties.
            while (property.NextVisible(true))
            {
                try
                {
                    // If it's an array which contains UnityEngine.Objects, add them as dependencies.
                    if (property.isArray)
                    {
                        for (int i = 0; i < property.arraySize; i++)
                        {
                            var element = property.GetArrayElementAtIndex(i);
                            // If the array contains UnityEngine.Object types, add them to the dependencies.
                            if (element.propertyType == UnityEditor.SerializedPropertyType.ObjectReference && element.objectReferenceValue != null)
                            {
                                var propertyType = element.objectReferenceValue.GetType();

                                // If it's a GameObject, use CollectDependencies so that Components are also added.
                                if (element.objectReferenceValue.GetType() == typeof(GameObject))
                                {
                                    // Only collect deeper dependencies if this GameObject hasn't already had its dependencies added.
                                    if (dependencies.Add(element.objectReferenceValue))
                                    {
                                        CollectDependencies(element.objectReferenceValue, dependencies, depth - 1);
                                    }
                                }
                                else
                                {
                                    // Only collect more dependencies if we've not already added them for this object.
                                    if (dependencies.Add(element.objectReferenceValue))
                                    {
                                        CollectDependenciesFromFields(element.objectReferenceValue, dependencies, depth - 1);
                                    }
                                }
                            }
                            // Otherwise this array does not contain UnityEngine.Object types, so we should stop.
                            else
                            {
                                break;
                            }
                        }
                    }
                    // Else if it's a normal UnityEngine.Object field, add it.
                    else if (property.propertyType == UnityEditor.SerializedPropertyType.ObjectReference && property.objectReferenceValue != null)
                    {
                        // If it's a GameObject, use CollectDependencies so that Components are also added.
                        if (property.objectReferenceValue.GetType() == typeof(GameObject))
                        {
                            // Only collect deeper dependencies if this GameObject hasn't already had its dependencies added.
                            if (dependencies.Add(property.objectReferenceValue))
                            {
                                CollectDependencies(property.objectReferenceValue, dependencies, depth - 1);
                            }
                        }
                        else
                        {
                            // Only add more dependencies if we've not already added them for this object.
                            if (dependencies.Add(property.objectReferenceValue))
                            {
                                CollectDependenciesFromFields(property.objectReferenceValue, dependencies, depth - 1);
                            }
                        }
                    }
                }
                catch { }
            }
        }
        public static void ReassignShader(UObject o)
        {
#if UNITY_EDITOR
            // shader
            if (o is Shader)
            {
                return;
            }

            // material
            var material = o as Material;
            if (material)
            {
                int render_queue = material.renderQueue;
                material.shader      = Shader.Find(material.shader.name);
                material.renderQueue = render_queue;
                return;
            }

            // renderer
            var renderer = o as Renderer;
            if (renderer)
            {
                var materials = renderer.sharedMaterials;
                for (int i = 0; i < materials.Length; i++)
                {
                    var m            = materials[i];
                    int render_queue = m.renderQueue;
                    m.shader      = Shader.Find(m.shader.name);
                    m.renderQueue = render_queue;
                }
                return;
            }

            var image = o as Image;
            if (image)
            {
                image.material.shader = Shader.Find(image.material.shader.name);
            }

            // entity
            var entity = o as Entity;
            if (entity)
            {
                var components = entity.GetComponentsInChildren <Component>(true);
                for (int i = 0; i < components.Length; i++)
                {
                    ReassignShader(components[i]);
                }
                return;
            }

            // 일반 객체
            var so = new UnityEditor.SerializedObject(o);
            var sp = so.GetIterator();
            while (sp.NextVisible(true))
            {
                if (sp.propertyType != UnityEditor.SerializedPropertyType.ObjectReference)
                {
                    continue;
                }
                if (sp.objectReferenceValue == null)
                {
                    continue;
                }

                var shader = sp.objectReferenceValue as Shader;
                if (shader == null)
                {
                    continue;
                }

                sp.objectReferenceValue = Shader.Find(shader.name);
            }
#endif
        }