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

2017-12-16  本文已影响0人  姚宏民
        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);
            }
        }
上一篇下一篇

猜你喜欢

热点阅读