[工具]检测引用丢失(Missing)

        static void CheckMissing(GameObject go, System.Type type)
        {
            bool isAnyChange = false;

            var components = go.GetComponentsInChildren(type, true);
            foreach (var component in components)
            {
                if (component == null)
                {
                    Debug.Log("component missing ");
                    continue;
                }

                SerializedObject so = new SerializedObject(component);
                bool isSoChange = false;
                var sp = so.GetIterator();
                while (sp.Next(true))
                {
                    if (sp.propertyType == SerializedPropertyType.ObjectReference)
                    {
                        if (sp.objectReferenceValue == null
                            && sp.objectReferenceInstanceIDValue != 0)
                        {
                            sp.objectReferenceInstanceIDValue = 0;
                            isSoChange = true;

                            Debug.Log("set missing reference to none: " +
                                go.name + "," +
                                component.gameObject + "." + component.name);
                        }
                    }
                }

                if (isSoChange)
                {
                    so.ApplyModifiedProperties();
                    isAnyChange = true;
                }
            }

            if (isAnyChange)
            {
                EditorUtility.SetDirty(go);
            }
        }

你可能感兴趣的:([工具]检测引用丢失(Missing))