美文网首页
[工具]检测引用丢失(Missing)

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

作者: 姚宏民 | 来源:发表于2017-12-16 15:38 被阅读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);
                }
            }
    

    相关文章

      网友评论

          本文标题:[工具]检测引用丢失(Missing)

          本文链接:https://www.haomeiwen.com/subject/xbiswxtx.html